Skip to content

Schema Diff: ignore defseqrelid to avoid false SERIAL/BIGSERIAL diffs and invalid ALTER SQL (#10236) - #10248

Open
agu2347 wants to merge 1 commit into
pgadmin-org:masterfrom
agu2347:fix-schema-diff-serial-defseqrelid
Open

Schema Diff: ignore defseqrelid to avoid false SERIAL/BIGSERIAL diffs and invalid ALTER SQL (#10236)#10248
agu2347 wants to merge 1 commit into
pgadmin-org:masterfrom
agu2347:fix-schema-diff-serial-defseqrelid

Conversation

@agu2347

@agu2347 agu2347 commented Aug 12, 2026

Copy link
Copy Markdown

Bug

Schema Diff reports a false-positive difference (and generates invalid SQL) for structurally identical SERIAL/BIGSERIAL/SMALLSERIAL columns across two databases.

Fixes #10236

Root cause

get_formatted_columns() reprojects a column as SERIAL/BIGSERIAL/SMALLSERIAL when it owns (pg_depend, deptype='a' -> seqrelid) the same sequence referenced by its own nextval() DEFAULT (pg_attrdef -> defseqrelid; see #9896/#10100/#10101). Both seqrelid and defseqrelid survive into the column dict that Schema Diff compares, but they are raw sequence OIDs -- meaningless to compare across two independently-created databases. Two databases can have byte-for-byte identical BIGSERIAL columns while their owned sequences get assigned completely different OIDs.

SchemaDiffTableCompare.column_keys_to_ignore already excluded seqrelid, but not its sibling defseqrelid. That gap caused two symptoms from the same root cause:

  1. compare() / are_dictionaries_identical() reports a table as different purely because of the benign defseqrelid mismatch, even when every user-visible column property (including cltype) is identical.
  2. compare_target_cols() adds the column to the changed diff list. Downstream SQL generation (columns/sql/*/update.sql) then renders ALTER COLUMN ... TYPE {cltype} for that column -- even though data.cltype == o_data.cltype ('bigserial' on both sides) -- producing the invalid statement reported in the issue: ALTER COLUMN adl_id TYPE bigserial;.

Fix

Add 'defseqrelid' to SchemaDiffTableCompare.column_keys_to_ignore, alongside its existing sibling 'seqrelid'. This fixes both symptoms at the source: the column is no longer considered different at all, so neither the false-positive diff nor the invalid ALTER COLUMN statement is generated.

Testing

Added tests/test_schema_diff_serial_column_ignore.py, mirroring the existing columns/tests/test_serial_detection_unit.py unit-test convention (BaseTestGenerator + mock dict fixtures, no live DB needed for the logic under test):

  • Two BIGSERIAL columns differing only by defseqrelid are not flagged as added/changed.
  • A genuine difference (NOT NULL toggled) on an otherwise-identical BIGSERIAL column is still detected, proving the fix doesn't mask real diffs.

Since standing up the full Postgres-backed regression harness in my sandbox was impractical for this change, I additionally verified the actual patched SchemaDiffTableCompare.compare_target_cols() logic in isolation (loading the real class source directly, bypassing only the unrelated Flask/DB-heavy base-class import) with 6 checks, including a check that reverting the fix reproduces the exact false positive described in the issue (proving this is a genuine regression guard, not a tautological test). All passed.

pycodestyle --config=.pycodestyle (the project's actual CI style gate, confirmed via .github/workflows/check-python-style.yml) is clean on both changed files.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed schema comparisons for serial columns across independently created databases.
    • Prevented differing sequence identifiers from being reported as false differences or generating invalid alteration statements.
    • Genuine column changes, such as NOT NULL differences, continue to be detected.

…pgadmin-org#10236)

get_formatted_columns() reprojects a column as SERIAL/BIGSERIAL/SMALLSERIAL
when it owns (pg_depend, deptype='a' -> seqrelid) the same sequence
referenced by its own nextval() DEFAULT (pg_attrdef -> defseqrelid,
pgadmin-org#9896/pgadmin-org#10100/pgadmin-org#10101). Both seqrelid and defseqrelid survive into the
column dict that Schema Diff compares, but they are raw sequence OIDs
that are meaningless to compare across two independently-created
databases -- two databases can have byte-for-byte identical BIGSERIAL
columns while their owned sequences get assigned completely different
OIDs.

column_keys_to_ignore already excluded seqrelid, but not its sibling
defseqrelid. That gap caused:

  * SchemaDiffTableCompare.compare() / are_dictionaries_identical() to
    report a table as different purely because of a benign OID mismatch,
    even when every user-visible column property (cltype included) was
    identical.
  * compare_target_cols() to add the column to the 'changed' diff list,
    which downstream generates SQL via the column update.sql template.
    Since data.cltype == o_data.cltype ('bigserial' on both sides), this
    produced the invalid statement reported in the issue:
    `ALTER COLUMN adl_id TYPE bigserial;`

Adding 'defseqrelid' to column_keys_to_ignore fixes both symptoms at the
source: the column is no longer considered different at all, so neither
the false-positive diff nor the invalid ALTER COLUMN statement is
generated.

Added a unit test (test_schema_diff_serial_column_ignore.py, mirroring
the existing test_serial_detection_unit.py convention) covering:
  * two BIGSERIAL columns differing only by defseqrelid are not flagged
  * a genuine difference on an otherwise-identical BIGSERIAL column is
    still detected (proves the fix doesn't mask real diffs)

Fixes pgadmin-org#10236
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ef89a3b2-abcf-4716-aeb9-be147fc5f903

📥 Commits

Reviewing files that changed from the base of the PR and between 067f7af and 229177a.

📒 Files selected for processing (2)
  • web/pgadmin/browser/server_groups/servers/databases/schemas/tables/schema_diff_table_utils.py
  • web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_schema_diff_serial_column_ignore.py

Walkthrough

The schema diff now ignores defseqrelid and existing seqrelid values when comparing serial columns. New BIGSERIAL tests verify that sequence OID differences are ignored while genuine column differences remain detected.

Changes

Serial schema-diff handling

Layer / File(s) Summary
Ignore sequence OIDs in column comparison
web/pgadmin/browser/server_groups/servers/databases/schemas/tables/schema_diff_table_utils.py, web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_schema_diff_serial_column_ignore.py
The column comparison ignores defseqrelid alongside seqrelid. BIGSERIAL tests verify ignored sequence OID differences, genuine difference detection, and matched-column consumption.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • pgadmin-org/pgadmin4#10167: Both changes handle SERIAL metadata. This PR updates schema-diff comparison, while the related PR addresses SERIAL detection and script generation.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the Schema Diff fix and the false SERIAL/BIGSERIAL differences and invalid SQL it addresses.
Linked Issues check ✅ Passed The change ignores harmless sequence OID differences and adds tests that preserve detection of genuine column differences, meeting issue #10236.
Out of Scope Changes check ✅ Passed The implementation and tests directly support the linked issue and PR objectives without unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Schema Diff: False positive and invalid SQL generated for BIGSERIAL / SERIAL columns

1 participant