diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2021-02-06 15:17:02 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2021-02-06 15:17:02 -0500 |
commit | ad85e5efa1bf6d534c1c519598c2beaaaa5eeb2f (patch) | |
tree | c49f016ddb4b8ebe6e84f70f6e0fbdfae8ceeb35 | |
parent | c06632e48b3537f9a062f6c4686a730625a429fd (diff) | |
download | postgresql-ad85e5efa1bf6d534c1c519598c2beaaaa5eeb2f.tar.gz postgresql-ad85e5efa1bf6d534c1c519598c2beaaaa5eeb2f.zip |
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
-rw-r--r-- | src/backend/catalog/pg_inherits.c | 8 | ||||
-rw-r--r-- | src/backend/rewrite/rewriteDefine.c | 25 | ||||
-rw-r--r-- | src/test/regress/expected/rules.out | 15 | ||||
-rw-r--r-- | src/test/regress/sql/rules.sql | 16 |
4 files changed, 50 insertions, 14 deletions
diff --git a/src/backend/catalog/pg_inherits.c b/src/backend/catalog/pg_inherits.c index 245a374fc91..929189ca7b7 100644 --- a/src/backend/catalog/pg_inherits.c +++ b/src/backend/catalog/pg_inherits.c @@ -274,9 +274,11 @@ has_subclass(Oid relationId) } /* - * has_superclass - does this relation inherit from another? The caller - * should hold a lock on the given relation so that it can't be concurrently - * added to or removed from an inheritance hierarchy. + * has_superclass - does this relation inherit from another? + * + * Unlike has_subclass, this can be relied on to give an accurate answer. + * However, the caller must hold a lock on the given relation so that it + * can't be concurrently added to or removed from an inheritance hierarchy. */ bool has_superclass(Oid relationId) diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c index f9ed8e2be4c..d218993f61f 100644 --- a/src/backend/rewrite/rewriteDefine.c +++ b/src/backend/rewrite/rewriteDefine.c @@ -25,6 +25,7 @@ #include "catalog/indexing.h" #include "catalog/namespace.h" #include "catalog/objectaccess.h" +#include "catalog/pg_inherits_fn.h" #include "catalog/pg_rewrite.h" #include "catalog/storage.h" #include "commands/policy.h" @@ -408,13 +409,14 @@ DefineQueryRewrite(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) @@ -428,6 +430,9 @@ DefineQueryRewrite(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), @@ -463,6 +468,12 @@ DefineQueryRewrite(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), diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index e10c6cceb4a..5bb35b5fc8c 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2575,16 +2575,27 @@ select reltoastrelid, relkind, relfrozenxid (1 row) drop view fooview; --- trying to convert a partitioned table to view is not allowed +-- cannot convert an inheritance parent or child to a view, though +create table fooview (x int, y text); +create table fooview_child () inherits (fooview); +create rule "_RETURN" as on select to fooview do instead + select 1 as x, 'aaa'::text as y; +ERROR: could not convert table "fooview" to a view because it has child tables +create rule "_RETURN" as on select to fooview_child do instead + select 1 as x, 'aaa'::text as y; +ERROR: could not convert table "fooview_child" to a view because it has parent tables +drop table fooview cascade; +NOTICE: drop cascades to table fooview_child +-- likewise, converting a partitioned table or partition to view is not allowed create table fooview (x int, y text) partition by list (x); create rule "_RETURN" as on select to fooview do instead select 1 as x, 'aaa'::text as y; ERROR: cannot convert partitioned table "fooview" to a view --- nor can one convert a partition to view create table fooview_part partition of fooview for values in (1); create rule "_RETURN" as on select to fooview_part do instead select 1 as x, 'aaa'::text as y; ERROR: cannot convert partition "fooview_part" to a view +drop table fooview; -- -- check for planner problems with complex inherited UPDATES -- diff --git a/src/test/regress/sql/rules.sql b/src/test/regress/sql/rules.sql index 291748d60c1..690d3e33110 100644 --- a/src/test/regress/sql/rules.sql +++ b/src/test/regress/sql/rules.sql @@ -898,16 +898,28 @@ select reltoastrelid, relkind, relfrozenxid drop view fooview; --- trying to convert a partitioned table to view is not allowed +-- cannot convert an inheritance parent or child to a view, though +create table fooview (x int, y text); +create table fooview_child () inherits (fooview); + +create rule "_RETURN" as on select to fooview do instead + select 1 as x, 'aaa'::text as y; +create rule "_RETURN" as on select to fooview_child do instead + select 1 as x, 'aaa'::text as y; + +drop table fooview cascade; + +-- likewise, converting a partitioned table or partition to view is not allowed create table fooview (x int, y text) partition by list (x); create rule "_RETURN" as on select to fooview do instead select 1 as x, 'aaa'::text as y; --- nor can one convert a partition to view create table fooview_part partition of fooview for values in (1); create rule "_RETURN" as on select to fooview_part do instead select 1 as x, 'aaa'::text as y; +drop table fooview; + -- -- check for planner problems with complex inherited UPDATES -- |