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.98.2) (envelope-from ) id 1x6vvv-000000000JD-3k9Z for pgsql-bugs@arkaria.postgresql.org; Wed, 16 Sep 2026 20:08:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x6vuu-000000007Uw-0WY1 for pgsql-bugs@arkaria.postgresql.org; Wed, 16 Sep 2026 20:07:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x6vut-000000007Ul-2txh for pgsql-bugs@lists.postgresql.org; Wed, 16 Sep 2026 20:07:39 +0000 Received: from mail-pz2-x0e.google.com ([2607:f8b0:4864:3b::e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x6vur-00000000TLD-40Dj for pgsql-bugs@postgresql.org; Wed, 16 Sep 2026 20:07:38 +0000 Received: by mail-pz2-x0e.google.com with SMTP id 41be03b00d2f7-cc1cea50dc1so36808a12.0 for ; Wed, 16 Sep 2026 13:07:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20251104.gappssmtp.com; s=20251104; t=1789589257; x=1790194057; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:content-type:date :to:from:subject:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=n9IGhRLT0E/4hrOl2QPG5o8C42xTXD0EczF0ZbesfLs=; b=gNPIAkvhKl7I4Lw5OuQq12rJ074qP3nUaGemD3TPYf+hI7Dk3g/R9yQtAjRQ+Hh7Ep 7SZrM37HSLhPcfQqNrBFh+ooX7lD2QTLc3eXj7nmIpj6Yl1eqLY/b2kX619Gemd37nTh RctJyIinQhgjs3MrQ0Gfh4NTQ+U69qFRvwnG5GrE7jNu6lcGenSq+3OyuFbsnr/gdTqX MdEKKjI4tSxD5/JRBoWLGp0qO3z2101qrg4xLnEWUDiOP6sHjdvxkCdIL1kmTE/BGBqC CE4ecpUJI1UdCaMFKHQ8kVGjVgf7DkepV7P/lqQcUlM6inKaFJMGBi+yHTOAH8rp1jeV rzRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789589257; x=1790194057; h=mime-version:user-agent:content-transfer-encoding:content-type:date :to:from:subject:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=n9IGhRLT0E/4hrOl2QPG5o8C42xTXD0EczF0ZbesfLs=; b=tF+kcv6yafZ4Ce5/FJgFvCaR3bJZv9srDQshbgW5dY95cWIlTMECZdlb5sNGmO056w jOjN5af3aoLdmxHFjoHCFotF5w4BQ0Poy8FpT7u+/1Kr75ZDS9cG6zosewlq1zMZpXHi qgc4eNZKMokWU+cxFUXtKHKx1swXcHpKxdgKp6h7X+WJGqcwqmdq7QrI3r9igMKoUWll DXXHk8DAVpAlz2kgReM3s24FXv8Ey1u+uu5CW/lHvh1qtLX5rl9NorJmi2VvC6+Mot4D oNbz3DKMsfbq57JSXi+cewqJJ9G597idqHbLCrd3k+h6d4JqH02cWkBR2PtpQDdG5DIs 0fVw== X-Gm-Message-State: AFuF++m+SduyGdDNwt0+8SACqqg3rQbBsAznZpXi58ZIbk8xmAtiSYb6 Zi4tg6YN+e9ICQ3Ntnfc+u/eURf5+Kbjy6Eza2iLRlKF4yg43CUX/tLSpVJTFVp5O9zqPmks84P 3TkQ= X-Gm-Gg: AYBFou0ju59hIm8EIZs6LzpXdbUTQloiYnA2L+pAV+42LbqKmbP7S0hDMs812wog327 BqUl9ueZ4lyALH6rqdvfQo1QzdeX2NWjZbJuBngT6paHGfRtivx+cuAWnVwf0PnBQ3NLHxh0FP0 c6V2gkdbetQ1QQJEhRwfDm2x0CUycK1pzgqFQzMvHplI6PUaGX3hcrdOj0hG1fEwVHlTClUGLYg jkYnsKh5jfCIBssIcThNpRyVNvVfevFxtWG6cAIvfBDwHhIWvw05yCJ8pGsXbJvVxQ2OXeXT/oU AXEXhN/0YZ5oeENzQ7U9iaF4SCC0rQk61SB1r2JDbFdXvF06YuQzDg3YeqZuAwNPKweud9kdVWr uL2JNV09qtisAJS5dlbxigFY40J02oTMrcvbg/aJmhHCHcEiXzhWffmxqYS5eT1z7eAvbvlmFBp TQcCawcYRWqSk3It//xgFaJGA29cjJ+iBGjVykFXDaOLUD2gZiKEr/jXqkOWX6/UdCb9SszW5k X-Received: by 2002:a17:903:1990:b0:2dd:3ddf:b876 with SMTP id d9443c01a7336-2dd8cc8bc70mr82517355ad.0.1789589257039; Wed, 16 Sep 2026 13:07:37 -0700 (PDT) Received: from [10.10.247.208] ([50.175.226.11]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2dd89f12117sm16578825ad.70.2026.09.16.13.07.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2026 13:07:36 -0700 (PDT) Message-ID: <6b50233fcc0f7d6f4845f083777fa9702d9612e4.camel@j-davis.com> Subject: TID Range Scans can return wrong results after scanning backwards From: Jeff Davis To: pgsql-bugs@postgresql.org Date: Wed, 16 Sep 2026 13:07:35 -0700 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3-0ubuntu1.1 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk AI reported a bug (appended to this email) where TID Range Scans can return the wrong results after changing the direction of the scan. Regards, Jeff Davis SQL repro --------- CREATE TABLE t (id int, data text) WITH (fillfactor =3D 10); -- 5 rows per page, 20 pages: ctids (0,1) .. (19,5) INSERT INTO t SELECT i, repeat('x', 100) FROM generate_series(1, 100) i; SET enable_seqscan =3D off; BEGIN; DECLARE c SCROLL CURSOR FOR SELECT ctid FROM t WHERE ctid >=3D '(2,1)' AND ctid <=3D '(11,5)'; MOVE FORWARD 35 c; -- cursor is now on (8,5) FETCH BACKWARD 2 c; -- (8,4), (8,3): correct FETCH BACKWARD ALL c; -- should return 32 rows, returns 12 COMMIT; Diagnosis --------- heap_setscanlimits() stores the number of blocks to scan in rs_numblocks. heapgettup_advance_block() then treats that field as a remaining budget: it decrements it once for every block it hands out, in either direction, and ends the scan when it reaches zero. That model only holds for a scan that moves in one direction and consumes exactly the blocks it requests. A change in direction breaks that.