]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add some tests for CREATE OR REPLACE VIEW with column additions
authorMichael Paquier <michael@paquier.xyz>
Wed, 4 Mar 2026 00:55:58 +0000 (09:55 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 4 Mar 2026 00:55:58 +0000 (09:55 +0900)
When working on an already-defined view with matching attributes, CREATE
OR REPLACE VIEW would internally generate an ALTER TABLE command with a
set of AT_AddColumnToView sub-commands, one for each attribute added.

Such a command is stored in event triggers twice:
- Once as a simple command.
- Once as an ALTER TABLE command, as it has sub-commands.

There was no test coverage to track this command pattern in terms of
event triggers and DDL deparsing:
- For the test module test_ddl_deparse, two command notices are issued.
- For event triggers, a CREATE VIEW command is logged twice, which may
look a bit weird first, but again this maps with the internal behavior
of how the commands are built, and how the event trigger code reacts in
terms of commands gathered.

While on it, this adds a test for CREATE SCHEMA with a CREATE VIEW
command embedded in it, case supported by the grammar but not covered
yet.

This hole in the test coverage has been found while digging into what
would be a similar behavior for sequences if adding attributes to them
with ALTER TABLE variants, after the initial relation creation.

Discussion: https://postgr.es/m/aaFG9bqkEn0RhLJG@paquier.xyz

src/test/modules/test_ddl_deparse/expected/create_view.out
src/test/modules/test_ddl_deparse/sql/create_view.sql
src/test/regress/expected/event_trigger.out
src/test/regress/sql/event_trigger.sql

index 4ae0f4978ec1ac7bb1db357d1f0c2ea5b41a6da8..0a8cc4627a8b295f48bbc9da55e04fc58eb0eaa8 100644 (file)
@@ -12,6 +12,12 @@ NOTICE:    subcommand: type REPLACE RELOPTIONS desc <NULL>
 CREATE VIEW datatype_view AS
   SELECT * FROM datatype_table;
 NOTICE:  DDL test: type simple, tag CREATE VIEW
+CREATE OR REPLACE VIEW datatype_view AS
+  SELECT * FROM datatype_table, static_view;
+NOTICE:  DDL test: type simple, tag CREATE VIEW
+NOTICE:  DDL test: type alter table, tag CREATE VIEW
+NOTICE:    subcommand: type ADD COLUMN TO VIEW desc column col of view datatype_view
+NOTICE:    subcommand: type REPLACE RELOPTIONS desc <NULL>
 CREATE RECURSIVE VIEW nums_1_100 (n) AS
     VALUES (1)
 UNION ALL
index 030b76f86fa64f080edc67155f73c661f4e3833b..f473dd74171d82161e9f901260b9a1f8d6b91ba3 100644 (file)
@@ -11,6 +11,9 @@ CREATE OR REPLACE VIEW static_view AS
 CREATE VIEW datatype_view AS
   SELECT * FROM datatype_table;
 
+CREATE OR REPLACE VIEW datatype_view AS
+  SELECT * FROM datatype_table, static_view;
+
 CREATE RECURSIVE VIEW nums_1_100 (n) AS
     VALUES (1)
 UNION ALL
index 16e4530708cc97b013648eb5cf738c063b7712b0..f897b079e67e98d7430fa46f4bb24afb0c45e8f6 100644 (file)
@@ -416,7 +416,8 @@ CREATE SCHEMA evttrig
        CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL)
        CREATE INDEX one_idx ON one (col_b)
        CREATE TABLE two (col_c INTEGER CHECK (col_c > 0) REFERENCES one DEFAULT 42)
-       CREATE TABLE id (col_d int NOT NULL GENERATED ALWAYS AS IDENTITY);
+       CREATE TABLE id (col_d int NOT NULL GENERATED ALWAYS AS IDENTITY)
+       CREATE VIEW one_view AS SELECT * FROM two;
 NOTICE:  END: command_tag=CREATE SCHEMA type=schema identity=evttrig
 NOTICE:  END: command_tag=CREATE SEQUENCE type=sequence identity=evttrig.one_col_a_seq
 NOTICE:  END: command_tag=CREATE SEQUENCE type=sequence identity=evttrig.one_col_c_seq
@@ -429,7 +430,14 @@ NOTICE:  END: command_tag=ALTER TABLE type=table identity=evttrig.two
 NOTICE:  END: command_tag=CREATE SEQUENCE type=sequence identity=evttrig.id_col_d_seq
 NOTICE:  END: command_tag=CREATE TABLE type=table identity=evttrig.id
 NOTICE:  END: command_tag=ALTER SEQUENCE type=sequence identity=evttrig.id_col_d_seq
+NOTICE:  END: command_tag=CREATE VIEW type=view identity=evttrig.one_view
 NOTICE:  END: command_tag=CREATE INDEX type=index identity=evttrig.one_idx
+-- View with column additions
+CREATE OR REPLACE VIEW evttrig.one_view AS SELECT * FROM evttrig.two, evttrig.id;
+NOTICE:  END: command_tag=CREATE VIEW type=view identity=evttrig.one_view
+NOTICE:  END: command_tag=CREATE VIEW type=view identity=evttrig.one_view
+DROP VIEW evttrig.one_view;
+NOTICE:  NORMAL: orig=t normal=f istemp=f type=view identity=evttrig.one_view schema=evttrig name=one_view addr={evttrig,one_view} args={}
 -- Partitioned tables with a partitioned index
 CREATE TABLE evttrig.parted (
     id int PRIMARY KEY)
index c613c0cfd439bd34867e7fad05b78d3acc4c028e..32e9bb58c5e946bc9697e8536f920d57dcddc507 100644 (file)
@@ -324,7 +324,12 @@ CREATE SCHEMA evttrig
        CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two', col_c SERIAL)
        CREATE INDEX one_idx ON one (col_b)
        CREATE TABLE two (col_c INTEGER CHECK (col_c > 0) REFERENCES one DEFAULT 42)
-       CREATE TABLE id (col_d int NOT NULL GENERATED ALWAYS AS IDENTITY);
+       CREATE TABLE id (col_d int NOT NULL GENERATED ALWAYS AS IDENTITY)
+       CREATE VIEW one_view AS SELECT * FROM two;
+
+-- View with column additions
+CREATE OR REPLACE VIEW evttrig.one_view AS SELECT * FROM evttrig.two, evttrig.id;
+DROP VIEW evttrig.one_view;
 
 -- Partitioned tables with a partitioned index
 CREATE TABLE evttrig.parted (