Re: RLS creates inaccurate limit and offset results - Mailing list pgsql-bugs

From Tom Lane
Subject Re: RLS creates inaccurate limit and offset results
Date
Msg-id 2381086.1762972021@sss.pgh.pa.us
Whole thread Raw
In response to Re: RLS creates inaccurate limit and offset results  (mike@mikebrancato.com)
List pgsql-bugs
mike@mikebrancato.com writes:
> If I understand the documentation (https://www.postgresql.org/docs/current/queries-limit.html), inconsistent results
fordifferent LIMIT / OFFSET values is only a known issue when selecting different subsets of data. 

You are willfully ignoring the lede on that page:

    When using LIMIT, it is important to use an ORDER BY clause that
    constrains the result rows into a unique order. Otherwise you will
    get an unpredictable subset of the query's rows.

This has nothing particularly to do with RLS; RLS is merely one way in
which a query might gain complications sufficient to cause the planner
to change plans.  Without ORDER BY, the planner is free to switch to
a plan that delivers rows in some other order.  Even just modifying
OFFSET can cause that to happen (since it's effectively a change in
the number of rows required to be fetched).

I suspect if you check with EXPLAIN, you'll find that you get
different plans with the different OFFSET values.  But whatever the
details, your code was making unjustified assumptions.

            regards, tom lane



pgsql-bugs by date:

Previous
From: Christophe Pettus
Date:
Subject: Re: RLS creates inaccurate limit and offset results
Next
From: Tom Lane
Date:
Subject: Re: BUG #19106: Potential regression with CTE materialization planning in Postgres 18