Skip to content

Conversation

@asonawalla
Copy link
Contributor

Summary

Fixes #224 - Perpetual diffs when policy expressions contain table references in same-schema subqueries.

Following the pattern established in PR #222 for function qualifiers, this extends policy expression normalization to also strip same-schema qualifiers from table references.

Problem:

  • User writes: USING (user_id IN (SELECT u.id FROM users u))
  • pgschema adds schema: FROM public.users u
  • PostgreSQL's pg_get_expr() returns: FROM users u
  • Result: perpetual diff on every plan/apply cycle

Solution:

  • Add regex pattern to strip same-schema table qualifiers (matching the function qualifier approach)
  • Pattern matches schema.identifier followed by whitespace, comma, ), or end of string

Changes

  • ir/normalize.go: Extended normalizePolicyExpression() to normalize table references
  • testdata/diff/create_policy/same_schema_table_reference/: New test case

Test plan

  • New test case passes: PGSCHEMA_TEST_FILTER="create_policy/same_schema_table_reference" go test ./internal/diff -run TestDiffFromFiles
  • Integration test passes with idempotency check: PGSCHEMA_TEST_FILTER="create_policy/same_schema_table_reference" go test ./cmd -run TestPlanAndApply
  • All existing policy tests pass: PGSCHEMA_TEST_FILTER="create_policy/" go test ./internal/diff -run TestDiffFromFiles
  • Verified fix works in downstream project

🤖 Generated with Claude Code

Following the pattern established in PR pgplex#222 for function qualifiers,
this extends policy expression normalization to also strip same-schema
qualifiers from table references.

When a policy expression contains a subquery like:
  FROM public.users u
PostgreSQL's pg_get_expr() returns it without the schema qualifier:
  FROM users u

This caused perpetual diffs because pgschema added the qualifier back.
The fix applies the same normalization approach: strip schema qualifiers
from table references when they match the target schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

Copy link
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the fix.

@tianzhou tianzhou merged commit ad6b14a into pgplex:main Jan 5, 2026
1 check passed
alecthomas pushed a commit to alecthomas/pgschema that referenced this pull request Jan 26, 2026
…gplex#226)

Following the pattern established in PR pgplex#222 for function qualifiers,
this extends policy expression normalization to also strip same-schema
qualifiers from table references.

When a policy expression contains a subquery like:
  FROM public.users u
PostgreSQL's pg_get_expr() returns it without the schema qualifier:
  FROM users u

This caused perpetual diffs because pgschema added the qualifier back.
The fix applies the same normalization approach: strip schema qualifiers
from table references when they match the target schema.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
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.

Perpetual policy diffs: table references not normalized

2 participants