pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Implementing product-aggregate
4+ messages / 2 participants
[nested] [flat]

* Implementing product-aggregate
@ 2024-03-11 09:16 Jan Kohnert <nospam001-lists@jan-kohnert.de>
  2024-03-14 07:36 ` Re: Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
  0 siblings, 1 reply; 4+ messages in thread

From: Jan Kohnert @ 2024-03-11 09:16 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi,

we need a product aggregate and used to implement this as 

exp(sum(ln([COLUMN])))

While using the sum of logarithms is working RDBMS-independently, we'd like to 
switch to a more PostgreSQL native way of doing this and implement an 
aggregate to be used. Currently the implementation is

create aggregate prod(numeric) (sfunc = numeric_mul, stype = numeric)

This is simply calling the implementation funtion of the *-Operator for the numeric 
datatype. Since I could not find any documentation of this implementation 
function, I am wondering, if using a possibly internal function might be a bad idea.

Are there any recommendations on this?

Thanks for any input!

-- 
MfG Jan

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Implementing product-aggregate
  2024-03-11 09:16 Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
@ 2024-03-14 07:36 ` Jan Kohnert <nospam001-lists@jan-kohnert.de>
  2024-03-14 14:17   ` Re: Implementing product-aggregate Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Jan Kohnert @ 2024-03-14 07:36 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi again,

Am Montag, 11. März 2024, 10:16:33 CET schrieb Jan Kohnert:
> create aggregate prod(numeric) (sfunc = numeric_mul, stype = numeric)

my basic idea was creating a function

create function multiply(arg1 numeric, arg2 numeric)
    returns numeric 
    language sql
    immutable
    returns null on null input
    return arg1 * arg2;

and use that function instead of the undocumented numeric_mul as the sfunc in 
the aggregate definition.

Then again, this seems odd, too, since we're only reimplementing basic stuff that's 
already there.

I'm still undecided...

-- 
MfG Jan

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Implementing product-aggregate
  2024-03-11 09:16 Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
  2024-03-14 07:36 ` Re: Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
@ 2024-03-14 14:17   ` Tom Lane <tgl@sss.pgh.pa.us>
  2024-03-14 18:26     ` Re: Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2024-03-14 14:17 UTC (permalink / raw)
  To: Jan Kohnert <nospam001-lists@jan-kohnert.de>; +Cc: pgsql-general@lists.postgresql.org

Jan Kohnert <nospam001-lists@jan-kohnert.de> writes:
> Am Montag, 11. März 2024, 10:16:33 CET schrieb Jan Kohnert:
>> create aggregate prod(numeric) (sfunc = numeric_mul, stype = numeric)
> ...
> Then again, this seems odd, too, since we're only reimplementing basic stuff that's 
> already there.

I wouldn't be concerned about relying on numeric_mul (or any of the
other functions underlying standard operators).  They're undocumented
only because documenting both the functions and the operators would
bloat the documentation to little purpose.  Using one makes your code
not so portable to non-Postgres DBMSes, but you already crossed that
bridge by deciding to use a custom aggregate.

A bigger question is whether this implementation actually has the
properties you want --- notably, maybe you should be using type
float8 not numeric.  Numeric would get pretty slow and be carrying
an awful lot of decimal places by the end of the query, I fear.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Implementing product-aggregate
  2024-03-11 09:16 Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
  2024-03-14 07:36 ` Re: Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
  2024-03-14 14:17   ` Re: Implementing product-aggregate Tom Lane <tgl@sss.pgh.pa.us>
@ 2024-03-14 18:26     ` Jan Kohnert <nospam001-lists@jan-kohnert.de>
  0 siblings, 0 replies; 4+ messages in thread

From: Jan Kohnert @ 2024-03-14 18:26 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hello,

Am Donnerstag, 14. März 2024, 15:17:58 CET schrieb Tom Lane:
> I wouldn't be concerned about relying on numeric_mul (or any of the
> other functions underlying standard operators).  They're undocumented
> only because documenting both the functions and the operators would
> bloat the documentation to little purpose.  Using one makes your code
> not so portable to non-Postgres DBMSes, but you already crossed that
> bridge by deciding to use a custom aggregate.

thank you for clearifying this. We're not too concerned about portability. Let's face 
the facts: Porting a reasonably complex database and the application using it 
from one DBMS to another will almost certainly introduce an awful lot of 
portability issues (f.e. the pseudo-types (big)serial, upserts, differences in merge 
implementations, progammability, and so on). My main concern was, that 
undocumented features sometimes tend to change without notice, since users 
are not expected to use them..
 
> A bigger question is whether this implementation actually has the
> properties you want --- notably, maybe you should be using type
> float8 not numeric.  Numeric would get pretty slow and be carrying
> an awful lot of decimal places by the end of the query, I fear.

This needs to be checked on our side. I was expecting, that using an aggregate 
this way would be significantly faster than using exp(sum(log())). Though we're 
not multiplying too many lines in a statement, if using the aggregate slows down 
performance, we should propably stick the old way doing it.

Best regards!

-- 
MfG Jan

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2024-03-14 18:26 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-03-11 09:16 Implementing product-aggregate Jan Kohnert <nospam001-lists@jan-kohnert.de>
2024-03-14 07:36 ` Jan Kohnert <nospam001-lists@jan-kohnert.de>
2024-03-14 14:17   ` Tom Lane <tgl@sss.pgh.pa.us>
2024-03-14 18:26     ` Jan Kohnert <nospam001-lists@jan-kohnert.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox