pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

pgschema:wait query is not schema-qualified: apply hangs when another schema has an invalid index of the same name

Open akuzminsky opened this issue 1 month ago • 0 comments

Version: v1.13.1 (darwin/arm64), PostgreSQL 18.6 (postgres:18), default embedded plan database

Summary

The pgschema:wait query emitted after CREATE INDEX CONCURRENTLY matches the index by name only, WHERE c.relname = '<index>', in any schema. executeWaitDirective reads the first row and polls until done with no timeout. If another schema holds an invalid index with the same name, as an interrupted or failed concurrent build leaves behind (#598), and its row comes first, apply waits forever, although the index it just built is valid. Schema-per-tenant databases, where every schema has the same index names, are where this bites.

Reproduction

CREATE SCHEMA tenant_a;
CREATE SCHEMA tenant_b;
CREATE TABLE tenant_a.items (sku int);
INSERT INTO tenant_a.items VALUES (1), (1);
CREATE TABLE tenant_b.items (sku int);
INSERT INTO tenant_b.items VALUES (1), (2);

-- fails on the duplicate and leaves an invalid index named items_sku_idx in tenant_a
CREATE UNIQUE INDEX CONCURRENTLY items_sku_idx ON tenant_a.items (sku);
ERROR:  could not create unique index "items_sku_idx"
DETAIL:  Key (sku)=(1) is duplicated.
-- desired.sql, for tenant_b
CREATE TABLE items (sku int);
CREATE INDEX items_sku_idx ON items (sku);

pgschema apply --db r4 --schema tenant_b --file desired.sql --auto-approve, killed after 90 seconds:

Executing group 1/2...
  Executing 1 statements in implicit transaction

Executing group 2/2...
  Waiting: Creating index items_sku_idx
    Progress: 0%

Nothing more was printed. Meanwhile the index in tenant_b had been built and was valid:

 nspname  |    relname    | indisvalid
----------+---------------+------------
 tenant_a | items_sku_idx | f
 tenant_b | items_sku_idx | t

Expected

The wait query limited to the target schema, e.g. WHERE c.relname = 'items_sku_idx' AND c.relnamespace = 'tenant_b'::regnamespace, so apply finishes as soon as tenant_b.items_sku_idx is valid.

akuzminsky avatar Oct 09 '26 23:10 akuzminsky