pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

CREATE/ALTER POLICY loses its USING / WITH CHECK parentheses when the expression is a scalar subquery

Open dhia-03 opened this issue 1 week ago • 0 comments

Summary

When a policy's USING or WITH CHECK expression is a scalar subquery, for example USING ((SELECT is_allowed())), pgschema writes it as USING ( SELECT is_allowed() AS is_allowed), with only the subquery's own parentheses. CREATE POLICY and ALTER POLICY need their own pair around the expression, so PostgreSQL rejects the statement. This affects dump, and also plan/apply whenever such a policy has to be created or altered.

Wrapping the function call in a scalar subquery is the usual way to write RLS policies that call a function: PostgreSQL then evaluates the function once per statement (as an InitPlan) and not once per row. Schemas that follow this pattern can't currently be dumped or applied.

Versions

  • pgschema 1.15.0 (the code is unchanged on main as of 2026-11-25)
  • PostgreSQL 18.6

Reproduction

CREATE FUNCTION is_allowed() RETURNS boolean LANGUAGE sql STABLE AS $$ SELECT true $$;
CREATE TABLE items (id int PRIMARY KEY, a int, b int);
ALTER TABLE items ENABLE ROW LEVEL SECURITY;
CREATE POLICY read_gate  ON items FOR SELECT USING ((SELECT is_allowed()));
CREATE POLICY write_gate ON items FOR INSERT WITH CHECK ((SELECT is_allowed()));
CREATE POLICY or_gate    ON items FOR UPDATE USING ((a > 0) OR (b > 0));

PostgreSQL accepts this. pgschema dump gives:

CREATE POLICY or_gate ON items FOR UPDATE TO PUBLIC USING ((a > 0) OR (b > 0));
CREATE POLICY read_gate ON items FOR SELECT TO PUBLIC USING ( SELECT is_allowed() AS is_allowed);
CREATE POLICY write_gate ON items FOR INSERT TO PUBLIC WITH CHECK ( SELECT is_allowed() AS is_allowed);

or_gate is correct. The other two are invalid, and applying the dump to an empty database fails:

Error: failed to apply desired state: failed to apply schema SQL to temporary schema pgschema_tmp_...: ERROR: syntax error at or near "SELECT" (SQLSTATE 42601)

The ALTER POLICY path has the same problem. Changing read_gate in the desired state to USING ((SELECT NOT is_allowed())) plans:

ALTER POLICY read_gate ON items USING ( SELECT (NOT is_allowed()));

Expected

CREATE POLICY read_gate ON items FOR SELECT TO PUBLIC USING (( SELECT is_allowed() AS is_allowed));
CREATE POLICY write_gate ON items FOR INSERT TO PUBLIC WITH CHECK (( SELECT is_allowed() AS is_allowed));
ALTER POLICY read_gate ON items USING (( SELECT (NOT is_allowed())));

Cause

ensureParentheses in internal/diff/policy.go (added for #259) treats any expression that starts with ( and ends with ) as already wrapped. pg_get_expr returns a scalar subquery as ( SELECT ... ), and those parentheses belong to the subquery, not to the policy clause. For the same reason, a top-level expression such as (a) OR (b) would also be taken as wrapped. pg_get_expr happens to wrap boolean operators, so or_gate above comes out correct, but the check doesn't guarantee it.

Proposed fix

Treat the expression as wrapped only when its first ( is closed by its last ) and the content isn't a subquery:

func ensureParentheses(expr string) string {
	if expr == "" {
		return expr
	}
	expr = strings.TrimSpace(expr)
	if enclosedByOneParenPair(expr) {
		inner := strings.TrimSpace(expr[1 : len(expr)-1])
		// "( SELECT ... )" is a scalar subquery: its parentheses belong to the
		// subquery, and USING / WITH CHECK still need their own pair around it.
		if !startsWithKeyword(inner, "SELECT") && !startsWithKeyword(inner, "WITH") {
			return expr
		}
	}
	return "(" + expr + ")"
}

enclosedByOneParenPair walks the string, skipping quoted literals and identifiers, and returns false if the depth returns to zero before the last character. startsWithKeyword is a case-insensitive prefix check with a word boundary, so an identifier like with_flag doesn't match. Extra parentheses are always valid SQL, so the worst case of a false positive is one redundant pair.

With this change the repro above dumps, plans and applies correctly, and go test ./internal/diff/ passes. I'm glad to open a PR with the change and a unit test if that helps.

dhia-03 avatar Nov 25 '26 10:11 dhia-03