Check syscache result in AlterStatistics
authorTomas Vondra <tomas.vondra@postgresql.org>
Sun, 23 Jan 2022 01:49:41 +0000 (02:49 +0100)
committerTomas Vondra <tomas.vondra@postgresql.org>
Sun, 23 Jan 2022 02:16:31 +0000 (03:16 +0100)
The syscache lookup may return NULL even for valid OID, for example due
to a concurrent DROP STATISTICS, so a HeapTupleIsValid is necessary.
Without it, it may fail with a segfault.

Reported by Alexander Lakhin, patch by me. Backpatch to 13, where ALTER
STATISTICS ... SET STATISTICS was introduced.

Backpatch-through: 13
Discussion: https://postgr.es/m/17372-bf3b6e947e35ae77%40postgresql.org

src/backend/commands/statscmds.c

index f7419b8f562abcc8d1d5290e42904b4516d145ab..54a190722dfc8af7a9f065dcabb6e09fa7b61a25 100644 (file)
@@ -657,6 +657,8 @@ AlterStatistics(AlterStatsStmt *stmt)
    rel = table_open(StatisticExtRelationId, RowExclusiveLock);
 
    oldtup = SearchSysCache1(STATEXTOID, ObjectIdGetDatum(stxoid));
+   if (!HeapTupleIsValid(oldtup))
+       elog(ERROR, "cache lookup failed for extended statistics object %u", stxoid);
 
    /* Must be owner of the existing statistics object */
    if (!pg_statistics_object_ownercheck(stxoid, GetUserId()))