From ac43756dac605549ad05abc154b9ba2b42d80739 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 5 Apr 2009 22:28:59 +0000 Subject: [PATCH] Change cardinality() into a C-code function, instead of a SQL-language 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 | 22 ++++++++++++++++++++++ src/include/catalog/catversion.h | 2 +- src/include/catalog/pg_proc.h | 4 ++-- src/include/utils/array.h | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index 85f860a32a..b94eae228f 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -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 diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 74f06d54ca..05305b7dff 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -53,6 +53,6 @@ */ /* yyyymmddN */ -#define CATALOG_VERSION_NO 200903311 +#define CATALOG_VERSION_NO 200904051 #endif diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 61d24c8690..1f4ecb038b 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -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_ )); diff --git a/src/include/utils/array.h b/src/include/utils/array.h index 24f8f32b65..d49c5f3ef3 100644 --- a/src/include/utils/array.h +++ b/src/include/utils/array.h @@ -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); -- 2.39.5