Skip to content

fix: sort by ORDER BY expressions across shards - #1900

Merged
jkaczman merged 1 commit into
mainfrom
jk-order-by-expressions
Oct 27, 2026
Merged

jkaczman merged 1 commit into
mainfrom
jk-order-by-expressions

Conversation

@jkaczman

Copy link
Copy Markdown
Contributor

Previously, when a query ran on several shards, we only sorted the rows we got back from the shards by certain ORDER BY keys [columns, positions, vector distances]

If a different kind of key, one that wasn't a [column, position, vector distance], was present in ORDER BY, e.g., CASE ... END, we ignored it, even if the query also selected it, e.g., SELECT id, CASE ... END AS prio ... ORDER BY CASE ... END.

Now, we correctly handle such a sort key by adding it to the SELECT list as a helper column, sorting by it, and dropping it before sending the rows back

This also fixes two cases where we previously returned rows in the wrong order. Now, an explicit error is returned instead:

  • If the expression has an aggregate in it, e.g., ORDER BY count(*).
  • If the query has DISTINCT and sorts by an expression.

Fixes #1736

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.77778% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...og/src/frontend/client/query_engine/route_query.rs 90.90% 1 Missing ⚠️
pgdog/src/frontend/router/parser/query/select.rs 83.33% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

ProjectPgDog
Branchjk-order-by-expressions
TestbedIntel v1

⚠️ WARNING: Truncated view!

The full continuous benchmarking report exceeds the maximum length allowed on this platform.

🚨 2 Alerts

🐰 View full continuous benchmarking report in Bencher

@github-actions

Copy link
Copy Markdown
Contributor

Metal bench failed. 1 of 150 measures outside bounds.

suite throughput cpu/txn user cycles/txn user instr/txn kernel cycles/txn ctx/txn
pooler 28.52 (+1.1% ± 0.9%) 29,944 (+1.5% ± 1.1%) 35,339 (-0.0% ± 0.03%) 38,369 (+1.2% ± 1.8%)
lb 29.47 (+0.2% ± 0.8%) 31,288 (-0.4% ± 1.0%) 36,205 (+0.0% ± 0.03%) 39,307 (+1.3% ± 1.5%)
sharding-w2 53.28 (+1.8%) 70,656 (+3.0%) 72,624 (+0.0%) 59,731 (+0.3%) 1.398 (-0.3%)
sharding-w8 155,002 (-0.1%) 47.25 (-0.8%) 69,299 (-1.8%) 56,295 (+0.0%) 52,493 (+0.4%) 0.1451 (+4.8%), bound +3%
Counters per txn
suite frontend stall l1i refill backend stall
pooler 16,222 (+3.7% ± 2.2%) 3,502 (+1.9% ± 0.9%) 2,328 (-4.9% ± 1.6%)
lb 17,073 (-0.1% ± 2.0%) 3,586 (+0.6% ± 0.7%) 2,520 (-3.6% ± 2.3%)
sharding-w2 42,192 (+5.7%) 5,665 (+0.9%) 6,103 (-3.5%)
sharding-w8 39,750 (-4.5%) 5,252 (+0.7%) 9,405 (+6.1%)

Microbench: all 135 measures within bounds.

  • extended_cached: 15 of 15 benches faster, median -3.1% ± 0.3%
  • extended_cold: 15 of 15 benches faster, median -2.3% ± 0.3%
  • simple_cold: 15 of 15 benches faster, median -2.8% ± 0.4%
lb flamegraph vs 1ab9634

lb

Open interactive for hover, zoom and search.

lb removed

Open interactive.

microbench flamegraph vs 1ab9634

microbench

Open interactive for hover, zoom and search.

microbench removed

Open interactive.

pooler flamegraph vs 1ab9634

pooler

Open interactive for hover, zoom and search.

pooler removed

Open interactive.

sharding-w2 flamegraph vs 1ab9634

sharding-w2

Open interactive for hover, zoom and search.

sharding-w2 removed

Open interactive.

sharding-w8 flamegraph vs 1ab9634

sharding-w8

Open interactive for hover, zoom and search.

sharding-w8 removed

Open interactive.

Logs

lb, microbench, pooler, sharding-w2, sharding-w8, verdict.json

Compared 22e8c67 with 1ab9634.

@jkaczman
jkaczman merged commit 5beb9bb into main Oct 27, 2026
56 of 58 checks passed
@jkaczman
jkaczman deleted the jk-order-by-expressions branch October 27, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cross-shard ORDER BY ignores expression sort keys

1 participant