Skip to content

fix(sql): recognize more PostgreSQL state-changing functions in the mutation classifier - #46687

Open
sha174n wants to merge 1 commit into
apache:masterfrom
sha174n:classifier-state-changing-fns
Open

sha174n wants to merge 1 commit into
apache:masterfrom
sha174n:classifier-state-changing-fns

Conversation

@sha174n

@sha174n sha174n commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

SUMMARY

The SQL statement mutation classifier (SQLStatement.is_mutating() in superset/sql/parse.py) flags statements that change state so callers can tell them apart from plain reads. It already recognizes a set of PostgreSQL functions that mutate shared server-side state even when invoked from inside a SELECT. This adds a few more that were not yet in that set, for completeness and consistency with the ones already handled:

  • pg_terminate_backend, which ends another session (the sibling pg_cancel_backend was already recognized).
  • The advisory-lock family (pg_advisory_lock / _shared / _unlock / _unlock_shared / _unlock_all / _xact_lock / _xact_lock_shared and their pg_try_* forms). Each acquires or releases a server-side lock that outlives the statement and is observable by every other session, so the call changes shared state despite reading like a boolean probe.

The new names are added to the existing PostgreSQL-scoped function set, so the change is additive and does not affect other dialects.

TESTING INSTRUCTIONS

Extends the parametrized unit tests in tests/unit_tests/sql/parse_tests.py. Run: pytest tests/unit_tests/sql/parse_tests.py

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
  • Migration is atomic, supports rollback & is backwards-compatible
  • Confirm DB migration upgrade and downgrade tested
  • Runtime changes include necessary synchronization

…utation classifier

Extend the SQL statement mutation classifier so it recognizes additional
PostgreSQL functions that change shared server-side state even though they
are called from inside a plain SELECT wrapper:

- pg_terminate_backend, which ends another session (the classifier already
  recognized the sibling pg_cancel_backend).
- the advisory-lock family (pg_advisory_lock / _shared / _unlock /
  _unlock_shared / _unlock_all / _xact_lock / _xact_lock_shared and their
  pg_try_* counterparts). Each acquires or releases a server-side lock that
  outlives the statement and is observable by every other session, so the
  call changes shared state despite reading like a boolean probe.

These are added to the existing PostgreSQL-scoped function name set, so the
change is additive and does not affect other dialects. Parametrized unit
tests cover each new form and confirm a plain SELECT stays non-mutating.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread superset/sql/parse.py
Comment on lines +1366 to +1376
"PG_ADVISORY_LOCK",
"PG_ADVISORY_LOCK_SHARED",
"PG_ADVISORY_UNLOCK",
"PG_ADVISORY_UNLOCK_SHARED",
"PG_ADVISORY_UNLOCK_ALL",
"PG_ADVISORY_XACT_LOCK",
"PG_ADVISORY_XACT_LOCK_SHARED",
"PG_TRY_ADVISORY_LOCK",
"PG_TRY_ADVISORY_LOCK_SHARED",
"PG_TRY_ADVISORY_XACT_LOCK",
"PG_TRY_ADVISORY_XACT_LOCK_SHARED",

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.

Suggestion: These names make SELECTs skip automatic row limits; selecting over a large table can acquire locks for many rows, leaving session locks held and exhausting server resources.

Assessment: 🟠 Major · 🔁 Occurrence: Rarely · 🏷️ Performance

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/sql/parse.py
**Line:** 1366:1376
**Comment:**
	*Performance: These names make SELECTs skip automatic row limits; selecting over a large table can acquire locks for many rows, leaving session locks held and exhausting server resources.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

@codeant-ai-for-open-source

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟡  Medium Risk

  • The PR needs attention before merging because classifying advisory-lock calls as mutating may disable automatic row limits.
  • An unbounded SELECT that acquires session-level locks per row could hold many locks and exhaust server resources.

Assessed commit: e1a8aef50356

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.42%. Comparing base (7819e82) to head (e1a8aef).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #46687   +/-   ##
=======================================
  Coverage   86.42%   86.42%           
=======================================
  Files        3183     3183           
  Lines      222605   222605           
  Branches    52996    52996           
=======================================
  Hits       192377   192377           
  Misses      26758    26758           
  Partials     3470     3470           
Flag Coverage Δ
hive 33.63% <ø> (ø)
mysql 51.83% <ø> (ø)
postgres 51.85% <ø> (ø)
presto 35.32% <ø> (ø)
python 88.81% <ø> (ø)
sqlite 51.55% <ø> (ø)
unit 84.37% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant