Skip to content

fix: strip same-schema OPERATOR() qualifiers after external plan inspection (#697) - #703

Merged
tianzhou merged 8 commits into
mainfrom
fix/issue-697-operator-qualifier
Nov 8, 2026
Merged

tianzhou merged 8 commits into
mainfrom
fix/issue-697-operator-qualifier

Conversation

@tianzhou

@tianzhou tianzhou commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With --plan-host and target schema public, a view or materialized view that uses an operator owned by an extension installed in public (for example hstore's -> or ?) was reported as modified on every plan. The plan database is inspected with only the temporary schema on search_path, so ruleutils deparses the operator as OPERATOR(public.->), while the target side prints the bare ->. The two definitions never matched, so the view (and everything depending on it) was dropped and recreated on every apply.

The same operators were also rendered inconsistently across contexts on the current side: column defaults, generated expressions and index expressions are deparsed under search_path = pg_catalog, so they came out as OPERATOR(public.->) (defaults) or, after prefix stripping, as OPERATOR(->) (generated columns, indexes), a form PostgreSQL never emits. A dump of a table with an hstore default therefore failed to plan with embedded Postgres at all.

Changes:

  • ir.normalizeOperatorSyntax reduces OPERATOR(<own schema>.op) to the bare operator in every deparsed expression context (views, generated expressions, index expressions and predicates, column defaults including nextval-backed ones), on both the current and desired side. Raw function bodies are untouched: OPERATOR() carries its own precedence there and is compared as written.
  • Operators that duplicate a pg_catalog operator's name and operand types are only reachable schema-qualified, and ruleutils always qualifies them. The inspector records the names whose every overload is shadowed (Metadata.ShadowedOperators) and both normalization paths keep their qualifier. The decision is per name because the normalizer only sees deparsed text; a name with both shadowed and unshadowed overloads (a custom public.->(jsonb, text) next to hstore's ->) is not exempted, so the extension operator, the common case, always compares cleanly.
  • Routine parameter defaults are normalized on the plan side like column defaults, so an operator in a DEFAULT of a function or procedure parameter no longer replans the routine.
  • newSameSchemaQualifierStripper (plan side, after the temp-schema rename) strips OPERATOR(public.op) like it already stripped public.func( and ::public.type, honours the shadowed set, and now also skips double-quoted identifiers.

Fixes #697

Test plan

  • TestNormalizeOperatorSyntax (ir): same-schema operators become bare, shadowed and other-schema operators keep their qualifier, quoted text is untouched, nextval defaults are covered, and StripSchemaPrefixFromBody leaves OPERATOR(public.*) as written.
  • TestNormalizeSchemaNames_StripsSameSchemaOperatorQualifiers (cmd/plan): plan-side stripping with a shadowed name, a string literal and quoted identifiers.
  • TestExternalDatabase_ExtensionOperatorInPublicPlansClean (cmd/plan): hstore operators in a default, generated column, CHECK, index expression and predicate, function parameter default, view (with a quoted "OPERATOR(public.->)" alias) and matview plan as a no-op through an external plan database, with a fully shadowed public.+ and a mixed-overload public.-> present on both databases; asserts the target IR exempts only +. Fails before the fix with the recreate shown in External plan database with target schema public renders extension operators as OPERATOR(public.->), recreating views on every plan #697.
  • Verified manually: the same schema plans clean with both providers, round-trips through apply, and with a custom public.+(integer, integer) the restored copy keeps OPERATOR(public.+) in the default and generated column.
go test ./ir
go test ./cmd/plan -run 'TestNormalizeSchemaNames|TestExternalDatabase_'
go test ./internal/diff

🤖 Generated with Claude Code

…ection (#697)

With an external plan database and target schema public, a view using an
operator owned by an extension installed in public (hstore's ->) was deparsed
as OPERATOR(public.->) on the plan side and as -> on the target side, so the
view was recreated on every plan. The same-schema qualifier stripper now also
removes OPERATOR(schema.op), as it already does for schema.func( and ::schema.type.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 8, 2026 08:43
@greptile-apps

greptile-apps Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change is limited to schema planning comparisons.

Preserve double-quoted identifiers before merging; the current replacement can produce a broken view migration.

Findings

  1. P1 Quoted column names change ▶

Summary

Removes same-schema OPERATOR(...) qualifiers after inspecting the plan database.

  • Plans recognize views that use operators in their own schema.

Reviews (1) · Last reviewed commit: "fix: strip same-schema OPERATOR() qualif..." · Reviewed by Greptile

Comment thread cmd/plan/plan.go Resolved

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.

🟡 Changes recommended

Operator stripping corrupts double-quoted identifiers and inconsistently normalizes defaults and generated expressions, causing failing view replacements, repeated changes, and potentially unnecessary column recreation.

2 open issues
What changed in this PR

This PR addresses #697 by normalizing same-schema operator qualifiers during external planning, preventing unchanged views and materialized views from being reported as modified.

Changes:

  • Strip OPERATOR(schema.op) qualifiers during desired-state normalization.
  • Add unit coverage for operator normalization and preservation of string literals.
  • Add an external-database regression test using hstore views and materialized views.
File Description
cmd/​plan/​plan.go Adds same-schema operator qualifier stripping.
cmd/​plan/​plan_test.go Tests qualified operators, literals, and already-bare operators.
cmd/​plan/​external_db_integration_test.go Checks that unchanged hstore views produce a no-op external plan.

Review effort: Balanced

Comment thread cmd/plan/plan.go Resolved
Comment thread cmd/plan/plan.go Resolved
…iers (#697)

Review follow-up: the plan-side stripper also mangled double-quoted
identifiers, and stripping only the desired side left column defaults,
generated expressions and index expressions (deparsed under
search_path = pg_catalog) disagreeing with the current side.

The IR normalizer now reduces OPERATOR(<own schema>.op) and the OPERATOR(op)
artifact of prefix stripping to the bare operator in every expression
context, on both sides, so a dump with an hstore default also plans with
embedded Postgres. The plan-side stripper skips double-quoted identifiers
the same way it skips string literals.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

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.

🟡 Changes recommended

Operator-wrapper normalization can change precedence and operator resolution, hiding required function and default updates and emitting SQL that uses the wrong operator.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread ir/normalize.go Resolved
Comment thread ir/normalize.go Resolved
Comment thread ir/normalize.go Resolved
… bodies alone (#697)

Review follow-up. The operator rewrite moves out of StripSchemaPrefixFromBody,
which also compares raw function bodies where OPERATOR() carries its own
precedence, into normalizeDeparsedExpression for deparsed contexts only, and
prefix stripping no longer turns OPERATOR(schema.op) into OPERATOR(op).

An operator in the managed schema that duplicates a pg_catalog operator's
name and operand types is only reachable schema-qualified; the inspector now
records such names (Metadata.ShadowedOperators) and both normalization paths
keep their qualifier. nextval-backed defaults are normalized too.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

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.

🟡 Changes recommended

Inconsistent schema-qualifier normalization for routine parameter defaults and overloaded operators causes unchanged routines, views, and materialized views to be reported as changed.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Normalize qualified parameter default expressions

ir/​normalize.go:233

This also normalizes function and procedure parameter defaults, but normalizeSchemaNames never normalizes their DefaultValue after renaming the temporary schema. With intarray in app, a qualified default such as ARRAY[1] OPERATOR(app.+) ARRAY[2] becomes bare + in the current IR but stays qualified in the temporary-schema IR, so verbatim parameter comparison repeatedly reports a routine change. Apply stripQualifiers(replaceString(*param.DefaultValue)) to non-nil defaults in both parameter loops in cmd/plan/plan.go, without rewriting routine bodies.

3 resolved since last review

Review effort: Balanced

Comment thread ir/inspector.go Resolved
…parameter defaults (#697)

Review follow-up. The shadowed-operator exemption is decided per name
because the normalizer only sees deparsed text, so a name with both a
shadowed and an unshadowed overload (a custom public.->(jsonb, text) next
to hstore's ->) is no longer exempted: the extension operator is the common
case and the one this fix is for. Routine parameter defaults are normalized
after the temp-schema rename like column defaults.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

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.

Comment thread ir/normalize.go Resolved
Comment thread ir/normalize.go Resolved
Comment thread cmd/plan/plan.go Resolved
Comment thread ir/normalize.go Resolved
…697)

Review follow-up: handle a quote_ident-form schema (OPERATOR("My Schema".op)),
share the operator character set as ir.OperatorNameChars, make the
OPERATOR( look-behind in prefix stripping constant-time, and stop re-running
the pattern inside the plan-side replacement.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment thread cmd/plan/plan.go Resolved
Comment thread cmd/plan/plan.go Resolved
Comment thread ir/normalize.go Resolved
Comment thread ir/inspector.go Resolved
…ate OPERATOR keyword case (#697)

Review follow-up: newSameSchemaQualifierStripper recognizes the target
schema in bare and quote_ident form for function, cast and operator
qualifiers; the OPERATOR( look-behind in prefix stripping accepts any
keyword case and whitespace; doc wording fixed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment thread cmd/plan/plan.go Resolved
Comment thread cmd/plan/plan.go Resolved
Comment thread cmd/plan/plan.go Resolved
Comment thread cmd/plan/plan.go Resolved
Comment thread ir/normalize.go Resolved
Comment thread ir/normalize.go Resolved
tianzhou and others added 2 commits November 8, 2026 02:54
…yword case in the scanner (#697)

Review follow-up: the plan-side stripper handles a quote_ident-form
function or type name after the schema qualifier, and
normalizeOperatorSyntax detects the OPERATOR keyword like
followsOperatorKeyword does (any case, optional whitespace).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…n defaults (#697)

The plan-side stripper now handles quoted object names, so the current-side
default normalizer must too, or a default such as public."MyFunc"() compares
differently on the two sides (broke dependency/issue_571 in CI). The
replacement is also quote-aware now, so string literals are left alone.
Regenerates the issue 571 fixture: its default reads "MyFunc"() like every
other expression context already did.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@tianzhou
tianzhou merged commit fa61e89 into main Nov 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.

External plan database with target schema public renders extension operators as OPERATOR(public.->), recreating views on every plan

2 participants