pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Online index replacement self-collides for a legal 63-byte index name

Open jlao4394 opened this issue 1 month ago • 0 comments

Summary

A legal 63-byte index name cannot be replaced through the online rewrite. Appending _pgschema_new produces a 76-byte candidate identifier which PostgreSQL truncates back to the original name. Creation skips the original index, the wait accepts it as ready, and the final drop/rename group fails with 42P01.

The final group rolls back and preserves the original index; this report is about replacement failure, not data loss.

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 apply CLI; the plan was not edited.
  • The relevant source is unchanged on current main at 4b3bd0f693b22f7490b114e1c9e34f8670902470. 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

The index name below consists of exactly 63 ASCII i characters.

before.sql:

CREATE SCHEMA registry;
CREATE TABLE registry.entries(id integer PRIMARY KEY,a integer,b integer);
CREATE INDEX iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii ON registry.entries(a);

desired.sql:

CREATE SCHEMA registry;
CREATE TABLE registry.entries(id integer PRIMARY KEY,a integer,b integer);
CREATE INDEX iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii ON registry.entries(b);
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 candidate is iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii_pgschema_new (76 bytes). PostgreSQL truncates it to the existing canonical name, so IF NOT EXISTS skips the build. The native wait completes immediately. The final implicit-transaction group drops that name and then tries to rename the same truncated name:

DROP INDEX IF EXISTS iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii;
ALTER INDEX iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii_pgschema_new RENAME TO iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii;

Native apply exits 1:

Error: failed to execute concatenated statements in group 3: ERROR: relation "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii" does not exist (SQLSTATE 42P01)

Independent catalog inspection with the query below shows the original index retains its OID, valid/ready flags, and (a) definition after failure:

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;

Expected result and boundary

Replacing a valid maximum-length identifier should use a candidate name that fits PostgreSQL's identifier limit and differs from the canonical name, or reject the plan explicitly before execution. A normal short-name replacement followed by a fresh plan succeeds and converges in the same fixture.

This is specifically the demonstrated 63-byte self-collision. Appending the 13-byte suffix exceeds the limit starting at canonical length 51, but names of 51–62 ASCII bytes can still replace successfully after truncation; this report does not claim that all names over 50 bytes fail.

Related code

Both replacement paths append the suffix directly in internal/plan/rewrite.go. This is distinct from #509: no column or other operation removes the original index before cutover.

🤖 Posted by Codex on jlao4394's behalf after the user requested upstream issue reports.

jlao4394 avatar Oct 27 '26 21:10 jlao4394