Browse Source

Replace alter_table.sql test usage of event triggers.

The test in 93765bd956 added an event trigger to ensure that the
tested table rewrites do not get optimized away (as happened in the
past). But doing so would require running the tests in isolation, as
otherwise the trigger might also fire in concurrent sessions, causing
test failures there.

Reported-By: Tom Lane
Discussion: https://postgr.es/m/3328.1570740683@sss.pgh.pa.us
Backpatch: 12, just as 93765bd956
master
Andres Freund 3 weeks ago
parent
commit
ae5cae54ca
2 changed files with 116 additions and 58 deletions
  1. 73
    31
      src/test/regress/expected/alter_table.out
  2. 43
    27
      src/test/regress/sql/alter_table.sql

+ 73
- 31
src/test/regress/expected/alter_table.out View File

@@ -2440,48 +2440,89 @@ drop table at_base_table;
-- 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);
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

+ 43
- 27
src/test/regress/sql/alter_table.sql View File

@@ -1555,47 +1555,62 @@ drop table at_base_table;

-- 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);

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;

--

Loading…
Cancel
Save