Re: RLS creates inaccurate limit and offset results

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: mike(at)mikebrancato(dot)com
Cc: Christophe Pettus <xof(at)thebuild(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: RLS creates inaccurate limit and offset results
Date: 2025-11-12 18:27:01
Message-ID: 2381086.1762972021@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

mike(at)mikebrancato(dot)com writes:
> If I understand the documentation (https://www.postgresql.org/docs/current/queries-limit.html) inconsistent results for different 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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2025-11-12 19:32:53 Re: BUG #19106: Potential regression with CTE materialization planning in Postgres 18
Previous Message Christophe Pettus 2025-11-12 18:20:07 Re: RLS creates inaccurate limit and offset results