| From: | Mayrom Rabinovich <mayromrabinovich(at)gmail(dot)com> |
|---|---|
| To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Cc: | pgsql-hackers(at)postgresql(dot)org |
| Subject: | Re: Planing edge case for sorts with limit on non null column |
| Date: | 2026-02-10 09:29:21 |
| Message-ID: | CAH-Ro_32WZ=BQxmTbOoD-p7nKj4jf7=bH+=yPzryM=j+MQkFdA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Thanks for the quick response,
On Thu, Feb 5, 2026 at 5:46 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Also, the question could be turned around: if you know
> that the table contains no nulls, why are you going out of your way to
> specify the "wrong" null order?
That query was generated by an ORM, and I didn't want to create a new
index on my table just for that query because of the overhead
associated with it.
So I ended up patching the ORM library I used in order to drop the
null ordering if the column is non null. But still, that caught me off
guard. I was expecting Postgres to build a better plan for the query.
Here is a simple example that shows how I stumbled into that edge case:
```
-- Create a table to query using created_at as a pagination cursor
CREATE TABLE d (i INT PRIMARY KEY, created_at TIMESTAMP NOT NULL DEFAULT NOW());
CREATE INDEX a ON d (created_at);
-- Get the next 10 records using the "a" index, this select is called
repeatedly with decreasing created_at value based on the smallest
value returned by the previous query.
-- this query is generated by my ORM and the ORM was programmed to
always return nulls last when working with pagination, so it builds a
query similar to this one:
SELECT * FROM d WHERE created_at < $0 ORDER BY created_at DESC NULLS
LAST LIMIT 10;
```
> I'm not really excited about poking holes in the PathKey concept to
> make this work the way you want. I think the odds of introducing bugs
> would be high.
Do you have anything in mind that would be acceptable or safe?
Unless you feel like the risk outweighs the benefit here, I do think
that this edge case could catch other people off guard, especially
users that interact with the database using some sort of ORM.
Thanks again,
Mayrom Rabinovich
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shveta malik | 2026-02-10 09:33:43 | Re: Improve pg_sync_replication_slots() to wait for primary to advance |
| Previous Message | Jakub Wartak | 2026-02-10 09:26:22 | Re: Problems with get_actual_variable_range's VISITED_PAGES_LIMIT |