public inbox for [email protected]
help / color / mirror / Atom feedFrom: Andrei Lepikhov <[email protected]>
To: PostgreSQL mailing lists <[email protected]>
To: Masahiko Sawada <[email protected]>
Subject: TRAP: failed Assert("offsets[i] > offsets[i - 1]"), File: "tidstore.c"
Date: Wed, 15 Apr 2026 14:48:35 +0200
Message-ID: <[email protected]> (raw)
Hi,
While experimenting with query plans, I periodically see test_tidstore
fail on the assertion in TidStoreSetBlockOffsets().
The cause is that the harness function do_set_block_offsets() forwards
the SQL array straight to TidStoreSetBlockOffsets(), which has an
explicit contract:
"The offset numbers 'offsets' must be sorted in ascending order."
array_agg() without ORDER BY gives no such guarantee, and plan shapes
that reshuffle the input can deliver the offsets out of order and trip
the Assert.
The issue is minor and doesn't expose any underlying bug, but it is
still worth fixing: it removes a source of flaky test runs and makes
life easier for extension developers who reuse the same pattern.
Patch attached.
--
regards, Andrei Lepikhov,
pgEdge
From 99c9abb3b751ec3603b6ad8248fb94ec529d2932 Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <[email protected]>
Date: Wed, 15 Apr 2026 14:41:51 +0200
Subject: [PATCH v0] Sort offsets in test_tidstore's do_set_block_offsets().
TidStoreSetBlockOffsets() requires its offsets[] argument to be
strictly ascending, and asserts the precondition.
The test harness was forwarding the OffsetNumber array straight from
SQL, which works only because the current planner happens to present
array_agg() input in the order the VALUES clause was written. That
ordering is not a SQL guarantee: array_agg() without ORDER BY is
unordered by specification, and any plan change that reshuffles the
Cartesian product used in test_tidstore.sql -- parallel aggregation,
a different join order, or a randomised planner -- can deliver the
offsets out of order and cause the Assert.
Fix by sorting offs in place inside do_set_block_offsets() before
calling TidStoreSetBlockOffsets(). Duplicates are intentionally left to fail
the strict-inequality Assert.
---
src/test/modules/test_tidstore/test_tidstore.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/src/test/modules/test_tidstore/test_tidstore.c b/src/test/modules/test_tidstore/test_tidstore.c
index c9a035fa494..bfc40bce24c 100644
--- a/src/test/modules/test_tidstore/test_tidstore.c
+++ b/src/test/modules/test_tidstore/test_tidstore.c
@@ -75,6 +75,19 @@ itemptr_cmp(const void *left, const void *right)
return 0;
}
+static int
+offsetnumber_cmp(const void *a, const void *b)
+{
+ OffsetNumber l = *(const OffsetNumber *) a;
+ OffsetNumber r = *(const OffsetNumber *) b;
+
+ if (l < r)
+ return -1;
+ else if (l > r)
+ return 1;
+ return 0;
+}
+
/*
* Create a TidStore. If shared is false, the tidstore is created
* on TopMemoryContext, otherwise on DSA. Although the tidstore
@@ -178,6 +191,9 @@ do_set_block_offsets(PG_FUNCTION_ARGS)
noffs = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta));
offs = ((OffsetNumber *) ARR_DATA_PTR(ta));
+ /* TidStoreSetBlockOffsets() requires offsets to be strictly ascending. */
+ qsort(offs, noffs, sizeof(OffsetNumber), offsetnumber_cmp);
+
/* Set TIDs in the store */
TidStoreLockExclusive(tidstore);
TidStoreSetBlockOffsets(tidstore, blkno, offs, noffs);
--
2.53.0
Attachments:
[text/plain] v0-0001-Sort-offsets-in-test_tidstore-s-do_set_block_offs.patch (2.2K, 2-v0-0001-Sort-offsets-in-test_tidstore-s-do_set_block_offs.patch)
download | inline diff:
From 99c9abb3b751ec3603b6ad8248fb94ec529d2932 Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <[email protected]>
Date: Wed, 15 Apr 2026 14:41:51 +0200
Subject: [PATCH v0] Sort offsets in test_tidstore's do_set_block_offsets().
TidStoreSetBlockOffsets() requires its offsets[] argument to be
strictly ascending, and asserts the precondition.
The test harness was forwarding the OffsetNumber array straight from
SQL, which works only because the current planner happens to present
array_agg() input in the order the VALUES clause was written. That
ordering is not a SQL guarantee: array_agg() without ORDER BY is
unordered by specification, and any plan change that reshuffles the
Cartesian product used in test_tidstore.sql -- parallel aggregation,
a different join order, or a randomised planner -- can deliver the
offsets out of order and cause the Assert.
Fix by sorting offs in place inside do_set_block_offsets() before
calling TidStoreSetBlockOffsets(). Duplicates are intentionally left to fail
the strict-inequality Assert.
---
src/test/modules/test_tidstore/test_tidstore.c | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/src/test/modules/test_tidstore/test_tidstore.c b/src/test/modules/test_tidstore/test_tidstore.c
index c9a035fa494..bfc40bce24c 100644
--- a/src/test/modules/test_tidstore/test_tidstore.c
+++ b/src/test/modules/test_tidstore/test_tidstore.c
@@ -75,6 +75,19 @@ itemptr_cmp(const void *left, const void *right)
return 0;
}
+static int
+offsetnumber_cmp(const void *a, const void *b)
+{
+ OffsetNumber l = *(const OffsetNumber *) a;
+ OffsetNumber r = *(const OffsetNumber *) b;
+
+ if (l < r)
+ return -1;
+ else if (l > r)
+ return 1;
+ return 0;
+}
+
/*
* Create a TidStore. If shared is false, the tidstore is created
* on TopMemoryContext, otherwise on DSA. Although the tidstore
@@ -178,6 +191,9 @@ do_set_block_offsets(PG_FUNCTION_ARGS)
noffs = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta));
offs = ((OffsetNumber *) ARR_DATA_PTR(ta));
+ /* TidStoreSetBlockOffsets() requires offsets to be strictly ascending. */
+ qsort(offs, noffs, sizeof(OffsetNumber), offsetnumber_cmp);
+
/* Set TIDs in the store */
TidStoreLockExclusive(tidstore);
TidStoreSetBlockOffsets(tidstore, blkno, offs, noffs);
--
2.53.0
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: TRAP: failed Assert("offsets[i] > offsets[i - 1]"), File: "tidstore.c"
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox