1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-08 11:42:09 +03:00

Disallow converting an inheritance child table to a view.

Generally, members of inheritance trees must be plain tables (or,
in more recent versions, foreign tables).  ALTER TABLE INHERIT
rejects creating an inheritance relationship that has a view at
either end.  When DefineQueryRewrite attempts to convert a relation
to a view, it already had checks prohibiting doing so for partitioning
parents or children as well as traditional-inheritance parents ...
but it neglected to check that a traditional-inheritance child wasn't
being converted.  Since the planner assumes that any inheritance
child is a table, this led to making plans that tried to do a physical
scan on a view, causing failures (or even crashes, in recent versions).

One could imagine trying to support such a case by expanding the view
normally, but since the rewriter runs before the planner does
inheritance expansion, it would take some very fundamental refactoring
to make that possible.  There are probably a lot of other parts of the
system that don't cope well with such a situation, too.  For now,
just forbid it.

Per bug #16856 from Yang Lin.  Back-patch to all supported branches.
(In versions before v10, this includes back-patching the portion of
commit 501ed02cf that added has_superclass().  Perhaps the lack of
that infrastructure partially explains the missing check.)

Discussion: https://postgr.es/m/16856-0363e05c6e1612fd@postgresql.org
This commit is contained in:
Tom Lane
2021-02-06 15:17:01 -05:00
parent f7400823c3
commit dd705a039f
4 changed files with 52 additions and 16 deletions

View File

@ -25,6 +25,7 @@
#include "catalog/heap.h"
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_inherits.h"
#include "catalog/pg_rewrite.h"
#include "catalog/storage.h"
#include "commands/policy.h"
@ -412,13 +413,14 @@ DefineQueryRewrite(const char *rulename,
* Are we converting a relation to a view?
*
* If so, check that the relation is empty because the storage for the
* relation is going to be deleted. Also insist that the rel not have
* any triggers, indexes, child tables, policies, or RLS enabled.
* (Note: these tests are too strict, because they will reject
* relations that once had such but don't anymore. But we don't
* really care, because this whole business of converting relations to
* views is just a kluge to allow dump/reload of views that
* participate in circular dependencies.)
* relation is going to be deleted. Also insist that the rel not be
* involved in partitioning, nor have any triggers, indexes, child or
* parent tables, RLS policies, or RLS enabled. (Note: some of these
* tests are too strict, because they will reject relations that once
* had such but don't anymore. But we don't really care, because this
* whole business of converting relations to views is just an obsolete
* kluge to allow dump/reload of views that participate in circular
* dependencies.)
*/
if (event_relation->rd_rel->relkind != RELKIND_VIEW &&
event_relation->rd_rel->relkind != RELKIND_MATVIEW)
@ -433,6 +435,9 @@ DefineQueryRewrite(const char *rulename,
errmsg("cannot convert partitioned table \"%s\" to a view",
RelationGetRelationName(event_relation))));
/* only case left: */
Assert(event_relation->rd_rel->relkind == RELKIND_RELATION);
if (event_relation->rd_rel->relispartition)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
@ -470,6 +475,12 @@ DefineQueryRewrite(const char *rulename,
errmsg("could not convert table \"%s\" to a view because it has child tables",
RelationGetRelationName(event_relation))));
if (has_superclass(RelationGetRelid(event_relation)))
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("could not convert table \"%s\" to a view because it has parent tables",
RelationGetRelationName(event_relation))));
if (event_relation->rd_rel->relrowsecurity)
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),