-
Notifications
You must be signed in to change notification settings - Fork 164
Ordering by random #671
Comments
PostgreSQL supports a lot of stuff, that PostgREST doesn't. Adding full support for everything that PostgreSQL does is certainly not achievable. I wouldn't consider this a valid argument for supporting anything.
We can look at this from two different angles:
In any case, I think you can already use it right now with a virtual / computed column. Just define it like this: create table my_table (...);
create function random(my_table) returns double precision
language sql as 'select random()'; You can then use it as a column on this endpoint: GET /my_table?order=random You could probably generalize the function to take |
Supporting TABLESAMPLE sounds more likely to happen. More details at: https://www.2ndquadrant.com/en/blog/tablesample-and-other-methods-for-getting-random-tuples/ |
TABLESAMPLE seems a bit complex to expose to clients. Since doing create function random(anyelement) returns double precision
language sql as 'select random()'; Is pretty simple and makes random available for all tables, we could turn this into a This depends on PostgREST/postgrest#2442, because we'd need to be able to restrict to which columns the order by can be applied. |
Similarly to the estimated count, perhaps we can apply the We could do it through an Edit: Maybe a header isn't right here as different media types can support random. |
pg16 has a new
Haven't tried it yet but seems it could be used in conjunction with custom media types to get a random row for any relation. |
It does not return a random row, however. It returns "a" row. IIRC, the current implementation is that it returns the "first" rows it finds. That's certainly not going to be a true random. |
Ah, ok. Then it's like |
Nope, it's not, because this is only an implementation detail and not guaranteed to stay that way. You can't rely on it. |
as postgres supports
order by random()
, please add similar functionality to PostgRESTEdit: Similar requests:
The text was updated successfully, but these errors were encountered: