From: | Aleksander Alekseev <aleksander(at)timescale(dot)com> |
---|---|
To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Cc: | Andrei Lepikhov <lepihov(at)gmail(dot)com>, wenhui qiu <qiuwenhuifx(at)gmail(dot)com> |
Subject: | Re: Should we optimize the `ORDER BY random() LIMIT x` case? |
Date: | 2025-05-16 12:01:04 |
Message-ID: | CAJ7c6TOXb3iRgSYO_2BM9CVkPgjaU1247gLpr5tiJGG4Nmmyug@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Hi,
> A custom SRF seems great to me. You may propose such an aggregate in the
> core - it seems it doesn't even need any syntax changes. For example:
> SELECT * FROM (SELECT sample(q, 10, <type>) FROM (SELECT ...) AS q);
> or something like that.
I experimented with this idea a bit and it looks like this is not going to work.
In our case sample() should behave both as an aggregate function and
as SRF. An aggregate function computes a single result [1]. We
probably could bypass this by returning an array but it would be
inconvenient for the user. The problem with SRFs is that we never know
when SRF is called the last time e.g. there is no SRF_IS_LASTCALL().
So there is no way to aggregate the tuples until we receive the last
one and then return the sample. I don't think SRF can know this in a
general case (JOINs, SELECT .. LIMIT .., etc). Unless I'm missing
something of course. Perhaps somebody smarter than me could show us
the exact way to implement sample() as an SRF, but at the moment
personally I don't see it.
On top of that it's worth noting that the query you showed above
wouldn't be such a great UI in my opinion.
If I'm right about the limitations of aggregate functions and SRFs
this leaves us the following options:
1. Changing the constraints of aggregate functions or SRFs. However I
don't think we want to do it for such a single niche scenario.
2. Custom syntax and a custom node.
3. To give up
Thoughts?
[1]: https://www.postgresql.org/docs/current/functions-aggregate.html
--
Best regards,
Aleksander Alekseev
From | Date | Subject | |
---|---|---|---|
Next Message | Fujii Masao | 2025-05-16 12:01:24 | Re: Assertion failure in smgr.c when using pg_prewarm with partitioned tables |
Previous Message | Amit Kapila | 2025-05-16 11:31:03 | Re: Conflict detection for update_deleted in logical replication |