pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Index `NULLS FIRST` / `NULLS LAST` ordering is dropped by `dump` and ignored by `plan`

Open rosssaunders-fd opened this issue 3 months ago • 0 comments

pgschema does not carry a btree index's null-ordering clause. It is lost in two places, and the second is the serious one:

  1. dump omits NULLS FIRST / NULLS LAST from CREATE INDEX, so a dump → reload round-trip silently produces an index with different ordering semantics to the one it was taken from.
  2. plan does not consider the clause when diffing, so it reports No changes detected between a database whose index is DESC (i.e. nulls first) and a desired state that declares DESC NULLS LAST. There is no way to converge the database to the declared state.

Because DESC defaults to NULLS FIRST and ASC defaults to NULLS LAST, dropping the clause is not a no-op whenever it was written non-defaulted — it inverts it.

Verified on pgplex/pgschema:1.11.1 and pgplex/pgschema:1.12.4 (latest) against PostgreSQL 18.4. Output is byte-identical between the two versions.

Repo Steps

create schema app;
create table app.events (
  id bigint generated always as identity primary key,
  occurred_at timestamptz,
  created_at timestamptz not null default now(),
  payload text
);

-- expression index, DESC NULLS LAST, with a DESC tiebreaker
create index idx_events_expr on app.events
  ((coalesce(occurred_at, created_at)) desc nulls last, id desc);

-- control: plain column, DESC NULLS LAST
create index idx_events_desc_nulls_last on app.events (occurred_at desc nulls last);

-- control: plain column, ASC NULLS FIRST
create index idx_events_asc_nulls_first on app.events (occurred_at asc nulls first);

Part 1 — dump drops the clause

pgschema dump --host ... --db probe --user probe --schema app --multi-file --file /tmp/out/app.sql

What PostgreSQL reports (pg_indexes.indexdef):

CREATE INDEX idx_events_asc_nulls_first ON app.events USING btree (occurred_at NULLS FIRST)
CREATE INDEX idx_events_desc_nulls_last ON app.events USING btree (occurred_at DESC NULLS LAST)
CREATE INDEX idx_events_expr ON app.events USING btree (COALESCE(occurred_at, created_at) DESC NULLS LAST, id DESC)

What pgschema dump emits — identical on 1.11.1 and 1.12.4:

CREATE INDEX IF NOT EXISTS idx_events_asc_nulls_first ON events (occurred_at);
CREATE INDEX IF NOT EXISTS idx_events_desc_nulls_last ON events (occurred_at DESC);
CREATE INDEX IF NOT EXISTS idx_events_expr ON events (COALESCE(occurred_at, created_at) DESC, id DESC);

Effective meaning after reloading that dump:

declared dumped as reloads as correct?
occurred_at NULLS FIRST (occurred_at) ASC NULLS LAST ✗ inverted
occurred_at DESC NULLS LAST (occurred_at DESC) DESC NULLS FIRST ✗ inverted
COALESCE(...) DESC NULLS LAST, id DESC (COALESCE(...) DESC, id DESC) DESC NULLS FIRST ✗ inverted

This is not specific to expression indexes — plain column indexes lose it too.

rosssaunders-fd avatar Aug 28 '26 05:08 rosssaunders-fd