Skip to content

Commit 2e33269

Browse files
committed
Guard against table-AM-less relations in planner.
The executor will dump core if it's asked to execute a seqscan on a relation having no table AM, such as a view. While that shouldn't really happen, it's possible to get there via catalog corruption, such as a missing ON SELECT rule. It seems worth installing a defense against that. There are multiple plausible places for such a defense, but I picked the planner's get_relation_info(). Per discussion of bug #17646 from Kui Liu. Back-patch to v12 where the tableam APIs were introduced; in older versions you won't get a SIGSEGV, so it seems less pressing. Discussion: https://postgr.es/m/17646-70c93cfa40365776@postgresql.org
1 parent 9ebcb5f commit 2e33269

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/backend/optimizer/util/plancat.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
126126
*/
127127
relation = table_open(relationObjectId, NoLock);
128128

129+
/*
130+
* Relations without a table AM can be used in a query only if they are of
131+
* special-cased relkinds. This check prevents us from crashing later if,
132+
* for example, a view's ON SELECT rule has gone missing. Note that
133+
* table_open() already rejected indexes and composite types; spell the
134+
* error the same way it does.
135+
*/
136+
if (!relation->rd_tableam)
137+
{
138+
if (!(relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE ||
139+
relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE))
140+
ereport(ERROR,
141+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
142+
errmsg("cannot open relation \"%s\"",
143+
RelationGetRelationName(relation)),
144+
errdetail_relkind_not_supported(relation->rd_rel->relkind)));
145+
}
146+
129147
/* Temporary and unlogged relations are inaccessible during recovery. */
130148
if (!RelationIsPermanent(relation) && RecoveryInProgress())
131149
ereport(ERROR,

0 commit comments

Comments
 (0)