Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vJFYi-00GDdn-05 for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Nov 2025 18:27:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vJFYf-00DTQp-2J for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Nov 2025 18:27:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vJFYf-00DTQh-1Q for pgsql-bugs@lists.postgresql.org; Wed, 12 Nov 2025 18:27:05 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vJFYd-007O9h-0K for pgsql-bugs@lists.postgresql.org; Wed, 12 Nov 2025 18:27:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 5ACIR1am2381087; Wed, 12 Nov 2025 13:27:01 -0500 From: Tom Lane To: mike@mikebrancato.com cc: Christophe Pettus , pgsql-bugs@lists.postgresql.org Subject: Re: RLS creates inaccurate limit and offset results In-reply-to: References: <9C0FBBAE-F65F-407D-B69A-B26CBC1AE35B@mikebrancato.com> Comments: In-reply-to mike@mikebrancato.com message dated "Tue, 11 Nov 2025 17:46:28 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2381085.1762972021.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 12 Nov 2025 13:27:01 -0500 Message-ID: <2381086.1762972021@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk mike@mikebrancato.com writes: > If I understand the documentation (https://www.postgresql.org/docs/curre= nt/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