From: Dean Rasheed Date: Wed, 21 Jun 2017 09:43:17 +0000 (+0100) Subject: Prevent table partitions from being turned into views. X-Git-Tag: REL_10_BETA2~103 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bcbf392ec84362040faf72daad22c647c74e2b2a;p=postgresql Prevent table partitions from being turned into views. A table partition must be a table, not a view, so don't allow a "_RETURN" rule to be added that would convert an existing table partition into a view. Amit Langote Discussion: https://postgr.es/m/CAEZATCVzFcAjZwC1bTFvJ09skB_sgkF4SwPKMywev-XTnimp9Q%40mail.gmail.com --- diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c index fd3768de17..4213bafa27 100644 --- a/src/backend/rewrite/rewriteDefine.c +++ b/src/backend/rewrite/rewriteDefine.c @@ -428,6 +428,12 @@ DefineQueryRewrite(char *rulename, errmsg("could not convert partitioned table \"%s\" to a view", RelationGetRelationName(event_relation)))); + if (event_relation->rd_rel->relispartition) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("could not convert partition \"%s\" to a view", + RelationGetRelationName(event_relation)))); + snapshot = RegisterSnapshot(GetLatestSnapshot()); scanDesc = heap_beginscan(event_relation, snapshot, 0, NULL); if (heap_getnext(scanDesc, ForwardScanDirection) != NULL) diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 912360d70a..2e42b9ec05 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -2572,6 +2572,11 @@ 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: could not 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: could not convert partition "fooview_part" to a view -- -- 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 aada114ab2..38751bb881 100644 --- a/src/test/regress/sql/rules.sql +++ b/src/test/regress/sql/rules.sql @@ -903,6 +903,11 @@ 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; + -- -- check for planner problems with complex inherited UPDATES --