Home > Software engineering >  Difference between aggregate and function in PostgreSQL
Difference between aggregate and function in PostgreSQL

Time:07-08

What is the difference between an aggregate and a function in PostgreSQL?

Is an aggregate just a specialized function? (Aggregate constraint: Parameter must be a multiset, the return value must be a single value)

Can I use a function everywhere where an aggregate is possible?

Are there any specialties when using either a PostgreSQL function or an aggregate?

CodePudding user response:

Aggregates are a subset of the functions.

Every aggregate is also a function (the input argument is of type array/set of values and the output is of type single scalar value)

You can replace each aggregate with any other aggregate - and get a different outcome - but if you replace it with a function which does not aggregate input values then you might get an error.

The speciality comes from the definition - aggregates take a list, perform some calculations over its values and produce a single result (which is obviously not a list). Other functions might not work with list inputs - e.g. LOWER, UPPER, SQRT, and so on.

CodePudding user response:

The difference depends on perspective. Internally aggregate function is executed as repeated calling of "cumulative" function (reads a value and updates state cumulative value) and "final" function (transforms cumulative value to final result). From user's perspective, the aggregate functions are functions too, but with different purpose and usage. The aggregate function and scalar functions are not replaceable. You cannot to use scalar function where aggregate function should be used, and you cannot to use aggregate function, where scalar function should be used. Aggregate functions are calculated across rows, scalar functions are calculated just per one row. Syntax of aggregate and scalar function is same, but everything else is significantly different.

CodePudding user response:

Yes, aggregate functions are specialised. Consider almost any SQL documentation and in most (hopefully all) you will find a reference to functions with many types of functions listed. One of those types is "Aggregate functions". From an example list:

Chapter 9. Functions and Operators
Table of Contents

9.1. Logical Operators
9.2. Comparison Functions and Operators
9.3. Mathematical Functions and Operators
9.4. String Functions and Operators
9.5. Binary String Functions and Operators
9.6. Bit String Functions and Operators
9.7. Pattern Matching
9.8. Data Type Formatting Functions
9.9. Date/Time Functions and Operators
...
9.21. Aggregate Functions

(Extract as at PostgreSQL 14). See Functions and Aggregate Functions (current version links)

You can substitute one aggregate function for another aggregate function e.g. you could swap MIN(column1) with MAX(column1) without causing a syntax error - but clearly the result could be very different. However it isn't possible to just substitute any function for an aggregation.

"Can I use a function everywhere where an aggregate is possible?"

Not in the sense you can "swap" any function (as already discussed) but you can include non-aggregation functions in a group by query, e.g.:

select date_trunc('month', datecolumn), sum(quantity)
from table1
group by date_trunc('month', datecolumn)

"Are there any specialties when using either a PostgreSQL function or an aggregate?"

Yes and No. Fully SQL standard compliant functions (e.g. MIN/MAX/SUM/COUNT) are just that "standards compliant". However more exotic functions do exist that may not exist in other dbms e.g. jsonb_object_agg(). Use the documentation to explore the many available functions.

One very useful specialised Postgres function (which returns a "set") is generate_series() e.g.

generate_series ( start timestamp, stop timestamp, step interval )

see: 9.25. Set Returning Functions

Note: Functions can also be "user defined" (i.e. built by a user). So the complete list of functions available to you may include some of these as well - and these will be "highly specialised" as they may be unique to your organisation.

  • Related