Skip to content

fix(pg-delta): recreate rules and triggers for replacement dependencies#285

Open
xmh1011 wants to merge 49 commits into
supabase:mainfrom
xmh1011:fix/pg-delta-issue-263-rule-trigger-cascade
Open

fix(pg-delta): recreate rules and triggers for replacement dependencies#285
xmh1011 wants to merge 49 commits into
supabase:mainfrom
xmh1011:fix/pg-delta-issue-263-rule-trigger-cascade

Conversation

@xmh1011

@xmh1011 xmh1011 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Close #263.

This is a follow-up for the rule/trigger cases found during the #263 dependency-replacement audit. It is intentionally separate from #275, which is scoped to RLS policies depending on rewritten columns.

Root cause

pg_depend already exposes both dependent object kinds:

  • rule:<schema>.<table>.<rule> for rewrite rules
  • trigger:<schema>.<table>.<trigger> for triggers, including WHEN expression dependencies

The gap was in expandReplaceDependencies: it could walk from a replaced function or an invalidated column to those raw dependents, but it could not resolve rule:* / trigger:* stable ids into catalog objects or synthesize replacement changes for them. The original diff therefore left ReplaceRule / ReplaceTrigger changes as CREATE OR REPLACE ..., which runs too late to unblock DROP FUNCTION or ALTER TABLE ... ALTER COLUMN ... TYPE.

Fix

  • Use invalidated stable ids as additional replacement roots so in-place column rewrites participate in dependency expansion.
  • Resolve surviving rule:* and trigger:* dependents from the main and branch catalogs.
  • Promote those dependents to explicit DROP + CREATE replacement changes.
  • Remove superseded same-object ReplaceRule / ReplaceTrigger changes.
  • Replay rule comments, rule enabled state, and trigger comments after recreation.

RED evidence

Before the fix:

  • Unit coverage showed expandReplaceDependencies did not add DropRule for dependent rules reached from a procedure signature replacement or an invalidated column.
  • PG17 rule + column rewrite failed with PostgreSQL 0A000: cannot alter type of a column used by a view or rule.
  • PG17 trigger WHEN + column rewrite failed with PostgreSQL 0A000: cannot alter type of a column used in a trigger definition.
  • PG17 rule/trigger WHEN + function signature replacement failed with PostgreSQL 2BP01 because the old function still had surviving rule/trigger dependents.

Validation

On this PR branch:

PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 \
PGDELTA_TEST_POSTGRES_VERSIONS=17 \
PGDELTA_CONNECTION_TIMEOUT_MS=15000 \
PGDELTA_CONNECT_TIMEOUT_MS=15000 \
  ./node_modules/.bin/bun run --cwd packages/pg-delta test \
  --max-concurrency 1 \
  --timeout 120000 \
  src/core/expand-replace-dependencies.test.ts \
  tests/integration/rule-operations.test.ts \
  tests/integration/trigger-operations.test.ts

Result: 39 pass / 0 fail.

./node_modules/.bin/bun run format-and-lint:fix && \
./node_modules/.bin/bun run check-types && \
./node_modules/.bin/bun run knip --fix

Result: passed. knip only reported existing configuration hints.

I also validated this together with the other local issue-audit branches on a combined branch:

  • PG17 pg-delta full local run: 1624 pass / 0 fail, 24 skip, 1 todo.
  • pg-topo full test suite: 159 pass / 0 fail.

Local integration runs used PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1, so security-label coverage remains for CI. Testcontainers left by local validation were removed after the run.

@changeset-bot

changeset-bot Bot commented Jun 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: bc2ace3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@supabase/pg-delta Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e57e706451

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b3c266759a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7ce8b97a6e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e5d6b234fe

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d33cf9d0bc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
@pkg-pr-new

pkg-pr-new Bot commented Jun 10, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/supabase/pg-toolbelt/@supabase/pg-delta@285
npm i https://pkg.pr.new/supabase/pg-toolbelt/@supabase/pg-topo@285

commit: d33cf9d

@xmh1011
xmh1011 force-pushed the fix/pg-delta-issue-263-rule-trigger-cascade branch from d33cf9d to 2f2100b Compare June 10, 2026 19:14

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0ccb4a1c91

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4fd4a87d42

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0bd3143c47

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/objects/table/changes/table.alter.ts Outdated
Comment thread packages/pg-delta/src/core/objects/table/changes/table.alter.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
@xmh1011
xmh1011 force-pushed the fix/pg-delta-issue-263-rule-trigger-cascade branch from 5c6d36e to a550a34 Compare June 11, 2026 21:55

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

