Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
ee4dcf14
Commit
ee4dcf14
authored
Mar 26, 2000
by
Tom Lane
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update/improve documentation about creating aggregate functions.
parent
0a27641c
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
165 additions
and
117 deletions
+165
-117
doc/src/sgml/ref/create_aggregate.sgml
doc/src/sgml/ref/create_aggregate.sgml
+107
-100
doc/src/sgml/xaggr.sgml
doc/src/sgml/xaggr.sgml
+58
-17
No files found.
doc/src/sgml/ref/create_aggregate.sgml
View file @
ee4dcf14
This diff is collapsed.
Click to expand it.
doc/src/sgml/xaggr.sgml
View file @
ee4dcf14
...
...
@@ -2,26 +2,57 @@
<Title>Extending <Acronym>SQL</Acronym>: Aggregates</Title>
<Para>
Aggregates in <ProductName>Postgres</ProductName>
are expressed in terms of state
transition functions. That is, an aggregate can be
Aggregate functions in <ProductName>Postgres</ProductName>
are expressed as <firstterm>state values</firstterm>
and <firstterm>state transition functions</firstterm>.
That is, an aggregate can be
defined in terms of state that is modified whenever an
instance is processed. Some state functions look at a
particular value in the instance when computing the new
state (<Acronym>sfunc1</Acronym> in the
create aggregate syntax) while
others only keep track of their own internal state
(<Acronym>sfunc2</Acronym>).
If we define an aggregate that uses only
<Acronym>sfunc1</Acronym>, we
define an aggregate that computes a running function of
input item is processed. To define a new aggregate
function, one selects a datatype for the state value,
an initial value for the state, and a state transition
function. The state transition function is just an
ordinary function that could also be used outside the
context of the aggregate.
</Para>
<Para>
Actually, in order to make it easier to construct useful
aggregates from existing functions, an aggregate can have
one or two separate state values, one or two transition
functions to update those state values, and a
<firstterm>final function</firstterm> that computes the
actual aggregate result from the ending state values.
</Para>
<Para>
Thus there can be as many as four datatypes involved:
the type of the input data items, the type of the aggregate's
result, and the types of the two state values. Only the
input and result datatypes are seen by a user of the aggregate.
</Para>
<Para>
Some state transition functions need to look at each successive
input to compute the next state value, while others ignore the
specific input value and simply update their internal state.
(The most useful example of the second kind is a running count
of the number of input items.) The <ProductName>Postgres</ProductName>
aggregate machinery defines <Acronym>sfunc1</Acronym> for
an aggregate as a function that is passed both the old state
value and the current input value, while <Acronym>sfunc2</Acronym>
is a function that is passed only the old state value.
</Para>
<Para>
If we define an aggregate that uses only <Acronym>sfunc1</Acronym>,
we have an aggregate that computes a running function of
the attribute values from each instance. "Sum" is an
example of this kind of aggregate. "Sum" starts at
zero and always adds the current instance's value to
its running total.
We will use the
<Acronym>int4pl</Acronym> that is
built into <ProductName>Postgres</ProductName>
to perform this addition.
its running total.
For example, if we want to make a Sum
aggregate to work on a datatype for complex numbers,
we only need the addition function for that datatype.
The aggregate definition is:
<ProgramListing>
CREATE AGGREGATE complex_sum (
...
...
@@ -39,11 +70,15 @@ SELECT complex_sum(a) FROM test_complex;
|(34,53.9) |
+------------+
</ProgramListing>
(In practice, we'd just name the aggregate "sum", and rely on
<ProductName>Postgres</ProductName> to figure out which kind
of sum to apply to a complex column.)
</Para>
<Para>
If we define only <Acronym>sfunc2</Acronym>, we are
specifying an aggregate
specifying an aggregate
that computes a running function that is independent of
the attribute values from each instance.
"Count" is the most common example of this kind of
...
...
@@ -104,4 +139,10 @@ SELECT my_average(salary) as emp_average FROM EMP;
+------------+
</ProgramListing>
</Para>
<Para>
For further details see
<xref endterm="sql-createaggregate-title"
linkend="sql-createaggregate-title">.
</Para>
</Chapter>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment