Skip to content

Commit 1d5fede

Browse files
committed
Code review for c94e694.
validateCheckConstraint() shouldn't try to access the storage for a partitioned table, because it no longer has any. Creating a _RETURN table on a partitioned table shouldn't be allowed, both because there's no value in it and because trying to do so would involve a validation scan against its nonexistent storage. Amit Langote, reviewed by Tom Lane. Regression test outputs updated to pass by me. Discussion: http://postgr.es/m/e5c3cbd3-1551-d6f8-c9e2-51777d632fd2@lab.ntt.co.jp
1 parent b935eb7 commit 1d5fede

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

src/backend/commands/tablecmds.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -8120,8 +8120,12 @@ validateCheckConstraint(Relation rel, HeapTuple constrtup)
81208120
bool isnull;
81218121
Snapshot snapshot;
81228122

8123-
/* VALIDATE CONSTRAINT is a no-op for foreign tables */
8124-
if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
8123+
/*
8124+
* VALIDATE CONSTRAINT is a no-op for foreign tables and partitioned
8125+
* tables.
8126+
*/
8127+
if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE ||
8128+
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
81258129
return;
81268130

81278131
constrForm = (Form_pg_constraint) GETSTRUCT(constrtup);

src/backend/rewrite/rewriteDefine.c

+6
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ DefineQueryRewrite(char *rulename,
422422
HeapScanDesc scanDesc;
423423
Snapshot snapshot;
424424

425+
if (event_relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
426+
ereport(ERROR,
427+
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
428+
errmsg("could not convert partitioned table \"%s\" to a view",
429+
RelationGetRelationName(event_relation))));
430+
425431
snapshot = RegisterSnapshot(GetLatestSnapshot());
426432
scanDesc = heap_beginscan(event_relation, snapshot, 0, NULL);
427433
if (heap_getnext(scanDesc, ForwardScanDirection) != NULL)

src/test/regress/expected/alter_table.out

+6
Original file line numberDiff line numberDiff line change
@@ -3372,3 +3372,9 @@ ERROR: partition constraint is violated by some row
33723372
-- cleanup
33733373
drop table p;
33743374
drop table p1;
3375+
-- validate constraint on partitioned tables should only scan leaf partitions
3376+
create table parted_validate_test (a int) partition by list (a);
3377+
create table parted_validate_test_1 partition of parted_validate_test for values in (0, 1);
3378+
alter table parted_validate_test add constraint parted_validate_test_chka check (a > 0) not valid;
3379+
alter table parted_validate_test validate constraint parted_validate_test_chka;
3380+
drop table parted_validate_test;

src/test/regress/expected/rules.out

+5
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,11 @@ select reltoastrelid, relkind, relfrozenxid
25742574
(1 row)
25752575

25762576
drop view fooview;
2577+
-- trying to convert a partitioned table to view is not allowed
2578+
create table fooview (x int, y text) partition by list (x);
2579+
create rule "_RETURN" as on select to fooview do instead
2580+
select 1 as x, 'aaa'::text as y;
2581+
ERROR: could not convert partitioned table "fooview" to a view
25772582
--
25782583
-- check for planner problems with complex inherited UPDATES
25792584
--

src/test/regress/sql/alter_table.sql

+7
Original file line numberDiff line numberDiff line change
@@ -2228,3 +2228,10 @@ alter table p attach partition p1 for values from (1, 2) to (1, 10);
22282228
-- cleanup
22292229
drop table p;
22302230
drop table p1;
2231+
2232+
-- validate constraint on partitioned tables should only scan leaf partitions
2233+
create table parted_validate_test (a int) partition by list (a);
2234+
create table parted_validate_test_1 partition of parted_validate_test for values in (0, 1);
2235+
alter table parted_validate_test add constraint parted_validate_test_chka check (a > 0) not valid;
2236+
alter table parted_validate_test validate constraint parted_validate_test_chka;
2237+
drop table parted_validate_test;

src/test/regress/sql/rules.sql

+5
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,11 @@ select reltoastrelid, relkind, relfrozenxid
898898

899899
drop view fooview;
900900

901+
-- trying to convert a partitioned table to view is not allowed
902+
create table fooview (x int, y text) partition by list (x);
903+
create rule "_RETURN" as on select to fooview do instead
904+
select 1 as x, 'aaa'::text as y;
905+
901906
--
902907
-- check for planner problems with complex inherited UPDATES
903908
--

0 commit comments

Comments
 (0)