get drops() {
// Treat ALTER PUBLICATION ... DROP TABLE as a destructive change so it runs
// in the drop phase before DROP TABLE removes the referenced relation.
return this.tables.map((table) => stableId.table(table.schema, table.name));
}

P2 Badge Do not report publication table membership as table drops

When a migration only removes a table from a publication, this getter adds the table stable id to the global droppedIds set; expandReplaceDependencies then treats any table:* drop-only id as a replacement root and walks/rebuilds dependents of that table even though the table itself survives. For a published table with dependent views/rules/etc., a simple ALTER PUBLICATION ... DROP TABLE can therefore synthesize unrelated drop/create DDL and risk losing dependent metadata instead of only changing publication membership.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
@xmh1011
xmh1011 force-pushed the fix/pg-delta-issue-263-rule-trigger-cascade branch from a550a34 to 31f8ec4 Compare June 12, 2026 15:59

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 31f8ec43a9

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
@xmh1011
xmh1011 force-pushed the fix/pg-delta-issue-263-rule-trigger-cascade branch from 31f8ec4 to 52acea9 Compare June 12, 2026 16:43

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8368591e80

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/objects/table/changes/table.alter.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09ec2406cb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/objects/table/table.diff.ts
Comment thread packages/pg-delta/src/core/sort/graph-builder.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 710f4397ec

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/objects/table/table.diff.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
@xmh1011

xmh1011 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

Final #263 audit update after the latest #285 review fixes.

Current branch state:

I did another focused deep pass over the #263 dependency-replacement family after adding the latest fixes in #285. I did not find another unresolved case in the tested replacement/invalidation classes.

Coverage now includes:

  • original policy/view/function replacement classes covered by the earlier merged work;
  • rules and triggers depending on rewritten columns or replaced function signatures;
  • SQL routines rebuilt from column invalidations;
  • aggregates depending on rebuilt SQL transition functions;
  • constraints depending on routines rebuilt from column invalidation;
  • PG17 constrained generated-column type changes;
  • generated-column metadata, dependent indexes, cross-table FKs, publications, materialized views, routine metadata, aggregate metadata, and rebuilt index comments.

Fresh verification on the latest #285 tree:

cd packages/pg-delta
PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 \
PGDELTA_TEST_POSTGRES_VERSIONS=17 \
TESTCONTAINERS_RYUK_DISABLED=true \
../../node_modules/.bin/bun run test --max-concurrency 1 --timeout 120000 \
  src/core/expand-replace-dependencies.test.ts \
  src/core/sort/sort-changes.test.ts \
  src/core/objects/index/changes/index.create.test.ts \
  src/core/objects/index/index.diff.test.ts \
  src/core/objects/table/table.diff.test.ts \
  tests/integration/alter-table-operations.test.ts \
  tests/integration/policy-dependencies.test.ts \
  tests/integration/rule-operations.test.ts \
  tests/integration/trigger-operations.test.ts \
  tests/integration/function-operations.test.ts \
  tests/integration/index-operations.test.ts \
  tests/integration/publication-operations.test.ts \
  tests/integration/materialized-view-operations.test.ts \
  tests/integration/aggregate-operations.test.ts \
  tests/integration/table-function-dependency-ordering.test.ts \
  tests/integration/table-operations.test.ts

Result: 228 pass, 2 skip, 0 fail, 13 snapshots, 1154 expect() calls, across 16 files.

The two skips are the pre-PG17 generated-column-type tests skipped under the PG17-only matrix. There were transient testcontainer connection retries during one alter-table case, but the retry passed and the final suite result was clean.

Based on this pass, I think #263 is covered by the merged PRs plus #285, and can be considered fixed once #285 merges.

@xmh1011
xmh1011 force-pushed the fix/pg-delta-issue-263-rule-trigger-cascade branch from 8ddf3ce to c6f805b Compare June 13, 2026 04:32

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c6f805bada

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/objects/table/table.diff.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8f397f84d4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 775d6af129

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/objects/table/changes/table.alter.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 701790af3a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
@xmh1011

xmh1011 commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@avallete @supabase/dev-workflows gentle ping for a code review pass.

All current review feedback on this PR has been addressed, and I rechecked the review-thread state today: 0 unresolved threads. The follow-up validation for the #263 coverage is posted above: focused pg-delta coverage across 16 files finished with 228 pass, 2 skip, 0 fail.

