Skip to content

Fix CREATE INDEX CONCURRENTLY IF NOT EXISTS invalidating existing index#8467

Draft
Copilot wants to merge 8 commits intomainfrom
copilot/fix-invalidated-index-issue
Draft

Fix CREATE INDEX CONCURRENTLY IF NOT EXISTS invalidating existing index#8467
Copilot wants to merge 8 commits intomainfrom
copilot/fix-invalidated-index-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

Description

Fix index invalidation on CREATE INDEX CONCURRENTLY IF NOT EXISTS

Problem

CREATE INDEX CONCURRENTLY IF NOT EXISTS on an existing index invalidates it. PostgreSQL correctly skips creation, but PostprocessIndexStmt unconditionally invalidates the index for all concurrent operations.

Solution

Check index validity before invalidation in PostprocessIndexStmt:

  • Query pg_index.indisvalid when if_not_exists is set
  • Skip invalidation if index was already valid (pre-existed)
  • Proceed normally for newly created indexes

Changes

  • Add catalog/pg_index.h include
  • Add pre-existence check using SearchSysCache1(INDEXRELID, ...)
  • Skip index_set_state_flags(INDEX_DROP_CLEAR_VALID) for pre-existing indexes
  • Add regression test verifying index remains valid
-- Before fix: index becomes invalid
CREATE INDEX CONCURRENTLY test_idx ON table (col);
CREATE INDEX CONCURRENTLY IF NOT EXISTS test_idx ON table (col);
-- Index now invalid ❌

-- After fix: index stays valid
CREATE INDEX CONCURRENTLY test_idx ON table (col);
CREATE INDEX CONCURRENTLY IF NOT EXISTS test_idx ON table (col);
-- Index still valid ✓
Original prompt

This section details on the original issue you should resolve

<issue_title>CREATE INDEX CONCURRENTLY IF NOT EXISTS invalidates index</issue_title>
<issue_description>Citus 13.0.1, Postgres 17.2

Issuing CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_name ... on a table where the index exists returns

relation "ix_name" already exists, skipping

but actually ends up invalidating the index. This happens on both local and distributed tables.

Expected behavior: Index stays valid</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 7 commits February 5, 2026 09:30
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Co-authored-by: emelsimsek <13130350+emelsimsek@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix index invalidation when creating concurrently if not exists Fix CREATE INDEX CONCURRENTLY IF NOT EXISTS invalidating existing index Feb 5, 2026
Copilot AI requested a review from emelsimsek February 5, 2026 09:36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same also happened to me - I wonder why copilot keep posting this file to the PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CREATE INDEX CONCURRENTLY IF NOT EXISTS invalidates index

3 participants