pg_order_pushdown gives different results
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 43/100
Research direction
Start by tracing how pg_order_pushdown builds the pushed-down ORDER BY and how the PostgreSQL extension maps DuckDB text ordering to PostgreSQL collation behavior. The reproduction uses val with the default en_US.UTF-8 collation and shows that COLLATE "C" matches DuckDB's byte ordering; check existing tests for order pushdown and text sorting. Done means pushed-down ordering and min(val) agree with DuckDB for the covered cases, with tests for the reproduction.
Written by the indexing model from the issue text.
Description
What happens?
DuckDB and and Postgres have different sorting rules, meaning results with and without pg_order_pushdown are different. This happens most obviously with the default postgres en_US.UTF-8 collation, because it differs from DuckDB's byte-order sorting (aka C collation).
To Reproduce
In psql,
CREATE TABLE tbl (val) AS VALUES ('apple'), ('Banana'), ('cherry');
Then in DuckDB:
DuckDB v2.0.0-dev87259 (Development Version, 6b6099a3e9)
memory D ATTACH '' AS pg (TYPE postgres);
memory D SET pg_use_ctid_scan = false; -- this is not related to #596
memory D SET pg_debug_show_queries = true;
memory D set pg_order_pushdown = false;
memory D SELECT val FROM pg.public.tbl ORDER BY val;
COPY (SELECT "val" FROM "public"."tbl" ) TO STDOUT (FORMAT "binary");
┌─────────┐
│ val │
│ varchar │
├─────────┤
│ Banana │
│ apple │
│ cherry │
└─────────┘
memory D set pg_order_pushdown = true;
memory D SELECT val FROM pg.public.tbl ORDER BY val;
COPY (SELECT "val" FROM "public"."tbl" ORDER BY "val" IS NULL, "val" ASC NULLS LAST) TO STDOUT (FORMAT "binary");
┌─────────┐
│ val │
│ varchar │
├─────────┤
│ apple │
│ Banana │
│ cherry │
└─────────┘
The same query gives a different ordering. Also, min(val) gives a different result than ORDER BY val LIMIT 1.
To make the postgres sort match DuckDB for text columns, the query needs COLLATE "C":
psql:
# SELECT "val" FROM "public"."tbl" ORDER BY "val" IS NULL, "val" COLLATE "C" ASC NULLS LAST;
val
--------
Banana
apple
cherry
Presumably the point of ORDER BY pushdown is to be able to use an index on "val", so it's important for users to create that in the "C" collation or it won't be used.
I think there's many more instances of this that I haven't checked (xml, arrays, bpchar, geometry, etc) that may be difficult/impossible to fix unless you just have a rule to not push them down.
OS:
Linux
PostgreSQL Version:
18
DuckDB Version:
8d201d476ce690dac2e33921547c43e4daca587a
DuckDB Client:
cli
Full Name:
Gavin Wahl
Affiliation:
Fusionbox
Have you tried this on the latest main branch?
- I agree
Have you tried the steps to reproduce? Do they include all relevant data and configuration? Does the issue you report still appear there?
- I agree
- Dominant language
- C++
- Stars
- 375
- Forks
- 113
- Avg merge
- 5h 32m
- Merged PRs (30d)
- 36
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from duckdb/duckdb-postgres
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
duckdb/duckdb-postgres#638 ·
Maintainers usually reply within 1 day
-
v2.0: generate_subscripts(list, 1) fails with 'positional argument following named argument' after attaching PostgresPossibly taken @staticlibs claimed this 3 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
duckdb/duckdb-postgres#630 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
duckdb/duckdb-postgres#628 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
duckdb/duckdb-postgres#623 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
duckdb/duckdb-postgres#618 ·
Maintainers usually reply within 1 day
All issues in duckdb/duckdb-postgres
Similar issues
-
[Bug] [Go] Ack returns nil when the server answers with a non-OK statusPossibly taken @leizhiyuan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
apache/rocketmq-clients#1465 ·
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
cp-algorithms/cp-algorithms#1738 ·
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
KhronosGroup/Vulkan-Tutorial#534 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 2 days
-
bug IO
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
MrNeRF/LichtFeld-Studio#3824 ·
Maintainers usually reply within 1 day