| 1 |
# Test TOAST behavior in PL/pgSQL procedures with transaction control. |
| 2 |
# |
| 3 |
# We need to ensure that values stored in PL/pgSQL variables are free |
| 4 |
# of external TOAST references, because those could disappear after a |
| 5 |
# transaction is committed (leading to errors "missing chunk number |
| 6 |
# ... for toast value ..."). The tests here do this by running VACUUM |
| 7 |
# in a second session. Advisory locks are used to have the VACUUM |
| 8 |
# kick in at the right time. The different "assign" steps test |
| 9 |
# different code paths for variable assignments in PL/pgSQL. |
| 10 |
|
| 11 |
setup |
| 12 |
{ |
| 13 |
CREATE TABLE test1 (a int, b text); |
| 14 |
ALTER TABLE test1 ALTER COLUMN b SET STORAGE EXTERNAL; |
| 15 |
INSERT INTO test1 VALUES (1, repeat('foo', 2000)); |
| 16 |
CREATE TYPE test2 AS (a bigint, b text); |
| 17 |
} |
| 18 |
|
| 19 |
teardown |
| 20 |
{ |
| 21 |
DROP TABLE test1; |
| 22 |
DROP TYPE test2; |
| 23 |
} |
| 24 |
|
| 25 |
session s1 |
| 26 |
|
| 27 |
setup |
| 28 |
{ |
| 29 |
SELECT pg_advisory_unlock_all(); |
| 30 |
} |
| 31 |
|
| 32 |
# assign_simple_var() |
| 33 |
step assign1 |
| 34 |
{ |
| 35 |
do $$ |
| 36 |
declare |
| 37 |
x text; |
| 38 |
begin |
| 39 |
select test1.b into x from test1; |
| 40 |
delete from test1; |
| 41 |
commit; |
| 42 |
perform pg_advisory_lock(1); |
| 43 |
raise notice 'length(x) = %', length(x); |
| 44 |
end; |
| 45 |
$$; |
| 46 |
} |
| 47 |
|
| 48 |
# assign_simple_var() |
| 49 |
step assign2 |
| 50 |
{ |
| 51 |
do $$ |
| 52 |
declare |
| 53 |
x text; |
| 54 |
begin |
| 55 |
x := (select test1.b from test1); |
| 56 |
delete from test1; |
| 57 |
commit; |
| 58 |
perform pg_advisory_lock(1); |
| 59 |
raise notice 'length(x) = %', length(x); |
| 60 |
end; |
| 61 |
$$; |
| 62 |
} |
| 63 |
|
| 64 |
# expanded_record_set_field() |
| 65 |
step assign3 |
| 66 |
{ |
| 67 |
do $$ |
| 68 |
declare |
| 69 |
r record; |
| 70 |
begin |
| 71 |
select * into r from test1; |
| 72 |
r.b := (select test1.b from test1); |
| 73 |
delete from test1; |
| 74 |
commit; |
| 75 |
perform pg_advisory_lock(1); |
| 76 |
raise notice 'length(r) = %', length(r::text); |
| 77 |
end; |
| 78 |
$$; |
| 79 |
} |
| 80 |
|
| 81 |
# expanded_record_set_fields() |
| 82 |
step assign4 |
| 83 |
{ |
| 84 |
do $$ |
| 85 |
declare |
| 86 |
r test2; |
| 87 |
begin |
| 88 |
select * into r from test1; |
| 89 |
delete from test1; |
| 90 |
commit; |
| 91 |
perform pg_advisory_lock(1); |
| 92 |
raise notice 'length(r) = %', length(r::text); |
| 93 |
end; |
| 94 |
$$; |
| 95 |
} |
| 96 |
|
| 97 |
# expanded_record_set_tuple() |
| 98 |
step assign5 |
| 99 |
{ |
| 100 |
do $$ |
| 101 |
declare |
| 102 |
r record; |
| 103 |
begin |
| 104 |
for r in select test1.b from test1 loop |
| 105 |
null; |
| 106 |
end loop; |
| 107 |
delete from test1; |
| 108 |
commit; |
| 109 |
perform pg_advisory_lock(1); |
| 110 |
raise notice 'length(r) = %', length(r::text); |
| 111 |
end; |
| 112 |
$$; |
| 113 |
} |
| 114 |
|
| 115 |
# FOR loop must not hold any fetched-but-not-detoasted values across commit |
| 116 |
step assign6 |
| 117 |
{ |
| 118 |
do $$ |
| 119 |
declare |
| 120 |
r record; |
| 121 |
begin |
| 122 |
insert into test1 values (2, repeat('bar', 3000)); |
| 123 |
insert into test1 values (3, repeat('baz', 4000)); |
| 124 |
for r in select test1.b from test1 loop |
| 125 |
delete from test1; |
| 126 |
commit; |
| 127 |
perform pg_advisory_lock(1); |
| 128 |
raise notice 'length(r) = %', length(r::text); |
| 129 |
end loop; |
| 130 |
end; |
| 131 |
$$; |
| 132 |
} |
| 133 |
|
| 134 |
# Check that the results of a query can be detoasted just after committing |
| 135 |
# (there's no interaction with VACUUM here) |
| 136 |
step "fetch-after-commit" |
| 137 |
{ |
| 138 |
do $$ |
| 139 |
declare |
| 140 |
r record; |
| 141 |
t text; |
| 142 |
begin |
| 143 |
insert into test1 values (2, repeat('bar', 3000)); |
| 144 |
insert into test1 values (3, repeat('baz', 4000)); |
| 145 |
for r in select test1.a from test1 loop |
| 146 |
commit; |
| 147 |
select b into t from test1 where a = r.a; |
| 148 |
raise notice 'length(t) = %', length(t); |
| 149 |
end loop; |
| 150 |
end; |
| 151 |
$$; |
| 152 |
} |
| 153 |
|
| 154 |
session s2 |
| 155 |
setup |
| 156 |
{ |
| 157 |
SELECT pg_advisory_unlock_all(); |
| 158 |
} |
| 159 |
step lock |
| 160 |
{ |
| 161 |
SELECT pg_advisory_lock(1); |
| 162 |
} |
| 163 |
step vacuum |
| 164 |
{ |
| 165 |
VACUUM test1; |
| 166 |
} |
| 167 |
step unlock |
| 168 |
{ |
| 169 |
SELECT pg_advisory_unlock(1); |
| 170 |
} |
| 171 |
|
| 172 |
permutation lock assign1 vacuum unlock |
| 173 |
permutation lock assign2 vacuum unlock |
| 174 |
permutation lock assign3 vacuum unlock |
| 175 |
permutation lock assign4 vacuum unlock |
| 176 |
permutation lock assign5 vacuum unlock |
| 177 |
permutation lock assign6 vacuum unlock |
| 178 |
permutation "fetch-after-commit" |