Online index replacement can drop the original and rename another table's index after a candidate-name collision
Summary
An online index replacement generates a predictable _pgschema_new name. If a valid, independently desired index on another table already occupies that name, CREATE INDEX CONCURRENTLY IF NOT EXISTS skips creation, the wait accepts the unrelated index, and cutover drops the intended original index and renames the other table's index. Native apply exits successfully even though the desired schema has not been reached.
This is not an interrupted-build artifact: the conflicting index is a legitimate object included in desired SQL.
Environment and verification
- pgschema v1.13.4, the latest release checked on October 27, 2026.
- PostgreSQL 17.11, Linux, disposable local databases, external planning database.
- The commands below were exercised with the native
pgschema plan/pgschema applyCLI; the plan was not edited. - The relevant source is unchanged on current
mainat4b3bd0f693b22f7490b114e1c9e34f8670902470. This is source verification, not a claim that an unreleased-main binary was executed.
Use two fresh disposable databases and an empty working directory without .pgschemaignore. The examples assume a local PostgreSQL administrator; adjust host, port, and user as needed:
export PGHOST=127.0.0.1 PGPORT=5432 PGUSER=postgres PGSSLMODE=disable
createdb repro_target
createdb repro_plan
Minimal reproduction
before.sql:
CREATE SCHEMA registry;
CREATE TABLE registry.entries(id integer PRIMARY KEY,a integer,b integer);
CREATE INDEX entries_sort_idx ON registry.entries(a);
CREATE TABLE registry.other(c integer);
CREATE INDEX entries_sort_idx_pgschema_new ON registry.other(c);
desired.sql changes only the indexed column on registry.entries; the other table and its index remain desired:
CREATE SCHEMA registry;
CREATE TABLE registry.entries(id integer PRIMARY KEY,a integer,b integer);
CREATE INDEX entries_sort_idx ON registry.entries(b);
CREATE TABLE registry.other(c integer);
CREATE INDEX entries_sort_idx_pgschema_new ON registry.other(c);
psql -X -v ON_ERROR_STOP=1 -d repro_target -f before.sql
pgschema plan --db repro_target --schema registry \
--file desired.sql \
--plan-host "$PGHOST" --plan-port "$PGPORT" --plan-db repro_plan \
--plan-user "$PGUSER" --plan-sslmode disable \
--output-json plan.json --output-sql plan.sql --no-color
pgschema apply --db repro_target --schema registry \
--plan plan.json --auto-approve --no-color
Actual result
The generated plan creates and waits for entries_sort_idx_pgschema_new, then executes:
DROP INDEX IF EXISTS entries_sort_idx;
ALTER INDEX entries_sort_idx_pgschema_new RENAME TO entries_sort_idx;
apply exits 0 and reports Changes applied successfully!.
Inspect before and after with:
SELECT c.oid, c.relname, t.relname AS table_name,
i.indisvalid, i.indisready, pg_get_indexdef(c.oid)
FROM pg_index i
JOIN pg_class c ON c.oid = i.indexrelid
JOIN pg_class t ON t.oid = i.indrelid
WHERE c.relnamespace = 'registry'::regnamespace
ORDER BY c.relname;
Observed identities (OID values vary between runs):
| State | Index | OID | Owning table | Indexed column |
|---|---|---|---|---|
| Before | entries_sort_idx |
16391 | entries |
a |
| Before | entries_sort_idx_pgschema_new |
16395 | other |
c |
| After | entries_sort_idx |
16395 | other |
c |
The original OID 16391 is gone. The unrelated index retains its OID and table but loses its desired name. A fresh plan remains nonempty. No table-row loss is claimed; this reproduction demonstrates removal and misidentification of indexes.
Expected result
A pre-existing unrelated relation must not be accepted as the replacement candidate. Safe rejection before removing the original index is an acceptable minimum; alternatively choose a noncolliding candidate and preserve the unrelated object. A name and indisvalid alone do not establish candidate identity.
Related code and issues checked
The candidate name and unconditional IF NOT EXISTS are generated in internal/plan/rewrite.go. This differs from #641, whose released fix scopes waits to a schema: both colliding indexes here are in the same schema. It also differs from #509, where a preceding column drop had already removed the original index.
🤖 Posted by Codex on jlao4394's behalf after the user requested upstream issue reports.