From 3f190f67eb45ae61d696fbce8ab48d246a12f709 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 27 Feb 2015 18:38:33 -0300 Subject: [PATCH] Fix table_rewrite event trigger for ALTER TYPE/SET DATA TYPE CASCADE When a composite type being used in a typed table is modified by way of ALTER TYPE, a table rewrite occurs appearing to come from ALTER TYPE. The existing event_trigger.c code was unable to cope with that and raised a spurious error. The fix is just to accept that command tag for the event, and document this properly. Noted while fooling with deparsing of DDL commands. This appears to be an oversight in commit 618c9430a. Thanks to Mark Wong for documentation wording help. --- doc/src/sgml/event-trigger.sgml | 9 ++++----- src/backend/commands/event_trigger.c | 3 ++- src/test/regress/expected/event_trigger.out | 11 +++++++++++ src/test/regress/sql/event_trigger.sql | 10 ++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/doc/src/sgml/event-trigger.sgml b/doc/src/sgml/event-trigger.sgml index 04353eac3d..f151eb7375 100644 --- a/doc/src/sgml/event-trigger.sgml +++ b/doc/src/sgml/event-trigger.sgml @@ -68,12 +68,11 @@ The table_rewrite event occurs just before a table is - rewritten by the command ALTER TABLE. While other + rewritten by some actions of the commands ALTER TABLE and + ALTER TYPE. While other control statements are available to rewrite a table, like CLUSTER and VACUUM, - the table_rewrite event is currently only triggered by - the ALTER TABLE command, and only when that command - attempts to rewrite the table. + the table_rewrite event is not triggered by them. @@ -318,7 +317,7 @@ X X - - - + X diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index dcf5b98747..deeb8dcfaf 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -326,7 +326,8 @@ validate_table_rewrite_tags(const char *filtervar, List *taglist) static event_trigger_command_tag_check_result check_table_rewrite_ddl_tag(const char *tag) { - if (pg_strcasecmp(tag, "ALTER TABLE") == 0) + if (pg_strcasecmp(tag, "ALTER TABLE") == 0 || + pg_strcasecmp(tag, "ALTER TYPE") == 0) return EVENT_TRIGGER_COMMAND_TAG_OK; return EVENT_TRIGGER_COMMAND_TAG_NOT_SUPPORTED; diff --git a/src/test/regress/expected/event_trigger.out b/src/test/regress/expected/event_trigger.out index 2095794576..8462fbff1c 100644 --- a/src/test/regress/expected/event_trigger.out +++ b/src/test/regress/expected/event_trigger.out @@ -370,6 +370,17 @@ alter table rewriteme NOTICE: Table 'rewriteme' is being rewritten (reason = 6) -- shouldn't trigger a table_rewrite event alter table rewriteme alter column foo type numeric(12,4); +-- typed tables are rewritten when their type changes +create type rewritetype as (a int); +create table rewritemetoo1 of rewritetype; +create table rewritemetoo2 of rewritetype; +alter type rewritetype alter attribute a type text cascade; +NOTICE: Table 'rewritemetoo1' is being rewritten (reason = 4) +NOTICE: Table 'rewritemetoo2' is being rewritten (reason = 4) +-- but this doesn't work +create table rewritemetoo3 (a rewritetype); +alter type rewritetype alter attribute a type varchar cascade; +ERROR: cannot alter type "rewritetype" because column "rewritemetoo3.a" uses it drop table rewriteme; drop event trigger no_rewrite_allowed; drop function test_evtrig_no_rewrite(); diff --git a/src/test/regress/sql/event_trigger.sql b/src/test/regress/sql/event_trigger.sql index c6e47ed856..4baad80b1d 100644 --- a/src/test/regress/sql/event_trigger.sql +++ b/src/test/regress/sql/event_trigger.sql @@ -276,6 +276,16 @@ alter table rewriteme -- shouldn't trigger a table_rewrite event alter table rewriteme alter column foo type numeric(12,4); +-- typed tables are rewritten when their type changes +create type rewritetype as (a int); +create table rewritemetoo1 of rewritetype; +create table rewritemetoo2 of rewritetype; +alter type rewritetype alter attribute a type text cascade; + +-- but this doesn't work +create table rewritemetoo3 (a rewritetype); +alter type rewritetype alter attribute a type varchar cascade; + drop table rewriteme; drop event trigger no_rewrite_allowed; drop function test_evtrig_no_rewrite();