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 1wI7WL-007o6l-1W for pgsql-bugs@arkaria.postgresql.org; Wed, 29 Apr 2026 16:12:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wI7WK-004HUa-1i for pgsql-bugs@arkaria.postgresql.org; Wed, 29 Apr 2026 16:12:16 +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 1wI7WK-004HUP-0m for pgsql-bugs@lists.postgresql.org; Wed, 29 Apr 2026 16:12:16 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wI7WH-00000003pP3-3uLz for pgsql-bugs@lists.postgresql.org; Wed, 29 Apr 2026 16:12:15 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2adbfab4501so59822305ad.2 for ; Wed, 29 Apr 2026 09:12:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1777479131; cv=none; d=google.com; s=arc-20240605; b=bbyjPApzQ+BoYYDDT+Ujin7W6AGj5ihtu4do+tRP4fKNu83A1ZN6UnOZmM5N5vM9WR qMLvpkfYd0AoXjDEgm+Gx+nRvqOtCZ9oQIdu85qSZnv1U2UHqJDY5kTYDsUQvCp3kks+ WwP8fDSJZGY6KxP36EVKorvpaIUvDjh/Jlw4JBQ/lTSjB3YFKvQQ+Fh9gNKZf07fbu6y of5Xmv7iXJDMSY55QT1YMHkMzpMIR0r/8UOGMWDRRqvDr1qzUHShOFkMTFfuxJYRNKOG J2I6PFd0Dcf/BZFxh9Vx9YK9K77lZfhfNcccHohFmwrVKcanhnRZPV98qbs2fkq/wv3M yZsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=OTu2jciGuSrNUMHbfysvc0x4DZx6lMiP77JrIV2+RAk=; fh=eq4N0ZJ8hrEgR38nsBqR0qT98+sD9swSO80XeCQALy8=; b=PCkqdAKDTFi52QijlA+X77UAYbpDKdnCJQYbl8MfmPy8/pGJVrJ64VEuggUGMZw3iQ grAIfmoYth10QoAVmJ6kxYfRnCM71ZqMnYZGqBW73wMECUBt77sadRdPk5p8xVTzWCMF 6lxiI8RYaVYEC/334c8FoqZ+E71L3kW3jVnnNsaGP63hUv2LR4OikZumHTtmqZ9srs2x Li3FhKyaw5rhI4+vWkWjp3QAtOnhJzXMnjt1r2uVj22ing4YZnB0tSg1ITW/O1NsvNx+ uxeQao6pEV6SLc8QJ+EFVuAE2Rr/7hhlJeNIaN4EE9YiQ5uSpqI+0A5jc6IWFH2/3pqF wTSw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777479131; x=1778083931; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=OTu2jciGuSrNUMHbfysvc0x4DZx6lMiP77JrIV2+RAk=; b=BBDvjvD9oTdb4yDyr+YNXZohkRiREvWMDFnMLy3yDNeZQXcYSPzvPsFhMxxPiatBYs 9BcUQxvw2ul7If27sreWNPngUCWz0+gYRPa1slCCRfU5P0b0D11ODizU5lpRKlICaLtK Oj73sN/5+wNmWBt8WjPTpV0R8ASXTol5N6yV/R0FuRv7uUZNtNo3fSLEY7kS0bO//7dl UUVltRkHZgxbtNPNnbCJF0JNuZ4Sjrnt7DxduIKuyVMB3eQQd//ZNSdMp2ADtlJFNt9w /ktglpvU3XAEl/uuStgEFq+4sdBMhvH/v8/qcge81VGdW+iswspvNu8AbapB51pnJEuN Uzwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777479131; x=1778083931; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=OTu2jciGuSrNUMHbfysvc0x4DZx6lMiP77JrIV2+RAk=; b=OGmLZNE4zDuDZlcreYzil5ATBjsmIr043kOS/lZ9xiDFd3+SDWS8s7/pINcHhbBoiG jfyZs7XDAo+95SQK3b/aj/0HeIoEzQZ/Gub98qSU5a2AWF6VrITFxP2U/yheCUAtQRCK fqdAdKrxnxlxbKKgWoRpMJLIiWVocmOcXwQH9NvJLFiQYtQPvDTifzaWfC2opI+94DWG L/PZsORC2NblY8nLqzIDh48ipnzQmsH+IZoZIzBnBWUjsyD2jb7rpQKwvXP+wT50+mfF ALg3FCvj9MOzv18FGMJy54TCMoujOAdewHqrSWAZkYFzzhJlzKsKnsdb8i/T8mIsa8N1 FyGA== X-Gm-Message-State: AOJu0YwBXxkxmsJplyb0PlLnDbHAd6rFuUIOulg4Edbz4hurwGbQ65Vz 2YZnmVVmg0jVVcFvgu/g9xlC1Lw2CgH0LkXSxQiHIwbir2Kb1a8Z3aVNKAAK2LzAE8HhR8VC2Wf KZ6Bh4+aLpCgMn2eJIEtj3i90rcoX4Zs= X-Gm-Gg: AeBDiespjYyaBncZ6flzvfR+Dbr3/LgwxMrQYbynPsrUO0tF01iCO6CVqJ289AB3xoi Ud/+3Pc5RACO38xUMQfHHgOOvofFm0roSVd8M8sn9RahzrC/ATQZhOqyyaE6g4l5WF8ErCfQMl0 EtmZ0zKwta5RjxWWGpHbYizHooXCEOCIQuZe/3Z4LgdLfz87+p1fVVHeaAQUjjcdelumFVrKnPA NPzPCc5xB1Vv2sH7rEcGDDOHiFAhGeAqgy/B535lzSliJ9nqpZWgmTd4THZwTsU82p++i/7FaZK JLLrx1PdSy4dJ2BibdNd0NBLXp68/8s2+Ph6cKWYOcTxiVyS6pM= X-Received: by 2002:a17:903:3c2f:b0:2b9:6453:389a with SMTP id d9443c01a7336-2b97c3cc0b3mr82487045ad.8.1777479131183; Wed, 29 Apr 2026 09:12:11 -0700 (PDT) MIME-Version: 1.0 References: <119bd418-1d7a-42c7-9270-86f3b6696399@gmail.com> <9dece148-3e1d-4b0f-b798-574988fe76fe@gmail.com> <117efd72-5692-4844-bc4e-c553d03d393e@gmail.com> In-Reply-To: From: Masahiko Sawada Date: Wed, 29 Apr 2026 09:11:32 -0700 X-Gm-Features: AVHnY4JJBIOkoJxDjPtzPnWlTE1Idf8VpanzZ8cXFZlIpuOHnxepF62oBuTd8H8 Message-ID: Subject: Re: TRAP: failed Assert("offsets[i] > offsets[i - 1]"), File: "tidstore.c" To: Andrei Lepikhov Cc: PostgreSQL mailing lists Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Apr 28, 2026 at 10:09=E2=80=AFAM Masahiko Sawada wrote: > > On Sun, Apr 26, 2026 at 2:06=E2=80=AFAM Andrei Lepikhov wrote: > > > > On 25/04/2026 01:23, Masahiko Sawada wrote: > > > On Wed, Apr 22, 2026 at 10:23=E2=80=AFAM Andrei Lepikhov wrote: > > >> Both tools are experimental and not meant for core use; they are onl= y used to > > >> trigger potential issues. In this case, I think the query picked a c= ostly sorted > > >> path, which led to the crash. > > > > > > Does this imply that array_agg() could return unsorted results > > > depending on the plan the optimizer chooses? Or is such a path > > > currently never selected by the optimizer? > > > > The array_agg() function does not sort its output. In theory, this mean= s the > > join could return results in any order, but in practice, I have not see= n this > > happen. > > > > > > > > I=E2=80=99m asking because if this scenario never occurs with the cur= rent > > > optimizer, it might make sense to apply the patch only to HEAD (i.e., > > > for PG20). On the other hand, backpatching to PG17 might be justified= , > > > given that DISTINCT does not guarantee sorted results in principle, > > > and the fix could benefit extension development on stable branches. > > > > In stable versions, the planner's logic remains unchanged. So, it seems > > reliable. However, backpatching could help extension developers a littl= e bit. > > Since this code fixes a real issue and does not break anything complex,= I would > > backpatch it. Still, I am fine with just committing it to master if you= prefer. > > > > P.S. > > > > I looked into the issue further. The problem happens when the join side= s are > > shuffled. Here is what I found: > > > > EXPLAIN of the successful execution (unnecessary details stripped): > > > > Insert on pg_temp.hideblocks (cost=3D1.21..1.66 rows=3D0 width=3D0) > > -> Subquery Scan on unnamed_subquery (cost=3D1.21..1.66 rows=3D5 w= idth=3D8) > > Output: unnamed_subquery.do_set_block_offsets > > -> GroupAggregate (cost=3D1.21..1.61 rows=3D5 width=3D16) > > Output: do_set_block_offsets("*VALUES*".column1, > > (array_agg("*VALUES*_1".column1))::smallint[]),= ... > > Group Key: "*VALUES*".column1 > > -> Sort (cost=3D1.21..1.27 rows=3D25 width=3D12) > > Output: "*VALUES*".column1, "*VALUES*_1".column1 > > Sort Key: "*VALUES*".column1 > > -> Nested Loop (cost=3D0.00..0.62 rows=3D25 widt= h=3D12) > > Output: "*VALUES*".column1, "*VALUES*_1".col= umn1 > > -> Values Scan on "*VALUES*" > > (cost=3D0.00..0.06 rows=3D5 width=3D8) > > Output: "*VALUES*".column1 > > -> Values Scan on "*VALUES*_1" > > (cost=3D0.00..0.06 rows=3D5 width=3D4) > > Output: "*VALUES*_1".column1 > > > > EXPLAIN that causes assertion: > > > > Insert on pg_temp.hideblocks (cost=3D1.03..1.48 rows=3D0 width=3D0) > > -> Subquery Scan on unnamed_subquery (cost=3D1.03..1.48 rows=3D5 w= idth=3D8) > > Output: unnamed_subquery.do_set_block_offsets > > -> GroupAggregate (cost=3D1.03..1.43 rows=3D5 width=3D16) > > Output: do_set_block_offsets("*VALUES*".column1, > > (array_agg("*VALUES*_1".column1))::smallint[]),= ... > > Group Key: "*VALUES*".column1 > > -> Sort (cost=3D1.03..1.09 rows=3D25 width=3D12) > > Output: "*VALUES*".column1, "*VALUES*_1".column1 > > Sort Key: "*VALUES*".column1 > > -> Nested Loop (cost=3D0.00..0.45 rows=3D25 widt= h=3D12) > > Output: "*VALUES*".column1, "*VALUES*_1".col= umn1 > > -> Values Scan on "*VALUES*_1" > > (cost=3D0.00..0.06 rows=3D5 width=3D4) > > Output: "*VALUES*_1".column1 > > -> Materialize (cost=3D0.00..0.09 rows=3D5= width=3D8) > > Output: "*VALUES*".column1 > > -> Values Scan on "*VALUES*" > > (cost=3D0.00..0.06 rows=3D5 wid= th=3D8) > > Output: "*VALUES*".column1 > > > > At the second case offsets have come to the aggregation without order t= hat > > highlighted the issue. > > Thank you for sharing the details. > > While the assertion failure is not observed during regular regression > tests because the query is simple enough that the optimizer > consistently chooses plans producing the sorted results, given that > the DISTINCT without the ORDER BY doesn't guarantee to produce the > sorted results in theory, I think it makes sense to apply the proposed > patch. And, it would also make sense to backpatch to PG17, where > tid_store was introduced, for extension development on back branches. > > I've attached the patches. I'm going to push them, barring any objections= . > Pushed. Regards, --=20 Masahiko Sawada Amazon Web Services: https://aws.amazon.com