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.
<para>
The <literal>table_rewrite</> event occurs just before a table is
- rewritten by the command <literal>ALTER TABLE</literal>. While other
+ rewritten by some actions of the commands <literal>ALTER TABLE</> and
+ <literal>ALTER TYPE</>. While other
control statements are available to rewrite a table,
like <literal>CLUSTER</literal> and <literal>VACUUM</literal>,
- the <literal>table_rewrite</> event is currently only triggered by
- the <literal>ALTER TABLE</literal> command, and only when that command
- attempts to rewrite the table.
+ the <literal>table_rewrite</> event is not triggered by them.
</para>
<para>
<entry align="center"><literal>X</literal></entry>
<entry align="center"><literal>X</literal></entry>
<entry align="center"><literal>-</literal></entry>
- <entry align="center"><literal>-</literal></entry>
+ <entry align="center"><literal>X</literal></entry>
<entry align="center"></entry>
</row>
<row>
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;
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();
-- 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();