Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

pg_order_pushdown gives different results

Closed
#598 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
43/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
cpp, postgresql

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from duckdb/duckdb-postgres

All issues in duckdb/duckdb-postgres

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.