mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-21 17:03:26 +02:00
can tell whether it is being used as an aggregate or not. This allows such a function to avoid re-pallocing a pass-by-reference transition value; normally it would be unsafe for a function to scribble on an input, but in the aggregate case it's safe to reuse the old transition value. Make int8inc() do this. This gets a useful improvement in the speed of COUNT(*), at least on narrow tables (it seems to be swamped by I/O when the table rows are wide). Per a discussion in early December with Neil Conway. I also fixed int_aggregate.c to check this, thereby turning it into something approaching a supportable technique instead of being a crude hack.
34 lines
1010 B
MySQL
34 lines
1010 B
MySQL
-- Adjust this setting to control where the objects get created.
|
|
SET search_path = public;
|
|
|
|
-- Internal function for the aggregate
|
|
-- Is called for each item in an aggregation
|
|
CREATE OR REPLACE FUNCTION int_agg_state (int4[], int4)
|
|
RETURNS int4[]
|
|
AS 'MODULE_PATHNAME','int_agg_state'
|
|
LANGUAGE C;
|
|
|
|
-- Internal function for the aggregate
|
|
-- Is called at the end of the aggregation, and returns an array.
|
|
CREATE OR REPLACE FUNCTION int_agg_final_array (int4[])
|
|
RETURNS int4[]
|
|
AS 'MODULE_PATHNAME','int_agg_final_array'
|
|
LANGUAGE C;
|
|
|
|
-- The aggregate function itself
|
|
-- uses the above functions to create an array of integers from an aggregation.
|
|
CREATE AGGREGATE int_array_aggregate (
|
|
BASETYPE = int4,
|
|
SFUNC = int_agg_state,
|
|
STYPE = int4[],
|
|
FINALFUNC = int_agg_final_array
|
|
);
|
|
|
|
-- The enumeration function
|
|
-- returns each element in a one dimensional integer array
|
|
-- as a row.
|
|
CREATE OR REPLACE FUNCTION int_array_enum(int4[])
|
|
RETURNS setof integer
|
|
AS 'MODULE_PATHNAME','int_enum'
|
|
LANGUAGE C IMMUTABLE STRICT;
|