Re: Should we optimize the `ORDER BY random() LIMIT x` case? - Mailing list pgsql-hackers

From Andrei Lepikhov
Subject Re: Should we optimize the `ORDER BY random() LIMIT x` case?
Date
Msg-id 129260c2-6c02-4a49-9143-091f6bf81cd6@gmail.com
Whole thread Raw
In response to Re: Should we optimize the `ORDER BY random() LIMIT x` case?  (Aleksander Alekseev <aleksander@timescale.com>)
List pgsql-hackers
On 15/5/2025 11:17, Aleksander Alekseev wrote:
>> What kind of optimisation trick may the optimiser use here to provide an
>> optimal plan? As I see it, it will need to think that all the tuples
>> should be returned from the subquery. The only profit is to skip sorting
>> the massive sample.
> 
> Doesn't look like a generic optimization trick will help us. I was
> thinking about a custom aggregate function, e.g. `SELECT sample(*, 10)
> ...`. However I doubt that aggregate functions are flexible enough. Or
> alternatively a rewrite rule. I never dealt with those before so I
> have no idea what I'm talking about :D
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.

-- 
regards, Andrei Lepikhov



pgsql-hackers by date:

Previous
From: Dilip Kumar
Date:
Subject: Re: Assertion failure in smgr.c when using pg_prewarm with partitioned tables
Next
From: Fujii Masao
Date:
Subject: Re: Assertion failure in smgr.c when using pg_prewarm with partitioned tables