Skip to content

fix: preserve INCLUDE columns in CREATE INDEX CONCURRENTLY (#508)#512

Merged
tianzhou merged 2 commits into
mainfrom
fix/issue-508-include-columns-concurrent-index
Jul 8, 2026
Merged

fix: preserve INCLUDE columns in CREATE INDEX CONCURRENTLY (#508)#512
tianzhou merged 2 commits into
mainfrom
fix/issue-508-include-columns-concurrent-index

Conversation

@tianzhou

@tianzhou tianzhou commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #508.

The generateIndexSQL in rewrite.go (used exclusively for CREATE INDEX CONCURRENTLY statements) was a duplicate of the canonical version in index.go but missing the INCLUDE clause. This caused covering indexes to lose their non-key columns when added to existing tables, creating an infinite plan loop.

  • Added INCLUDE columns emission between the key columns and NULLS NOT DISTINCT
  • Fixed operator class vs direction ordering to match the canonical version (operator class before direction, per PostgreSQL syntax)
  • Root cause: two separate generateIndexSQL implementations drifted apart

Test plan

  • New fixture online/issue_508_include_columns_concurrent_index — adds a covering index (a) INCLUDE (b) to an existing table
  • Diff test passes (non-concurrent output preserves INCLUDE)
  • Integration test passes (concurrent output preserves INCLUDE)
  • Full diff test suite passes — no regressions

🤖 Generated with Claude Code

The duplicate generateIndexSQL in rewrite.go (used for concurrent index
creation) was missing the INCLUDE clause entirely, causing covering
indexes to lose their non-key columns. Also fix operator class vs
direction ordering to match the canonical version in index.go.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 8, 2026 02:09
@greptile-apps

greptile-apps Bot commented Jul 8, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes concurrent covering-index generation. The main changes are:

  • Adds INCLUDE columns to the online CREATE INDEX CONCURRENTLY rewrite path.
  • Emits operator classes before index direction to match PostgreSQL syntax.
  • Adds an online regression fixture for (a) INCLUDE (b) indexes.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
internal/plan/rewrite.go Updates concurrent index SQL generation to preserve INCLUDE columns and match the canonical operator-class ordering.
testdata/diff/online/issue_508_include_columns_concurrent_index/diff.sql Adds expected diff output for creating a covering index with INCLUDE columns.
testdata/diff/online/issue_508_include_columns_concurrent_index/new.sql Adds the desired schema fixture containing the covering index.
testdata/diff/online/issue_508_include_columns_concurrent_index/old.sql Adds the baseline schema fixture before the covering index exists.
testdata/diff/online/issue_508_include_columns_concurrent_index/plan.json Adds expected JSON plan output with concurrent covering-index creation.
testdata/diff/online/issue_508_include_columns_concurrent_index/plan.sql Adds expected SQL plan output with the concurrent covering-index statement and wait query.
testdata/diff/online/issue_508_include_columns_concurrent_index/plan.txt Adds expected text plan output for the online covering-index fixture.

Reviews (1): Last reviewed commit: "fix: preserve INCLUDE columns in CREATE ..." | Re-trigger Greptile

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a drift bug in the online-plan rewrite path for CREATE INDEX CONCURRENTLY, ensuring covering indexes keep their INCLUDE columns (preventing repeated “rebuild index” loops in subsequent plans).

Changes:

  • Emit INCLUDE (...) columns in the internal/plan/rewrite.go concurrent index SQL generator.
  • Align index column syntax ordering with PostgreSQL grammar (operator class before sort direction).
  • Add a new online diff fixture (issue_508_include_columns_concurrent_index) validating concurrent index creation preserves INCLUDE.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
testdata/diff/online/issue_508_include_columns_concurrent_index/plan.txt New expected plan output showing CREATE INDEX CONCURRENTLY ... INCLUDE (...) plus wait step.
testdata/diff/online/issue_508_include_columns_concurrent_index/plan.sql New SQL fixture asserting concurrent index creation includes INCLUDE.
testdata/diff/online/issue_508_include_columns_concurrent_index/plan.json New JSON plan fixture verifying the emitted SQL includes INCLUDE.
testdata/diff/online/issue_508_include_columns_concurrent_index/old.sql Fixture “before” state table definition (no index).
testdata/diff/online/issue_508_include_columns_concurrent_index/new.sql Fixture “desired” state adding covering index with INCLUDE.
testdata/diff/online/issue_508_include_columns_concurrent_index/diff.sql Fixture diff output for non-concurrent CREATE INDEX ... INCLUDE (...).
internal/plan/rewrite.go Fix concurrent index SQL generation to preserve INCLUDE columns and correct column clause ordering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Move the INCLUDE columns concurrent index test case from standalone
issue_508_include_columns_concurrent_index fixture into the existing
add_composite_index fixture.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tianzhou tianzhou merged commit e18d9ed into main Jul 8, 2026
1 check passed
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.

INCLUDE columns dropped when adding or rebuilding an index via CREATE INDEX CONCURRENTLY

2 participants