Could you please review when you have a chance? Once this merges, I think #263 can be considered covered by the merged dependency-replacement work plus this PR.

xmh1011 added 3 commits July 1, 2026 20:44
RED evidence:

Unit: expandReplaceDependencies did not add DropRule for dependent rules when a procedure signature changed or a column was invalidated.

Integration PG17: rule rewrite failed with PostgreSQL 0A000 cannot alter type of a column used by a view or rule.

Integration PG17: trigger rewrite failed with PostgreSQL 0A000 cannot alter type of a column used in a trigger definition.

Signed-off-by: xiaominghao <xiaominghao@baidu.com>
Promote rule and trigger dependents from pg_depend when replacement roots or invalidated objects require dependent rewrite objects to be removed before the parent change.

GREEN evidence:

Unit: expand-replace-dependencies.test.ts passed 10/10.

Integration PG17: rule and trigger column rewrite regressions passed.

Integration PG17: rule and trigger function signature regressions passed.

format-and-lint:fix, check-types, and knip --fix exited 0.
Signed-off-by: xiaominghao <xiaominghao@baidu.com>
Signed-off-by: xiaominghao <xiaominghao@baidu.com>
xmh1011 added 10 commits July 1, 2026 20:44
RED evidence:\n- expandReplaceDependencies > promotes aggregate dependents when a column invalidation rebuilds a SQL routine failed because no DropAggregate was produced.\n- expandReplaceDependencies > drops constraints that depend on routines rebuilt from column invalidation failed because no AlterTableDropConstraint was produced.\n- table.diff > postgres 17 rebuilds constrained generated columns when their type changes failed because no AlterTableDropColumn was produced.\n- PG17 integration generated ALTER COLUMN SET EXPRESSION / ALTER COLUMN TYPE for a constrained generated column instead of DROP COLUMN / ADD COLUMN; aggregate and constraint routine dependency tests missed the expected drops.
GREEN evidence:\n- cd packages/pg-delta && ../../node_modules/.bin/bun run test src/core/expand-replace-dependencies.test.ts src/core/objects/table/table.diff.test.ts --test-name-pattern 'promotes aggregate dependents|drops constraints that depend on routines|postgres 17 rebuilds constrained generated' -> 3 pass, 0 fail.\n- cd packages/pg-delta && PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 PGDELTA_TEST_POSTGRES_VERSIONS=17 TESTCONTAINERS_RYUK_DISABLED=true ../../node_modules/.bin/bun run test --timeout 120000 tests/integration/table-function-dependency-ordering.test.ts tests/integration/table-operations.test.ts --test-name-pattern 'aggregate depending on invalidated SQL function|constraint depending on invalidated SQL function|postgres 17 generated column type change' -> 3 pass, 0 fail.\n- Broader focused unit suite -> 63 pass, 0 fail.\n- Broader PG17 focused integration suite -> 7 pass, 0 fail.\n- ./node_modules/.bin/bun run format-and-lint:fix && ./node_modules/.bin/bun run check-types && ./node_modules/.bin/bun run knip --fix && git diff --check -> pass (knip config hints only).
RED evidence before the fix:

- Unit: expand-replace-dependencies.test.ts failed because view add-drop-only expansion did not emit AlterViewChangeOwner and index rebuild expansion did not emit AlterIndexSetStatistics.

- Integration PG17: alter-table-operations.test.ts still emitted SET EXPRESSION AS (NULL::character varying(64)) / ALTER COLUMN TYPE / SET EXPRESSION for a NOT NULL generated column own type change instead of DROP COLUMN / ADD COLUMN.
Replay index statistics and view metadata when column invalidation expansion synthesizes dependent rebuilds, and rebuild PostgreSQL 17 generated columns when NOT NULL constraints make SET EXPRESSION unsafe.

GREEN validation:

- ../../node_modules/.bin/bun run test src/core/expand-replace-dependencies.test.ts --test-name-pattern 'replays (index statistics|view metadata)' -> 2 pass / 0 fail.

- PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 PGDELTA_TEST_POSTGRES_VERSIONS=17 TESTCONTAINERS_RYUK_DISABLED=true ../../node_modules/.bin/bun run test tests/integration/alter-table-operations.test.ts --test-name-pattern 'postgres 17 rebuilds not-null generated column for own type changes' -> 1 pass / 0 fail.

