BUG #19686: Rolling back SET TABLESPACE + INSERT leads to index corruption
The following bug has been logged on the website:
Bug reference: 19686
Logged by: Alexander Lakhin
Email address: exclusion@gmail.com
PostgreSQL version: 19beta3
Operating system: Ubuntu 24.04
Description:
The following script:
rm -rf /tmp/ts
mkdir /tmp/ts
echo "
CREATE TABLESPACE ts LOCATION '/tmp/ts';
CREATE TABLE t (a int);
CREATE INDEX ON t(a);
BEGIN;
ALTER TABLE t SET TABLESPACE ts;
INSERT INTO t VALUES (0);
ROLLBACK;
INSERT INTO t VALUES (0);
INSERT INTO t SELECT i FROM generate_series(1, 4000) i;
" | psql
triggers:
2026-09-13 18:09:42.598 EEST|postgres|regression|6aa6bcb6.297fd6|LOG:
statement: INSERT INTO t SELECT i FROM generate_series(1, 4000) i;
TRAP: failed Assert("_bt_posting_valid(itup)"), File: "nbtdedup.c", Line:
900, PID: 2719702
ExceptionalCondition at assert.c:51:13
_bt_form_posting at nbtdedup.c:905:7
_bt_dedup_finish_pending at nbtdedup.c:584:11
_bt_dedup_pass at nbtdedup.c:173:15
_bt_delete_or_dedup_one_page at nbtinsert.c:2827:3
_bt_findinsertloc at nbtinsert.c:918:4
_bt_doinsert at nbtinsert.c:261:16
btinsert at nbtree.c:219:11
index_insert at indexam.c:231:9
The assertion failure is reproduced starting from 0d861bbb7.
The index corruption is also reproduced with gist:
echo "
CREATE TABLESPACE ts LOCATION '/tmp/ts';
CREATE TABLE t (a int4range);
CREATE INDEX ON t USING gist (a);
BEGIN;
ALTER TABLE t SET TABLESPACE ts;
INSERT INTO t VALUES (int4range(0, 5));
ROLLBACK;
INSERT INTO t VALUES (int4range(0, 5));
" | psql
echo "
SELECT * FROM t;
SET enable_seqscan = off;
SET enable_bitmapscan = off;
EXPLAIN SELECT * FROM t WHERE a && int4range(0, 100);
SELECT * FROM t WHERE a && int4range(0, 100);
" | psql
returns:
ROLLBACK
INSERT 0 1
a
-------
[0,5)
(1 row)
SET
SET
QUERY PLAN
-------------------------------------------------------------------------
Index Only Scan using t_a_idx on t (cost=0.14..40.39 rows=14 width=32)
Index Cond: (a && '[0,100)'::int4range)
(2 rows)
a
-------
[0,5)
[0,5)
(2 rows)
I started investigating.
The first thing I noticed is that we have no debugging on what happens
during
the SET TABLESPACE, I think that creating or deleting files deserves a line
in the log, so I will start by adding DEBUG1 around it.
Anyone else looking into this?
Regards,
Alexandre
On 2026-09-15 08:59, Alexandre Felipe wrote:
I started investigating.
...
Anyone else looking into this?
I looked into it too; here is what I found, in case it is useful.
The trigger is that ALTER TABLE ... SET TABLESPACE gives the heap a new
relfilenode while the table's indexes deliberately keep theirs. The two
then unwind differently on abort: the heap's new file is discarded, so
the heap TIDs used by rows inserted after the SET TABLESPACE are free
again, but the index entries for those rows were written to the
unchanged index files and survive. A later insert reuses one of those
freed TIDs, and the index is left with two entries pointing at the same
live heap tuple.
That fits your point that it is not really an nbtree problem: gist shows
the same duplicate through an index-only scan, and the _bt_posting_valid
assert from 0d861bbb7 only makes btree notice it. Dropping the insert,
or the SET TABLESPACE, or turning the insert into an update all avoid
it, which lines up with the TID-reuse explanation.
Attached is a patch for discussion. After the heap is moved,
ATExecSetTableSpace gives each of the table's indexes a new
relfilenumber by copying it within its own tablespace, so the indexes
share the heap's rollback: on abort the new heap and index files are
discarded together, and on commit they are kept together. It closes
both the btree and the gist case here, and make check-world passes.
I first tried reindexing the indexes instead, which also fixes it, but
on a 1M-row table with three indexes that made SET TABLESPACE roughly
20x slower (about 100 ms to 1.9 s); copying the files keeps it near 2x.
As far as I can tell an abort-time fix is not possible, so SET TABLESPACE
has to make the indexes safe up front, but whether this is the right way
to do it is a question for people who know this code better than I do.
Manu
Hi Manu,
I think alexandre already had a fix but in a different thread. Maybe
you folks can merge two threads together?
https://commitfest.postgresql.org/patch/7312/
Thanks,
Shihao
On 2026-09-27, Shihao Zhong wrote:
I think alexandre already had a fix but in a different thread. Maybe
you folks can merge two threads together?
https://commitfest.postgresql.org/patch/7312/
Thanks a lot for the pointer -- I hadn't come across #7312 when I
searched before posting, so I appreciate you connecting the two. I'll
take a proper look at Alexandre's patch and follow up on that thread so
the discussion stays in one place.
Manu
On Sun, Sep 13, 2026 at 04:00:01PM +0000, PG Bug reporting form wrote:
triggers:
2026-09-13 18:09:42.598 EEST|postgres|regression|6aa6bcb6.297fd6|LOG:
statement: INSERT INTO t SELECT i FROM generate_series(1, 4000) i;
TRAP: failed Assert("_bt_posting_valid(itup)"), File: "nbtdedup.c", Line:
900, PID: 2719702
ExceptionalCondition at assert.c:51:13
_bt_form_posting at nbtdedup.c:905:7
_bt_dedup_finish_pending at nbtdedup.c:584:11
_bt_dedup_pass at nbtdedup.c:173:15
_bt_delete_or_dedup_one_page at nbtinsert.c:2827:3
_bt_findinsertloc at nbtinsert.c:918:4
_bt_doinsert at nbtinsert.c:261:16
btinsert at nbtree.c:219:11
index_insert at indexam.c:231:9The assertion failure is reproduced starting from 0d861bbb7.
Err, Peter?
--
Michael
On Sun, Sep 27, 2026 at 4:35 PM Michael Paquier <michael@paquier.xyz> wrote:
The assertion failure is reproduced starting from 0d861bbb7.
Err, Peter?
The problem has nothing to do with nbtree deduplication -- Alexander
recreated the same problem with a GiST index.. It just so happens that
deduplication has assertions that tend to fail when the same TID is
stored multiple times (e.g., due to a recovery problem).
--
Peter Geoghegan