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 1wGvS5-006h9k-11 for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Apr 2026 09:06:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wGvS2-00A8Wy-22 for pgsql-bugs@arkaria.postgresql.org; Sun, 26 Apr 2026 09:06:54 +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 1wGvS2-00A8Wp-0q for pgsql-bugs@lists.postgresql.org; Sun, 26 Apr 2026 09:06:54 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wGvRz-000000038q4-3M1p for pgsql-bugs@lists.postgresql.org; Sun, 26 Apr 2026 09:06:53 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-48984d29fe3so107541385e9.0 for ; Sun, 26 Apr 2026 02:06:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777194410; x=1777799210; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=aA+/pT3NpRy/E0x6EaNyx6s8EffNBzy7iFMeQ1JwIEQ=; b=hy2kT3fkotsM8ufqG39ypU4iT28lUPhhntiDd2O9D9oC4qyitHaFUdNw3OTgqR5cLg mLrMK1qwRkR59p5tmec2HG1G8kkEB4xHZkd7vmamakSqOv0AfyoWv/bjXA3Ng107GIwJ wy6rtBY7V3sPSI939OZ5DNisfZyFa1KwLkh8sk+vtwCdwAU0tLzFheWMN2E83Obim7l6 HZ2k0aJUBDEjgr3DXy3dbY5/wX3+qtvKHN4/uOyueAIlbeU+NC4IbnxR5s8IXSpPqeht aEzdCwSdaguJqLkSExKFkoAaNMgRMQ493IyDEQEDaq79aOHmwBJ4yEB/RUVSU7jMo8Mk 4PSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777194410; x=1777799210; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aA+/pT3NpRy/E0x6EaNyx6s8EffNBzy7iFMeQ1JwIEQ=; b=Qj6LyoD05RY7S87rVJnE9JS48Vw2iXdhLnMXksOl0rR3lPxdaN/isKHZD97O2pADM5 S8Og3z1h1NcA+ojZPujtHJc29DORrcSEbehmpUr92SpadWf0R1qgcYJkdy3QLqfp+lIQ M/oU5o2uDaD57UIteN8HouywOIzgYGkC+QsWV7cbIWgJV0KomCjos6l71p0bRcBPHZwD 9m8C7cZvYEYEjKe9Mvhfh2TMoZDypW9sbTdPtbe2EBQIKAOJHyRDZPRhsVVz4aWRSEuG qkRNjjeNClFUKVMOGXRfjmOAJJMB0yAHPigc0UYqdYtg+GPZqOGvlnLuwifhIUj1bpPY O4xA== X-Gm-Message-State: AOJu0YxQbhuWDUSlzS/s8bQbvzq1deUtzOEuZ076ppxJzrTsHbIZd/Ze gUCX/35z67MDUWwXy8HgKeeI0BsztvR+UnPXEwiyhfDO6tiUCVjF8J+xEhMOMxjg X-Gm-Gg: AeBDiesKT5BuRACDf/qJNwK8WYJBLXEsCOZrT+i4fsEr0BtpMSYwFNyTFDcaHM6zNd7 m8jyaNQRPLgBiFCBsehfXALTn8wxHTuK4d3fO07scB4d5z5odk3eomr11AoEqy5VUCJz0x6iuws jfn92PGrXK6ZEpof46VYhGyvDERmW7HZ5C0KD5jTzvjpv+8mFRoanld2Sc23zyuJCOJjF5xhvYS SgSMDKcNUM0Ftd04gO+7QljrVVt4zgsy1VhBLNuDMb9isdBvlaBiAxITiYzdVE/Wf0/Gb8MsdAk 36kIuP9qV0DPlqLgsNytFUe2BmREuIsWono5QY5uSYABPnp7xe9s2uNaqI8X7vnBE/X+MZwWmtq 9dja18soktwtGabZWHvaZ95pj+NsF8ICuG757Pm2jZlibLk/uij0jKCuB6wA8S5zX1X348jMpb1 rgyGnKmJE/3wEba4SGO9Nb2OWSblXcqpKVQNqpn9kJJUnyjnjKxapQZXLfFr0mpYdRkakVRf5hr NGtPUNX X-Received: by 2002:a05:600c:154e:b0:488:9ed3:1492 with SMTP id 5b1f17b1804b1-488fb74fc02mr515636985e9.10.1777194409305; Sun, 26 Apr 2026 02:06:49 -0700 (PDT) Received: from [192.168.0.86] (84.123.230.95.dyn.user.ono.com. [84.123.230.95]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48a66533ff4sm73849665e9.6.2026.04.26.02.06.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Apr 2026 02:06:48 -0700 (PDT) Message-ID: <117efd72-5692-4844-bc4e-c553d03d393e@gmail.com> Date: Sun, 26 Apr 2026 11:06:47 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: TRAP: failed Assert("offsets[i] > offsets[i - 1]"), File: "tidstore.c" To: Masahiko Sawada Cc: PostgreSQL mailing lists References: <119bd418-1d7a-42c7-9270-86f3b6696399@gmail.com> <9dece148-3e1d-4b0f-b798-574988fe76fe@gmail.com> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 25/04/2026 01:23, Masahiko Sawada wrote: > On Wed, Apr 22, 2026 at 10:23 AM Andrei Lepikhov wrote: >> Both tools are experimental and not meant for core use; they are only used to >> trigger potential issues. In this case, I think the query picked a costly 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 means the join could return results in any order, but in practice, I have not seen this happen. > > I’m asking because if this scenario never occurs with the current > 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 little 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 sides are shuffled. Here is what I found: EXPLAIN of the successful execution (unnecessary details stripped): Insert on pg_temp.hideblocks (cost=1.21..1.66 rows=0 width=0) -> Subquery Scan on unnamed_subquery (cost=1.21..1.66 rows=5 width=8) Output: unnamed_subquery.do_set_block_offsets -> GroupAggregate (cost=1.21..1.61 rows=5 width=16) Output: do_set_block_offsets("*VALUES*".column1, (array_agg("*VALUES*_1".column1))::smallint[]), ... Group Key: "*VALUES*".column1 -> Sort (cost=1.21..1.27 rows=25 width=12) Output: "*VALUES*".column1, "*VALUES*_1".column1 Sort Key: "*VALUES*".column1 -> Nested Loop (cost=0.00..0.62 rows=25 width=12) Output: "*VALUES*".column1, "*VALUES*_1".column1 -> Values Scan on "*VALUES*" (cost=0.00..0.06 rows=5 width=8) Output: "*VALUES*".column1 -> Values Scan on "*VALUES*_1" (cost=0.00..0.06 rows=5 width=4) Output: "*VALUES*_1".column1 EXPLAIN that causes assertion: Insert on pg_temp.hideblocks (cost=1.03..1.48 rows=0 width=0) -> Subquery Scan on unnamed_subquery (cost=1.03..1.48 rows=5 width=8) Output: unnamed_subquery.do_set_block_offsets -> GroupAggregate (cost=1.03..1.43 rows=5 width=16) Output: do_set_block_offsets("*VALUES*".column1, (array_agg("*VALUES*_1".column1))::smallint[]),... Group Key: "*VALUES*".column1 -> Sort (cost=1.03..1.09 rows=25 width=12) Output: "*VALUES*".column1, "*VALUES*_1".column1 Sort Key: "*VALUES*".column1 -> Nested Loop (cost=0.00..0.45 rows=25 width=12) Output: "*VALUES*".column1, "*VALUES*_1".column1 -> Values Scan on "*VALUES*_1" (cost=0.00..0.06 rows=5 width=4) Output: "*VALUES*_1".column1 -> Materialize (cost=0.00..0.09 rows=5 width=8) Output: "*VALUES*".column1 -> Values Scan on "*VALUES*" (cost=0.00..0.06 rows=5 width=8) Output: "*VALUES*".column1 At the second case offsets have come to the aggregation without order that highlighted the issue. -- regards, Andrei Lepikhov, pgEdge