Thread: BUG #18256: spgGetCache() attempts to read to read invalid file for index attached to partitioned table

The following bug has been logged on the website:

Bug reference:      18256
Logged by:          Alexander Lakhin
Email address:      exclusion@gmail.com
PostgreSQL version: 16.1
Operating system:   Ubuntu 22.04
Description:

[ a kind of follow-up to bug #18246 ]

The following query:
CREATE TABLE t(a int, b text) PARTITION BY RANGE (a);
CREATE INDEX idx ON t USING spgist(b);
SELECT * FROM t;

produces an unexpected error:
ERROR:  could not open file "pg_tblspc/0/PG_16_202307071/0/0": No such file
or directory

(On master, thanks to commit 049ef3398, which added an Assert in smgr.c,
that assertion fails:
...
#5  0x000055fcb19630d3 in ExceptionalCondition (conditionName=0x55fcb1b573f8
"RelFileNumberIsValid(rlocator.relNumber)", fileName=0x55fcb1b573ec
"smgr.c", lineNumber=159) at assert.c:66
#6  0x000055fcb1770731 in smgropen (rlocator=..., backend=-1) at
smgr.c:159
#7  0x000055fcb171e0a7 in RelationGetSmgr (rel=0x7ff29e501e88) at
../../../../src/include/utils/rel.h:575
#8  0x000055fcb171f0ed in ReadBufferExtended (reln=0x7ff29e501e88,
forkNum=MAIN_FORKNUM, blockNum=0, mode=RBM_NORMAL, strategy=0x0) at
bufmgr.c:803
#9  0x000055fcb171efce in ReadBuffer (reln=0x7ff29e501e88, blockNum=0) at
bufmgr.c:737
#10 0x000055fcb1294810 in spgGetCache (index=0x7ff29e501e88) at
spgutils.c:261
#11 0x000055fcb1291aff in spgcanreturn (index=0x7ff29e501e88, attno=1) at
spgscan.c:1092
#12 0x000055fcb125a3ef in index_can_return (indexRelation=0x7ff29e501e88,
attno=1) at indexam.c:762
#13 0x000055fcb166154b in get_relation_info (root=0x55fcb27dd928,
relationObjectId=16385, inhparent=false, rel=0x55fcb2700938) at
plancat.c:272
#14 0x000055fcb166956e in build_simple_rel (root=0x55fcb27dd928, relid=1,
parent=0x0) at relnode.c:339
...
)

Reproduced on REL_16_STABLE (starting from 3c569049b) .. master.


PG Bug reporting form <noreply@postgresql.org> writes:
> The following query:
> CREATE TABLE t(a int, b text) PARTITION BY RANGE (a);
> CREATE INDEX idx ON t USING spgist(b);
> SELECT * FROM t;

> produces an unexpected error:
> ERROR:  could not open file "pg_tblspc/0/PG_16_202307071/0/0": No such file
> or directory

Thanks for the report.  It looks like we could work around this by
teaching spgGetCache to not try to get the lastUsedPages info for
a partitioned index.  (The rest of what it does is still valid for
partitioned indexes.)  I wonder though if there are any other similar
bugs --- obviously, non-btree partitioned indexes haven't been
tested sufficiently.

> Reproduced on REL_16_STABLE (starting from 3c569049b) .. master.

It should be possible to exhibit similar problems long before 3c569049b,
I would guess.

            regards, tom lane