Skip to content

Commit a32ca78

Browse files
committed
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
1 parent 290e3b7 commit a32ca78

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/backend/commands/view.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace,
111111
}
112112
}
113113

114-
if (attrList == NIL)
115-
ereport(ERROR,
116-
(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
117-
errmsg("view must have at least one column")));
118-
119114
/*
120115
* Look up, check permissions on, and lock the creation namespace; also
121116
* check for a preexisting view with the same name. This will also set

src/test/regress/expected/create_view.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ COMMENT ON VIEW noview IS 'no view';
2020
ERROR: relation "noview" does not exist
2121
COMMENT ON VIEW toyemp IS 'is a view';
2222
COMMENT ON VIEW toyemp IS NULL;
23+
-- These views are left around mainly to exercise special cases in pg_dump.
24+
CREATE TABLE view_base_table (key int PRIMARY KEY, data varchar(20));
25+
CREATE VIEW key_dependent_view AS
26+
SELECT * FROM view_base_table GROUP BY key;
27+
ALTER TABLE view_base_table DROP CONSTRAINT view_base_table_pkey; -- fails
28+
ERROR: cannot drop constraint view_base_table_pkey on table view_base_table because other objects depend on it
29+
DETAIL: view key_dependent_view depends on constraint view_base_table_pkey on table view_base_table
30+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
31+
CREATE VIEW key_dependent_view_no_cols AS
32+
SELECT FROM view_base_table GROUP BY key HAVING length(data) > 0;
2333
--
2434
-- CREATE OR REPLACE VIEW
2535
--

src/test/regress/expected/rules.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,14 @@ iexit| SELECT ih.name,
12891289
FROM ihighway ih,
12901290
ramp r
12911291
WHERE (ih.thepath ## r.thepath);
1292+
key_dependent_view| SELECT view_base_table.key,
1293+
view_base_table.data
1294+
FROM view_base_table
1295+
GROUP BY view_base_table.key;
1296+
key_dependent_view_no_cols| SELECT
1297+
FROM view_base_table
1298+
GROUP BY view_base_table.key
1299+
HAVING (length((view_base_table.data)::text) > 0);
12921300
mvtest_tv| SELECT mvtest_t.type,
12931301
sum(mvtest_t.amt) AS totamt
12941302
FROM mvtest_t

src/test/regress/expected/sanity_check.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ timestamp_tbl|f
205205
timestamptz_tbl|f
206206
timetz_tbl|f
207207
varchar_tbl|f
208+
view_base_table|t
208209
-- restore normal output mode
209210
\a\t
210211
--

src/test/regress/sql/create_view.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ COMMENT ON VIEW noview IS 'no view';
2424
COMMENT ON VIEW toyemp IS 'is a view';
2525
COMMENT ON VIEW toyemp IS NULL;
2626

27+
-- These views are left around mainly to exercise special cases in pg_dump.
28+
29+
CREATE TABLE view_base_table (key int PRIMARY KEY, data varchar(20));
30+
31+
CREATE VIEW key_dependent_view AS
32+
SELECT * FROM view_base_table GROUP BY key;
33+
34+
ALTER TABLE view_base_table DROP CONSTRAINT view_base_table_pkey; -- fails
35+
36+
CREATE VIEW key_dependent_view_no_cols AS
37+
SELECT FROM view_base_table GROUP BY key HAVING length(data) > 0;
38+
2739
--
2840
-- CREATE OR REPLACE VIEW
2941
--

0 commit comments

Comments
 (0)