- ../../node_modules/.bin/bun run test src/core/expand-replace-dependencies.test.ts -> 31 pass / 0 fail.

- ./node_modules/.bin/bun run format-and-lint:fix && ./node_modules/.bin/bun run check-types && ./node_modules/.bin/bun run knip --fix && git diff --check -> exit 0; knip reported existing configuration hints only.
RED summaries:

- generated-column rebuild left AlterTableAlterColumnType for status_label: expected false, received true.

- matview index rebuild omitted AlterIndexSetStatistics: expected true, received false.

- FK-backed standalone index rebuild produced no dependent FK drop/add: dropConstraintIdx was -1.
GREEN verification:

- focused expandReplaceDependencies RED tests now pass.

- PG17 table integration passes generated-column superseded type alter and FK-backed index replacement cases.

- PG17 materialized-view integration passes index statistics replay coverage.
Signed-off-by: xiaominghao <xiaominghao@baidu.com>
Generated-column reset now casts the temporary NULL expression to the current column type, so PostgreSQL 17 incompatible generated own-type changes no longer emit NULL::new_type before ALTER COLUMN TYPE.

Generated-column and standalone index rebuild expansion now reapplies REPLICA IDENTITY USING INDEX without duplicating an existing setter, and constraint rebuilds replay backing index statistics after the constraint recreates its owned index.

RED evidence:

- table.diff incompatible generated own-type regression failed with NULL::text instead of NULL::integer.

- generated-column rebuild replica identity regression failed with replicaIdentityIdx = -1 after CreateIndex.

- constraint-owned backing index statistics regression failed with statisticsIdx = -1 after AddConstraint.

- standalone index rebuild replica identity regression failed with 0 AlterTableSetReplicaIdentity changes.

GREEN validation:

- table.diff/table.alter focused generated + replica identity matrix: 8 pass / 0 fail.

- expand-replace-dependencies.test.ts: 35 pass / 0 fail.

- check-types, format-and-lint:fix, knip --fix, and git diff --check exited 0; knip reported existing config hints only.

Signed-off-by: xiaominghao <xiaominghao@baidu.com>
RED evidence:

- missing branch-table regression fails with TypeError: undefined is not an object (evaluating 'branchTable.schema').

- constraint-owned replica identity regression fails with Expected length: 1 / Received length: 0 for AlterTableSetReplicaIdentity.

Signed-off-by: xiaominghao <xiaominghao@baidu.com>
Skip constraint backing-index metadata replay when the branch table or branch constraint no longer exists, avoiding the missing branch-table dereference during drop-only constraint expansion.

Also reapply REPLICA IDENTITY USING INDEX after constraint-owned backing indexes are recreated, reusing the existing replacement metadata de-duplication path so pre-existing setters are not duplicated.

GREEN validation:

- PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 PGDELTA_TEST_POSTGRES_VERSIONS=17 TESTCONTAINERS_RYUK_DISABLED=true ../../node_modules/.bin/bun run test src/core/expand-replace-dependencies.test.ts --test-name-pattern 'keeps drop-only constraint dependents|reapplies replica identity when constraint rebuilds' -> 2 pass / 0 fail.

- PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 PGDELTA_TEST_POSTGRES_VERSIONS=17 TESTCONTAINERS_RYUK_DISABLED=true ../../node_modules/.bin/bun run test src/core/expand-replace-dependencies.test.ts -> 37 pass / 0 fail.

- ./node_modules/.bin/bun run format-and-lint:fix && ./node_modules/.bin/bun run check-types && ./node_modules/.bin/bun run knip --fix && git diff --check -> pass; knip reported existing configuration hints only.

Signed-off-by: xiaominghao <xiaominghao@baidu.com>
@xmh1011
xmh1011 force-pushed the fix/pg-delta-issue-263-rule-trigger-cascade branch from c23e7d1 to a055809 Compare July 1, 2026 12:45

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a055809995

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/objects/table/changes/table.alter.ts
xmh1011 added 2 commits July 1, 2026 22:09
RED evidence:\n- table.alter unit run failed because AlterTableSetCluster is not exported.\n- expand replacement focused unit run failed: quoted stable ids did not emit generated-column/publication rebuilds; clustered index replay was missing (clusterIdx -1 after CreateIndex).\n- PG17 generated-column integration failed because resetGenerated index 0 was not greater than dropIndex 1, proving SET EXPRESSION ran before DROP INDEX.
Preserve CLUSTER markers when replacement rebuilds clustered indexes, parse quoted table stable IDs without splitting inside quoted identifiers, and invalidate generated-column expression resets so dependent indexes drop before the reset.

