From 2e7a68896bfa84b28cd57e23e141aa9c899275c7 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 12 Apr 2005 04:26:34 +0000 Subject: Add aggsortop column to pg_aggregate, so that MIN/MAX optimization can be supported for all datatypes. Add CREATE AGGREGATE and pg_dump support too. Add specialized min/max aggregates for bpchar, instead of depending on text's min/max, because otherwise the possible use of bpchar indexes cannot be recognized. initdb forced because of catalog changes. --- doc/src/sgml/catalogs.sgml | 12 ++++++++--- doc/src/sgml/ref/create_aggregate.sgml | 39 +++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index 11d774410d3..5a171e94969 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -1,6 +1,6 @@ @@ -250,11 +250,17 @@ pg_proc.oid Final function (zero if none) + + aggsortop + oid + pg_operator.oid + Associated sort operator (zero if none) + aggtranstype oid pg_type.oid - The type of the aggregate function's internal transition (state) data + Data type of the aggregate function's internal transition (state) data agginitval @@ -263,7 +269,7 @@ The initial value of the transition state. This is a text field containing the initial value in its external string - representation. If the value is null, the transition state + representation. If this field is null, the transition state value starts out null. diff --git a/doc/src/sgml/ref/create_aggregate.sgml b/doc/src/sgml/ref/create_aggregate.sgml index 4ae1d1dd8cd..24e233f5894 100644 --- a/doc/src/sgml/ref/create_aggregate.sgml +++ b/doc/src/sgml/ref/create_aggregate.sgml @@ -1,5 +1,5 @@ @@ -26,6 +26,7 @@ CREATE AGGREGATE name ( STYPE = state_data_type [ , FINALFUNC = ffunc ] [ , INITCOND = initial_condition ] + [ , SORTOP = sort_operator ] ) @@ -125,6 +126,29 @@ CREATE AGGREGATE name ( avg returns null when it sees there were zero input rows. + + + Aggregates that behave like MIN or MAX can + sometimes be optimized by looking into an index instead of scanning every + input row. If this aggregate can be so optimized, indicate it by + specifying a sort operator. The basic requirement is that + the aggregate must yield the first element in the sort ordering induced by + the operator; in other words + +SELECT agg(col) FROM tab; + + must be equivalent to + +SELECT col FROM tab ORDER BY col USING sortop LIMIT 1; + + Further assumptions are that the aggregate ignores null inputs, and that + it delivers a null result if and only if there were no non-null inputs. + Ordinarily, a datatype's < operator is the proper sort + operator for MIN, and > is the proper sort + operator for MAX. Note that the optimization will never + actually take effect unless the specified operator is the LessThan or + GreaterThan strategy member of a btree index opclass. + @@ -211,6 +235,19 @@ CREATE AGGREGATE name ( + + + sort_operator + + + The associated sort operator for a MIN- or + MAX-like aggregate. + This is just an operator name (possibly schema-qualified). + The operator is assumed to have the same input datatypes as + the aggregate. + + + -- cgit v1.2.3