diff options
author | Tom Lane | 2022-10-17 15:35:23 +0000 |
---|---|---|
committer | Tom Lane | 2022-10-17 15:35:23 +0000 |
commit | eec34661182db7b79a6b56c551ac1d79ef3e1f89 (patch) | |
tree | 9154731e2076f6339efd602e82ab8644dc3ab705 | |
parent | c68a1839902daeb42cf1ebc89edfdd91c00e5091 (diff) |
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/[email protected]
-rw-r--r-- | src/backend/optimizer/util/plancat.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c index 6d5718ee4c..9defe37836 100644 --- a/src/backend/optimizer/util/plancat.c +++ b/src/backend/optimizer/util/plancat.c @@ -127,6 +127,24 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, */ relation = table_open(relationObjectId, NoLock); + /* + * Relations without a table AM can be used in a query only if they are of + * special-cased relkinds. This check prevents us from crashing later if, + * for example, a view's ON SELECT rule has gone missing. Note that + * table_open() already rejected indexes and composite types; spell the + * error the same way it does. + */ + if (!relation->rd_tableam) + { + if (!(relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE || + relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("cannot open relation \"%s\"", + RelationGetRelationName(relation)), + errdetail_relkind_not_supported(relation->rd_rel->relkind))); + } + /* Temporary and unlogged relations are inaccessible during recovery. */ if (!RelationIsPermanent(relation) && RecoveryInProgress()) ereport(ERROR, |