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

From Nico Williams
Subject Re: Should we optimize the `ORDER BY random() LIMIT x` case?
Date
Msg-id aCuaT2gFGra1ba4L@ubby
Whole thread Raw
In response to Re: Should we optimize the `ORDER BY random() LIMIT x` case?  (Nico Williams <nico@cryptonector.com>)
List pgsql-hackers
On Mon, May 19, 2025 at 10:38:19AM -0500, Nico Williams wrote:
> On Mon, May 19, 2025 at 01:25:00PM +0300, Aleksander Alekseev wrote:
> > I agree this would be most convenient for the user. Unfortunately this
> > will require us to check every SELECT query: "oh, isn't it by any
> > chance ORDER BY random() LIMIT x?". I don't think we can't afford such
> > a performance degradation, even a small one, for an arguably rare
> > case.
> 
> Can the detection of such queries be done by the yacc/bison parser
> grammar?

Maybe the `sortby` rule could check if the expression is `random()`,
then `sort_clause` could check if `$3` is a one-item `sortby_list` of
just `random()` and mark `$$` as special -- this should be cheap, yes?
We'd still need to check for `LIMIT` somewhere else.



pgsql-hackers by date:

Previous
From: Peter Geoghegan
Date:
Subject: Re: strange perf regression with data checksums
Next
From: Hari Krishna Sunder
Date:
Subject: Re: Statistics Import and Export