Re: range_agg extremely slow compared to naive implementation in obscure circumstances

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Duncan Sands <duncan(dot)sands(at)deepbluecap(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: range_agg extremely slow compared to naive implementation in obscure circumstances
Date: 2023-01-30 12:36:21
Message-ID: CAApHDvrLODBkpcPUAEqgUy-aY85xy5T6nm+=aOf+8weOOYAxOw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Mon, 30 Jan 2023 at 23:43, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> Maybe there is some problem in range_deserialize function

It seems to be that range_deserialize is called from within
range_compare which is the qsort comparison function (see
multirange_canonicalize). That'll end up calling range_deserialize
twice, once for each item being compared about O(N log N) times.

Ordinarily, this probably isn't too bad as we only do this in the
aggregate's final function. It's likely the performance is bad here
as the aggregate is being used as a window function and the finalfn
must be called once for each row rather than once per group as it
would if it was being used as a normal aggregate function.

It might be better if we had multirange_canonicalize() deserialize
these once and used some representation that could more easily be
qsorted. I'm not planning on doing any work on it though.

It's probably unlikely that we'd do anything about this as part of a bug fix.

David

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Bruce Momjian 2023-01-30 15:34:20 Re: FW: Query execution failure
Previous Message Pavel Stehule 2023-01-30 10:42:45 Re: range_agg extremely slow compared to naive implementation in obscure circumstances