-- a column requiring a default (bug #16038)
-- ensure that rewrites aren't silently optimized away, removing the
-- value of the test
-CREATE OR REPLACE FUNCTION evtrig_rewrite_log() RETURNS event_trigger
+CREATE FUNCTION check_ddl_rewrite(p_tablename regclass, p_ddl text)
+RETURNS boolean
LANGUAGE plpgsql AS $$
+DECLARE
+ v_relfilenode oid;
BEGIN
- RAISE WARNING 'rewriting table %',
- pg_event_trigger_table_rewrite_oid()::regclass;
+ v_relfilenode := relfilenode FROM pg_class WHERE oid = p_tablename;
+
+ EXECUTE p_ddl;
+
+ RETURN v_relfilenode <> (SELECT relfilenode FROM pg_class WHERE oid = p_tablename);
END;
$$;
-CREATE EVENT TRIGGER evtrig_rewrite_log ON table_rewrite
- EXECUTE PROCEDURE evtrig_rewrite_log();
CREATE TABLE rewrite_test(col text);
INSERT INTO rewrite_test VALUES ('something');
INSERT INTO rewrite_test VALUES (NULL);
--- empty[12] doesn't need rewrite, but notempty[12]_rewrite will force one
-ALTER TABLE rewrite_test
- ADD COLUMN empty1 text,
- ADD COLUMN notempty1_rewrite serial;
-WARNING: rewriting table rewrite_test
-ALTER TABLE rewrite_test
- ADD COLUMN notempty2_rewrite serial,
- ADD COLUMN empty2 text;
-WARNING: rewriting table rewrite_test
+-- empty[12] don't need rewrite, but notempty[12]_rewrite will force one
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN empty1 text,
+ ADD COLUMN notempty1_rewrite serial;
+$$);
+ check_ddl_rewrite
+-------------------
+ t
+(1 row)
+
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN notempty2_rewrite serial,
+ ADD COLUMN empty2 text;
+$$);
+ check_ddl_rewrite
+-------------------
+ t
+(1 row)
+
-- also check that fast defaults cause no problem, first without rewrite
-ALTER TABLE rewrite_test
- ADD COLUMN empty3 text,
- ADD COLUMN notempty3_norewrite int default 42;
-ALTER TABLE rewrite_test
- ADD COLUMN notempty4_norewrite int default 42,
- ADD COLUMN empty4 text;
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN empty3 text,
+ ADD COLUMN notempty3_norewrite int default 42;
+$$);
+ check_ddl_rewrite
+-------------------
+ f
+(1 row)
+
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN notempty4_norewrite int default 42,
+ ADD COLUMN empty4 text;
+$$);
+ check_ddl_rewrite
+-------------------
+ f
+(1 row)
+
-- then with rewrite
-ALTER TABLE rewrite_test
- ADD COLUMN empty5 text,
- ADD COLUMN notempty5_norewrite int default 42,
- ADD COLUMN notempty5_rewrite serial;
-WARNING: rewriting table rewrite_test
-ALTER TABLE rewrite_test
- ADD COLUMN notempty6_rewrite serial,
- ADD COLUMN empty6 text,
- ADD COLUMN notempty6_norewrite int default 42;
-WARNING: rewriting table rewrite_test
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN empty5 text,
+ ADD COLUMN notempty5_norewrite int default 42,
+ ADD COLUMN notempty5_rewrite serial;
+$$);
+ check_ddl_rewrite
+-------------------
+ t
+(1 row)
+
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN notempty6_rewrite serial,
+ ADD COLUMN empty6 text,
+ ADD COLUMN notempty6_norewrite int default 42;
+$$);
+ check_ddl_rewrite
+-------------------
+ t
+(1 row)
+
-- cleanup
-drop event trigger evtrig_rewrite_log;
-drop function evtrig_rewrite_log();
+DROP FUNCTION check_ddl_rewrite(regclass, text);
DROP TABLE rewrite_test;
--
-- lock levels
-- ensure that rewrites aren't silently optimized away, removing the
-- value of the test
-CREATE OR REPLACE FUNCTION evtrig_rewrite_log() RETURNS event_trigger
+CREATE FUNCTION check_ddl_rewrite(p_tablename regclass, p_ddl text)
+RETURNS boolean
LANGUAGE plpgsql AS $$
+DECLARE
+ v_relfilenode oid;
BEGIN
- RAISE WARNING 'rewriting table %',
- pg_event_trigger_table_rewrite_oid()::regclass;
+ v_relfilenode := relfilenode FROM pg_class WHERE oid = p_tablename;
+
+ EXECUTE p_ddl;
+
+ RETURN v_relfilenode <> (SELECT relfilenode FROM pg_class WHERE oid = p_tablename);
END;
$$;
-CREATE EVENT TRIGGER evtrig_rewrite_log ON table_rewrite
- EXECUTE PROCEDURE evtrig_rewrite_log();
CREATE TABLE rewrite_test(col text);
INSERT INTO rewrite_test VALUES ('something');
INSERT INTO rewrite_test VALUES (NULL);
--- empty[12] doesn't need rewrite, but notempty[12]_rewrite will force one
-ALTER TABLE rewrite_test
- ADD COLUMN empty1 text,
- ADD COLUMN notempty1_rewrite serial;
-ALTER TABLE rewrite_test
- ADD COLUMN notempty2_rewrite serial,
- ADD COLUMN empty2 text;
+-- empty[12] don't need rewrite, but notempty[12]_rewrite will force one
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN empty1 text,
+ ADD COLUMN notempty1_rewrite serial;
+$$);
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN notempty2_rewrite serial,
+ ADD COLUMN empty2 text;
+$$);
-- also check that fast defaults cause no problem, first without rewrite
-ALTER TABLE rewrite_test
- ADD COLUMN empty3 text,
- ADD COLUMN notempty3_norewrite int default 42;
-ALTER TABLE rewrite_test
- ADD COLUMN notempty4_norewrite int default 42,
- ADD COLUMN empty4 text;
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN empty3 text,
+ ADD COLUMN notempty3_norewrite int default 42;
+$$);
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN notempty4_norewrite int default 42,
+ ADD COLUMN empty4 text;
+$$);
-- then with rewrite
-ALTER TABLE rewrite_test
- ADD COLUMN empty5 text,
- ADD COLUMN notempty5_norewrite int default 42,
- ADD COLUMN notempty5_rewrite serial;
-ALTER TABLE rewrite_test
- ADD COLUMN notempty6_rewrite serial,
- ADD COLUMN empty6 text,
- ADD COLUMN notempty6_norewrite int default 42;
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN empty5 text,
+ ADD COLUMN notempty5_norewrite int default 42,
+ ADD COLUMN notempty5_rewrite serial;
+$$);
+SELECT check_ddl_rewrite('rewrite_test', $$
+ ALTER TABLE rewrite_test
+ ADD COLUMN notempty6_rewrite serial,
+ ADD COLUMN empty6 text,
+ ADD COLUMN notempty6_norewrite int default 42;
+$$);
-- cleanup
-drop event trigger evtrig_rewrite_log;
-drop function evtrig_rewrite_log();
+DROP FUNCTION check_ddl_rewrite(regclass, text);
DROP TABLE rewrite_test;
--