Re: Allow WindowFuncs prosupport function to use more optimal WindowClause options

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Vik Fearing <vik(at)postgresfriends(dot)org>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Erwin Brandstetter <brsaweda(at)gmail(dot)com>
Subject: Re: Allow WindowFuncs prosupport function to use more optimal WindowClause options
Date: 2022-10-26 01:38:22
Message-ID: CAApHDvpkktr-OOact6BoT6pB-V5XdD4Fmj7a8wns68oWqzoFnw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, 23 Oct 2022 at 03:03, Vik Fearing <vik(at)postgresfriends(dot)org> wrote:
> Shouldn't it be able to detect that these two windows are the same and
> only do one WindowAgg pass?
>
>
> explain (verbose, costs off)
> select row_number() over w1,
> lag(amname) over w2
> from pg_am
> window w1 as (order by amname),
> w2 as (w1 rows unbounded preceding)
> ;

Good thinking. I think the patch should also optimise that case. It
requires re-doing a similar de-duplication phase the same as what's
done in transformWindowFuncCall(). I've added code to do that in the
attached version.

This got me wondering if the support function, instead of returning
some more optimal versions of the frameOptions, I wondered if it
should just return which aspects of the WindowClause it does not care
about. For example,

SELECT row_number() over (), lag(relname) over (order by relname)
from pg_class;

could, in theory, have row_number() reuse the WindowAgg for lag. Here
because the WindowClause for row_number() has an empty ORDER BY
clause, I believe it could just reuse the lag's WindowClause. It
wouldn't be able to do that if row_number() had an ORDER BY, or if
row_number() were some other WindowFunc that cared about peer rows.
I'm currently thinking this might not be worth the trouble as it seems
a bit unlikely that someone would use row_number() and not care about
the ORDER BY. However, maybe the row_number() could reuse some other
WindowClause with a more strict ordering. My current thoughts are that
this feels a bit too unlikely to apply in enough cases for it to be
worthwhile. I just thought I'd mention it for the sake of the
archives.

David

Thanks for taking it for a spin.

David

Attachment Content-Type Size
v4_optimize_windowfuncs_frameoptions_when_possible.patch text/plain 17.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Melanie Plageman 2022-10-26 03:15:06 Re: pg_stat_bgwriter.buffers_backend is pretty meaningless (and more?)
Previous Message Michael Paquier 2022-10-26 00:43:53 Re: Issue in GIN fast-insert: XLogBeginInsert + Read/LockBuffer ordering