| 1 |
# Test cleanup of objects in temporary schema. |
| 2 |
|
| 3 |
setup { |
| 4 |
CREATE TABLE s1_temp_schema(oid oid); |
| 5 |
-- to help create a long function |
| 6 |
CREATE FUNCTION exec(p_foo text) RETURNS void LANGUAGE plpgsql AS $$BEGIN EXECUTE p_foo; END;$$; |
| 7 |
} |
| 8 |
|
| 9 |
teardown { |
| 10 |
DROP TABLE s1_temp_schema; |
| 11 |
DROP FUNCTION exec(text); |
| 12 |
} |
| 13 |
|
| 14 |
session "s1" |
| 15 |
setup { |
| 16 |
CREATE TEMPORARY TABLE just_to_create_temp_schema(); |
| 17 |
DROP TABLE just_to_create_temp_schema; |
| 18 |
INSERT INTO s1_temp_schema SELECT pg_my_temp_schema(); |
| 19 |
} |
| 20 |
|
| 21 |
step s1_advisory { |
| 22 |
SELECT pg_advisory_lock('pg_namespace'::regclass::int8); |
| 23 |
} |
| 24 |
|
| 25 |
step s1_create_temp_objects { |
| 26 |
|
| 27 |
-- create function large enough to be toasted, to ensure we correctly clean those up, a prior bug |
| 28 |
-- https://postgr.es/m/CAOFAq3BU5Mf2TTvu8D9n_ZOoFAeQswuzk7yziAb7xuw_qyw5gw%40mail.gmail.com |
| 29 |
SELECT exec(format($outer$ |
| 30 |
CREATE OR REPLACE FUNCTION pg_temp.long() RETURNS text LANGUAGE sql AS $body$ SELECT %L; $body$$outer$, |
| 31 |
(SELECT string_agg(g.i::text||':'||random()::text, '|') FROM generate_series(1, 100) g(i)))); |
| 32 |
|
| 33 |
-- The above bug requires function removal to happen after a catalog |
| 34 |
-- invalidation. dependency.c sorts objects in descending oid order so |
| 35 |
-- that newer objects are deleted before older objects, so create a |
| 36 |
-- table after. |
| 37 |
CREATE TEMPORARY TABLE invalidate_catalog_cache(); |
| 38 |
|
| 39 |
-- test non-temp function is dropped when depending on temp table |
| 40 |
CREATE TEMPORARY TABLE just_give_me_a_type(id serial primary key); |
| 41 |
|
| 42 |
CREATE FUNCTION uses_a_temp_type(just_give_me_a_type) RETURNS int LANGUAGE sql AS $$SELECT 1;$$; |
| 43 |
} |
| 44 |
|
| 45 |
step s1_discard_temp { |
| 46 |
DISCARD TEMP; |
| 47 |
} |
| 48 |
|
| 49 |
step s1_exit { |
| 50 |
SELECT pg_terminate_backend(pg_backend_pid()); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
session "s2" |
| 55 |
|
| 56 |
step s2_advisory { |
| 57 |
SELECT pg_advisory_lock('pg_namespace'::regclass::int8); |
| 58 |
} |
| 59 |
|
| 60 |
step s2_check_schema { |
| 61 |
SELECT oid::regclass FROM pg_class WHERE relnamespace = (SELECT oid FROM s1_temp_schema); |
| 62 |
SELECT oid::regproc FROM pg_proc WHERE pronamespace = (SELECT oid FROM s1_temp_schema); |
| 63 |
SELECT oid::regproc FROM pg_type WHERE typnamespace = (SELECT oid FROM s1_temp_schema); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
# Test temporary object cleanup during DISCARD. |
| 68 |
permutation |
| 69 |
s1_create_temp_objects |
| 70 |
s1_discard_temp |
| 71 |
s2_check_schema |
| 72 |
|
| 73 |
# Test temporary object cleanup during process exit. |
| 74 |
# |
| 75 |
# To check (in s2) if temporary objects (in s1) have properly been removed we |
| 76 |
# need to wait for s1 to finish cleaning up. Luckily session level advisory |
| 77 |
# locks are released only after temp table cleanup. |
| 78 |
permutation |
| 79 |
s1_advisory |
| 80 |
s2_advisory |
| 81 |
s1_create_temp_objects |
| 82 |
s1_exit |
| 83 |
s2_check_schema |
| 84 |
|
| 85 |
# Can't run further tests here, because s1's connection is dead |