diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2019-02-17 12:37:31 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2019-02-17 12:37:31 -0500 |
commit | 4eca1905d34b34a48b80553262edc81048fd7dfe (patch) | |
tree | 08974db0992f59fd7775294948aad8786303b06d | |
parent | d43a1ff8f28c2895ac24326f0c8bbf2088f28cc5 (diff) | |
download | postgresql-4eca1905d34b34a48b80553262edc81048fd7dfe.tar.gz postgresql-4eca1905d34b34a48b80553262edc81048fd7dfe.zip |
Fix CREATE VIEW to allow zero-column views.
We should logically have allowed this case when we allowed zero-column
tables, but it was overlooked.
Although this might be thought a feature addition, it's really a bug
fix, because it was possible to create a zero-column view via
the convert-table-to-view code path, and then you'd have a situation
where dump/reload would fail. Hence, back-patch to all supported
branches.
Arrange the added test cases to provide coverage of the related
pg_dump code paths (since these views will be dumped and reloaded
during the pg_upgrade regression test). I also made them test
the case where pg_dump has to postpone the view rule into post-data,
which disturbingly had no regression coverage before.
Report and patch by Ashutosh Sharma (test case by me)
Discussion: https://postgr.es/m/CAE9k0PkmHdeSaeZt2ujnb_cKucmK3sDDceDzw7+d5UZoNJPYOg@mail.gmail.com
-rw-r--r-- | src/backend/commands/view.c | 5 | ||||
-rw-r--r-- | src/test/regress/expected/create_view.out | 10 | ||||
-rw-r--r-- | src/test/regress/expected/rules.out | 8 | ||||
-rw-r--r-- | src/test/regress/expected/sanity_check.out | 1 | ||||
-rw-r--r-- | src/test/regress/sql/create_view.sql | 12 |
5 files changed, 31 insertions, 5 deletions
diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c index 2f0ba12d222..2510b9c3d25 100644 --- a/src/backend/commands/view.c +++ b/src/backend/commands/view.c @@ -111,11 +111,6 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, } } - if (attrList == NIL) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("view must have at least one column"))); - /* * Look up, check permissions on, and lock the creation namespace; also * check for a preexisting view with the same name. This will also set diff --git a/src/test/regress/expected/create_view.out b/src/test/regress/expected/create_view.out index 141fc6da62a..340e5a1c1a4 100644 --- a/src/test/regress/expected/create_view.out +++ b/src/test/regress/expected/create_view.out @@ -20,6 +20,16 @@ COMMENT ON VIEW noview IS 'no view'; ERROR: relation "noview" does not exist COMMENT ON VIEW toyemp IS 'is a view'; COMMENT ON VIEW toyemp IS NULL; +-- These views are left around mainly to exercise special cases in pg_dump. +CREATE TABLE view_base_table (key int PRIMARY KEY, data varchar(20)); +CREATE VIEW key_dependent_view AS + SELECT * FROM view_base_table GROUP BY key; +ALTER TABLE view_base_table DROP CONSTRAINT view_base_table_pkey; -- fails +ERROR: cannot drop constraint view_base_table_pkey on table view_base_table because other objects depend on it +DETAIL: view key_dependent_view depends on constraint view_base_table_pkey on table view_base_table +HINT: Use DROP ... CASCADE to drop the dependent objects too. +CREATE VIEW key_dependent_view_no_cols AS + SELECT FROM view_base_table GROUP BY key HAVING length(data) > 0; -- -- CREATE OR REPLACE VIEW -- diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 078129f251b..7e2b19408ed 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -1289,6 +1289,14 @@ iexit| SELECT ih.name, FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath); +key_dependent_view| SELECT view_base_table.key, + view_base_table.data + FROM view_base_table + GROUP BY view_base_table.key; +key_dependent_view_no_cols| SELECT + FROM view_base_table + GROUP BY view_base_table.key + HAVING (length((view_base_table.data)::text) > 0); mvtest_tv| SELECT mvtest_t.type, sum(mvtest_t.amt) AS totamt FROM mvtest_t diff --git a/src/test/regress/expected/sanity_check.out b/src/test/regress/expected/sanity_check.out index 66957706a00..abc765493ba 100644 --- a/src/test/regress/expected/sanity_check.out +++ b/src/test/regress/expected/sanity_check.out @@ -205,6 +205,7 @@ timestamp_tbl|f timestamptz_tbl|f timetz_tbl|f varchar_tbl|f +view_base_table|t -- restore normal output mode \a\t -- diff --git a/src/test/regress/sql/create_view.sql b/src/test/regress/sql/create_view.sql index 9480030005e..845505caa65 100644 --- a/src/test/regress/sql/create_view.sql +++ b/src/test/regress/sql/create_view.sql @@ -24,6 +24,18 @@ COMMENT ON VIEW noview IS 'no view'; COMMENT ON VIEW toyemp IS 'is a view'; COMMENT ON VIEW toyemp IS NULL; +-- These views are left around mainly to exercise special cases in pg_dump. + +CREATE TABLE view_base_table (key int PRIMARY KEY, data varchar(20)); + +CREATE VIEW key_dependent_view AS + SELECT * FROM view_base_table GROUP BY key; + +ALTER TABLE view_base_table DROP CONSTRAINT view_base_table_pkey; -- fails + +CREATE VIEW key_dependent_view_no_cols AS + SELECT FROM view_base_table GROUP BY key HAVING length(data) > 0; + -- -- CREATE OR REPLACE VIEW -- |