Change cardinality() into a C-code function, instead of a SQL-language
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 5 Apr 2009 22:28:59 +0000 (22:28 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 5 Apr 2009 22:28:59 +0000 (22:28 +0000)
alias for array_length(v,1).  The efficiency gain here is doubtless
negligible --- what I'm interested in is making sure that if we have
second thoughts about the definition, we will not have to force a
post-beta initdb to change the implementation.

src/backend/utils/adt/arrayfuncs.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/utils/array.h

index 85f860a32a1e0097cf302f8a6d2ee94a2448c980..b94eae228f95e4aff6b192d95d42854c3be4ec02 100644 (file)
@@ -1668,6 +1668,28 @@ array_length(PG_FUNCTION_ARGS)
        PG_RETURN_INT32(result);
 }
 
+/*
+ * array_cardinality :
+ *             SQL-spec alias for array_length(v, 1)
+ */
+Datum
+array_cardinality(PG_FUNCTION_ARGS)
+{
+       ArrayType  *v = PG_GETARG_ARRAYTYPE_P(0);
+       int                *dimv;
+       int                     result;
+
+       /* Sanity check: does it look like an array at all? */
+       if (ARR_NDIM(v) <= 0 || ARR_NDIM(v) > MAXDIM)
+               PG_RETURN_NULL();
+
+       dimv = ARR_DIMS(v);
+
+       result = dimv[0];
+
+       PG_RETURN_INT32(result);
+}
+
 /*
  * array_ref :
  *       This routine takes an array pointer and a subscript array and returns
index 74f06d54cac508c8caf2f19084fddb9d82984b72..05305b7dfff10452a2c3703b134057d294a3b31d 100644 (file)
@@ -53,6 +53,6 @@
  */
 
 /*                                                     yyyymmddN */
-#define CATALOG_VERSION_NO     200903311
+#define CATALOG_VERSION_NO     200904051
 
 #endif
index 61d24c8690ccd536a2cb673a42dcd86871241f6f..1f4ecb038b9a841316ae6ae4e4578eb6722110cd 100644 (file)
@@ -1005,8 +1005,8 @@ DATA(insert OID = 2092 (  array_upper        PGNSP PGUID 12 1 0 0 f f f t f i 2 0 23
 DESCR("array upper dimension");
 DATA(insert OID = 2176 (  array_length     PGNSP PGUID 12 1 0 0 f f f t f i 2 0 23 "2277 23" _null_ _null_ _null_ _null_ array_length _null_ _null_ _null_ ));
 DESCR("array length");
-DATA(insert OID = 2179 (  cardinality      PGNSP PGUID 14 1 0 0 f f f t f i 1 0 23 "2277" _null_ _null_ _null_ _null_ "select pg_catalog.array_length($1, 1)" _null_ _null_ _null_ ));
-DESCR("array length");
+DATA(insert OID = 2179 (  cardinality      PGNSP PGUID 12 1 0 0 f f f t f i 1 0 23 "2277" _null_ _null_ _null_ _null_ array_cardinality _null_ _null_ _null_ ));
+DESCR("array cardinality");
 DATA(insert OID = 378 (  array_append     PGNSP PGUID 12 1 0 0 f f f f f i 2 0 2277 "2277 2283" _null_ _null_ _null_ _null_ array_push _null_ _null_ _null_ ));
 DESCR("append element onto end of array");
 DATA(insert OID = 379 (  array_prepend    PGNSP PGUID 12 1 0 0 f f f f f i 2 0 2277 "2283 2277" _null_ _null_ _null_ _null_ array_push _null_ _null_ _null_ ));
index 24f8f32b65d74289b9dcd0a8a33b5e4604d667a3..d49c5f3ef395b20e2ae3972b69c93523b9c605ae 100644 (file)
@@ -200,6 +200,7 @@ extern Datum array_dims(PG_FUNCTION_ARGS);
 extern Datum array_lower(PG_FUNCTION_ARGS);
 extern Datum array_upper(PG_FUNCTION_ARGS);
 extern Datum array_length(PG_FUNCTION_ARGS);
+extern Datum array_cardinality(PG_FUNCTION_ARGS);
 extern Datum array_larger(PG_FUNCTION_ARGS);
 extern Datum array_smaller(PG_FUNCTION_ARGS);
 extern Datum generate_subscripts(PG_FUNCTION_ARGS);