pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Partial unique index always seen as modified

Open alecthomas opened this issue 11 months ago • 1 comments

I added the following index:

CREATE UNIQUE INDEX workstations_name_idx ON workstations(name) WHERE (status <> 'deleted'::workstation_status);

Which applied fine the first time, but now every time I run pgschema it detects the index as modified and re-applies it:

~/dev/blox $ just db local apply
PGPASSWORD="$DB_PASSWORD" pgschema apply --host "$DB_HOST" --port "$DB_PORT" --user "$DB_USER" --db "$DB_NAME" --file schema/schema.sql "$@"
Plan: 1 to modify.

Summary by type:
  tables: 1 to modify

Tables:
  ~ workstations
    ~ workstations_name_idx (index - concurrent rebuild)

DDL to be executed:
--------------------------------------------------

-- Transaction Group #1
CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS workstations_name_idx_pgschema_new ON workstations (name) WHERE (status <> 'deleted'::public.workstation_status);

-- Transaction Group #2
-- pgschema:wait
SELECT
    COALESCE(i.indisvalid, false) as done,
    CASE
        WHEN p.blocks_total > 0 THEN p.blocks_done * 100 / p.blocks_total
        ELSE 0
    END as progress
FROM pg_class c
LEFT JOIN pg_index i ON c.oid = i.indexrelid
LEFT JOIN pg_stat_progress_create_index p ON c.oid = p.index_relid
WHERE c.relname = 'workstations_name_idx_pgschema_new';

-- Transaction Group #3
DROP INDEX workstations_name_idx;

ALTER INDEX workstations_name_idx_pgschema_new RENAME TO workstations_name_idx;

Do you want to apply these changes? (yes/no): yes

Applying changes...

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

Executing group 2/3...
  Waiting: Creating index workstations_name_idx_pgschema_new
    Completed immediately (< 1s)

Executing group 3/3...
  Executing 2 statements in implicit transaction
Changes applied successfully!

PS. First bug I've hit and it's a minor one, thanks for pgschema, I've been waiting for something like this for years!

alecthomas avatar Dec 17 '25 05:12 alecthomas

Oh I should also add that if I change it to:

CREATE UNIQUE INDEX workstations_name_idx ON workstations(name)

It works exactly as expected.

alecthomas avatar Dec 17 '25 07:12 alecthomas