Re: Mark the enum comparison functions as leakproof

From: Abraham Pérez <abraham1798(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Mark the enum comparison functions as leakproof
Date: 2026-11-26 13:22:12
Message-ID: CAF6dGh1su+N_RF3Ac6zL1iek6exbSBgRxgMsRs_Gg+UsDd251Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I picked this patch up from the current commitfest (PG20-3); it had no
reviewer assigned, so here is a review of v1.

Testing
-------

- Applies cleanly with "git am" on today's master (73401447c34).
- Builds with --enable-debug --enable-cassert --enable-tap-tests and no new
compiler warnings.
- "make check" passes (all 243 tests, including opr_sanity).
- I confirmed proleakproof is set on all seven functions and that it produces
the intended planner behavior. With the patch, an enum equality qual can be
used as an index condition under RLS -- the case Andreas described upthread.
With a policy on the table and an index on the enum column, as a non-owner:

EXPLAIN (COSTS OFF) SELECT * FROM lt WHERE c = 'g';
Bitmap Heap Scan on lt
Recheck Cond: (c = 'g'::rainbow)
Filter: ((id % 2) = 0)
-> Bitmap Index Scan on lt_c_idx
Index Cond: (c = 'g'::rainbow)

A non-leakproof reference (a regex match, textregexeq) stays a plain filter
applied together with the RLS policy qual, as expected:

EXPLAIN (COSTS OFF) SELECT * FROM lt WHERE t ~ '^x1';
Seq Scan on lt
Filter: (((id % 2) = 0) AND (t ~ '^x1'::text))

Correctness of the leakproofness argument
------------------------------------------

I traced the code paths the comparison functions can reach:

- enum_eq/enum_ne compare the two OIDs directly and can neither error out nor
leak anything, so marking them leakproof is clearly safe (and there seems to
be agreement on that already).

- The ordered comparisons go through enum_cmp_internal(). The equal-OID and
even-OID fast paths do no catalog work. The remaining paths can raise:
* "invalid internal value for enum: %u" (enum.c), and
* "enum value %u not found in cache for enum %s"
(compare_values_of_enum() in typcache.c).
Both include the argument OID, but they are only reachable with an enum OID
that has no matching pg_enum row, i.e. corrupt or forged data that cannot be
produced for a valid stored value through SQL. The remaining error source is
OOM while loading the enum cache, which can reveal allocation sizes but not
the values. So I agree the markings are safe as the code stands today.

The open question
-----------------

The unresolved point here is not whether v1 is safe today, but the policy
concern Tom raised: enum_cmp() is more code than one can scrutinize at a
glance, and the leakproof property then has to be preserved across future
changes to enum.c and typcache.c. The opr_sanity "list of built-in leakproof
functions" is explicitly a tripwire for that, so adding seven functions to it
is a non-trivial commitment rather than a mechanical change.

I don't have anything to add to that trade-off beyond the testing above. But
since enum_eq is uncontroversially safe and is also the function Andreas
actually needed for index conditions under RLS, one concrete way forward would
be to split the patch: mark only enum_eq/enum_ne leakproof now (small,
self-evidently safe, and it unblocks the equality / index-condition use case),
and keep the enum_cmp question separate. The GIN array operators Andreas
mentioned (arraycontains/arraycontained/arrayoverlap) are a different topic and
out of scope for this patch.

Status
------

Given the unresolved objection to the enum_cmp part, I'm setting this to
Waiting on Author, mainly to get a decision on scope (the full set vs.
equality only). Happy to re-test whichever direction you choose.

Thanks,
Abraham

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2026-11-26 13:23:03 Re: Prevent stack overflow in exprLocation while parsing long queries
Previous Message Tomas Vondra 2026-11-26 13:19:51 Re: Unify parallel worker handling for index builds and instrumentation