Conversation
A policy's USING and WITH CHECK expressions are resolved at CREATE POLICY time, so the functions they call must exist first and be dropped after. PostgreSQL records those references in pg_depend (pg_policy -> pg_proc), so read them into Policy.DependsOnFunctions. The ordering has to live on the table's vertex, not the policy's: a table's policies are emitted inside the table's own statement list (inside CREATE TABLE for a new table, appended by the table's alter otherwise), so the policy never has a vertex of its own in the top-level graph. The table's add/alter therefore runs after those functions, and the table's drop runs before their drop.
dilame
force-pushed
the
fix/policy-function-deps
branch
from
September 26, 2026 22:10
452b86b to
6f230d5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
A table is now ordered after the functions its RLS policies call.
GetPoliciesreturns the policy'soid.PolicygainsDependsOnFunctions, read frompg_depend(pg_policy→pg_proc), the same way a check constraint's function dependencies are read.The edges belong on the table's vertex, not the policy's. A table's policies are emitted inside the table's own statement list — inside
CREATE TABLEfor a new table, appended by the table's alter otherwise — so a policy never has a vertex of its own in the top-level graph, and an edge from it would resolve against nothing.Why
PostgreSQL resolves the functions named in a policy expression at
CREATE POLICYtime. Nothing ordered a policy against them, so a plan that creates a function and a policy calling it could emit the policy first and fail:This is common in RLS-heavy schemas, where every policy calls a session helper such as
app_runtime.current_user_id().Validation
go test ./...New acceptance case: a policy added to an existing table whose
USINGexpression calls a function created in the same plan. Applying the plan is the assertion — the wrong order fails withfunction current_user_id() does not exist.