Generated column type rewrites also omit PostgreSQL's forbidden USING clause, and explicit same-id invalidation requirements no longer create cycles between same-column in-place ALTERs.

RED evidence:

- table.alter serializer regression failed because generated column type rewrite emitted USING computed_name::text.

- sort-changes regression failed with AlterTableAlterColumnDropDefault <-> AlterTableAlterColumnType explicit column cycle.

- alter-table integration failed before the fix because generated-column index reset ran before DropIndex, then exposed PostgreSQL 42611 for USING on generated columns.

Validation:

- bun test touched pg-delta unit files from /tmp: table.alter, table.diff, sort-changes, expand-replace-dependencies

- PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 PGDELTA_TEST_POSTGRES_VERSIONS=17 TESTCONTAINERS_RYUK_DISABLED=true bun run test --max-concurrency 1 --timeout 120000 tests/integration/alter-table-operations.test.ts

- PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 PGDELTA_TEST_POSTGRES_VERSIONS=17 TESTCONTAINERS_RYUK_DISABLED=true bun run test --max-concurrency 1 --timeout 120000 tests/integration/index-operations.test.ts

- bun run format-and-lint:fix

- bun run check-types

- bun run knip --fix

- git diff --check

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 00023b2da0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
xmh1011 added 2 commits July 1, 2026 22:40
RED evidence:

- expand-replace-dependencies focused test failed with clusterIdx -1: Expected > 3 / Received -1 because no AlterMaterializedViewSetCluster was emitted.

- materialized-view alter focused test failed with missing export: AlterMaterializedViewSetCluster not found.
Adds ALTER MATERIALIZED VIEW ... CLUSTER ON support and replays clustered markers for standalone materialized-view indexes recreated during dependency expansion.

Validation:

- Unit: expand-replace-dependencies.test.ts + materialized-view.alter.test.ts, 43 pass / 0 fail.

- Integration: PG17 restore materialized view index when replacing for column type rewrite, 1 pass / 0 fail.

- format-and-lint:fix, check-types, knip --fix, and git diff --check passed; knip only reported existing configuration hints.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

https://github.com/supabase/pg-toolbelt/blob/20e3b53f4e9ef26fc46e3595d4b5d1bac6155f73//tmp/pg-toolbelt-review-1466/packages/pg-delta/src/core/objects/table/table.diff.ts#L793-L800
P2 Badge Rebuild generated columns that cannot be assignment-cast

For a PostgreSQL 17 generated column that is nullable and has no constraints, this guard leaves the reset + ALTER COLUMN ... TYPE path in place. The serializer then intentionally omits USING for generated columns, so conversions without an implicit/assignment cast from the old stored type to the new one (for example an integer GENERATED ... column becoming uuid GENERATED ...) still fail during the type alter before the final expression is restored; those cases need to take the drop/add rebuild path instead. Fresh evidence beyond the earlier reset-cast thread: the current guard only checks nullability/constraints and never accounts for no-assignment-cast type changes.


https://github.com/supabase/pg-toolbelt/blob/20e3b53f4e9ef26fc46e3595d4b5d1bac6155f73//tmp/pg-toolbelt-review-1466/packages/pg-delta/src/core/expand-replace-dependencies.ts#L1397
P2 Badge Seed aggregate replacements as dependency roots

Adding aggregate: here only makes aggregates rebuildable when reached from an invalidated column/routine, but an aggregate whose own argument signature changes gets different old/new stable IDs and is never added to replaceRoots (there is no aggregate name match like procedures, and the drop-only seed omits aggregates). If a surviving view/rule is updated to stop using the old aggregate signature, the plan still runs DROP AGGREGATE in the drop phase before that rewrite object is dropped/recreated, so PostgreSQL rejects the drop due to the old pg_depend edge.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

xmh1011 added 2 commits July 2, 2026 14:56
RED evidence: focused regressions failed before the fix. table.diff expected generated-column drop/add but received reset plus ALTER COLUMN status_id TYPE uuid; expandReplaceDependencies expected a promoted DropView/CreateView for an aggregate signature change but dropViewIdx was -1.
Generated-column type changes on PostgreSQL 17+ now rebuild when the new stored type has no implicit or assignment cast from the old type. Aggregate signature replacements are seeded as replacement roots so surviving rewrite objects are dropped before the old signature is dropped.\n\nGREEN evidence: related unit files passed with 69 pass / 0 fail; PG17 focused integration for generated-column no-cast and aggregate-signature replacement passed with 2 pass / 0 fail. Static checks passed: format-and-lint:fix, check-types, knip --fix, and git diff --check.
@xmh1011

