Prevent table partitions from being turned into views.
authorDean Rasheed <[email protected]>
Wed, 21 Jun 2017 09:43:17 +0000 (10:43 +0100)
committerDean Rasheed <[email protected]>
Wed, 21 Jun 2017 09:43:17 +0000 (10:43 +0100)
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

src/backend/rewrite/rewriteDefine.c
src/test/regress/expected/rules.out
src/test/regress/sql/rules.sql

index fd3768de171bbd4fade9ad16e9f637d57321fd99..4213bafa27096daff94a04a3abed97258c4fb5e4 100644 (file)
@@ -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)
index 912360d70af0bf63796666ce1b1b2da6970b86b6..2e42b9ec05fab06496a84ab3079029f1a55393ee 100644 (file)
@@ -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
 --
index aada114ab2b821839feeb404e94336c5db66c1b1..38751bb8818889d30d3b3e965a88ce185660c2a6 100644 (file)
@@ -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
 --