xmh1011 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the latest Codex Review in 613a2ed4 and e3d34158.

Changes:

  • PG17 generated columns now take the drop/add rebuild path when the target stored type has no implicit or assignment cast from the previous stored type. The table extractor carries type OIDs plus target assignment-cast source OIDs, and that derived metadata is excluded from equality normalization.
  • Aggregate signature changes now seed the old aggregate stable id as a replacement root when the same schema/name is recreated with a different signature, so dependent views/rules are dropped before DROP AGGREGATE and recreated after the new aggregate.

Regression coverage:

  • Unit: postgres 17 rebuilds generated columns when type change lacks an assignment cast
  • Unit: promotes aggregate dependents when an aggregate signature changes
  • Integration: PG17 generated-column integer -> uuid no-cast roundtrip
  • Integration: aggregate signature replacement with a dependent view

Fresh verification:

./node_modules/.bin/bun test 
  /Users/xiaominghao/.config/superpowers/worktrees/pg-toolbelt/pr285-sync-main-20260701/packages/pg-delta/src/core/objects/table/table.diff.test.ts 
  /Users/xiaominghao/.config/superpowers/worktrees/pg-toolbelt/pr285-sync-main-20260701/packages/pg-delta/src/core/expand-replace-dependencies.test.ts
# 69 pass / 0 fail

cd packages/pg-delta
PGDELTA_SKIP_DUMMY_SECLABEL_BUILD=1 
PGDELTA_TEST_POSTGRES_VERSIONS=17 
TESTCONTAINERS_RYUK_DISABLED=true 
../../node_modules/.bin/bun run test --max-concurrency 1 --timeout 120000 
  tests/integration/alter-table-operations.test.ts 
  tests/integration/aggregate-operations.test.ts 
  --test-name-pattern "target type has no assignment cast|old aggregate signature"
# 2 pass / 0 fail

Static checks also exited 0: bun run format-and-lint:fix, bun run check-types, bun run knip --fix, and git diff --check.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e3d3415859

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/objects/table/table.diff.ts
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts
xmh1011 added 2 commits July 2, 2026 15:42
RED evidence: focused regressions failed before the fix. generated domain column changes produced NULL reset plus ALTER COLUMN TYPE instead of drop/add; domain CHECK and domain DEFAULT dependents produced zero AlterDomain drop/restore changes; the sorter did not place AlterDomainDropDefault before DropProcedure.
Domain CHECK constraints and defaults that depend on routines rebuilt from column invalidations are now released and restored around the routine replacement. Domain default drops are routed through the drop phase so pg_depend orders them before routine drops. PostgreSQL 17+ generated columns now rebuild when either side uses a domain type that could reject the temporary NULL reset path.\n\nGREEN evidence: related unit files passed with 79 pass / 0 fail; PG17 focused integration for generated no-cast, aggregate signature replacement, generated domain type, and domain CHECK/default dependency release passed with 4 pass / 0 fail. Static checks passed: format-and-lint:fix, check-types, knip --fix, and git diff --check.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 96d7c6c7ae

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
xmh1011 added 2 commits July 2, 2026 16:30
RED evidence before the fix:

- generated regular-to-generated rebuild metadata expected true, received false

- routine signature column default drop expected 1, received 0

- routine signature domain default drop expected 1, received 0

- routine signature publication table release expected 1, received 0
Release column and domain defaults reached from routine signature replacement roots before dropping the old routine, and release publication table filters when the publication dependency points at a routine rather than a column.

Also restore generated-column dependent metadata for regular-to-generated rebuilds where the old column had no default.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e29cf2a889

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/pg-delta/src/core/expand-replace-dependencies.ts Outdated
xmh1011 added 2 commits July 2, 2026 16:54
RED evidence before the fix:

- does not duplicate existing publication table adds when releasing routine filters failed

- audit_accounts ADD count expected 1, received 2
Filter publication release tables per existing ADD/DROP membership changes so expansion only emits missing publication table changes instead of overlapping existing diff output.
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.

pg-delta: missing dependency chain resolution for ALTER COLUMN TYPE and DROP FUNCTION with Policy/View dependencies

1 participant