| 1 |
-- |
| 2 |
-- The test uses a GIN index over int[]. The table contains arrays |
| 3 |
-- with integers from 1 to :next_i. Each integer occurs exactly once, |
| 4 |
-- no gaps or duplicates, although the index does contain some |
| 5 |
-- duplicate elements because some of the inserting transactions are |
| 6 |
-- rolled back during the test. The exact contents of the table depend |
| 7 |
-- on the physical layout of the index, which in turn depends at least |
| 8 |
-- on the block size, so instead of check for the exact contents, we |
| 9 |
-- check those invariants. :next_i psql variable is maintained at all |
| 10 |
-- times to hold the last inserted integer + 1. |
| 11 |
-- |
| 12 |
|
| 13 |
-- This uses injection points to cause errors that leave some page |
| 14 |
-- splits in "incomplete" state |
| 15 |
create extension injection_points; |
| 16 |
|
| 17 |
-- Make all injection points local to this process, for concurrency. |
| 18 |
SELECT injection_points_set_local(); |
| 19 |
|
| 20 |
-- Use the index for all the queries |
| 21 |
set enable_seqscan=off; |
| 22 |
|
| 23 |
-- Print a NOTICE whenever an incomplete split gets fixed |
| 24 |
SELECT injection_points_attach('gin-finish-incomplete-split', 'notice'); |
| 25 |
|
| 26 |
-- |
| 27 |
-- First create the test table and some helper functions |
| 28 |
-- |
| 29 |
create table gin_incomplete_splits(i int4[]) with (autovacuum_enabled = off); |
| 30 |
|
| 31 |
create index on gin_incomplete_splits using gin (i) with (fastupdate = off); |
| 32 |
|
| 33 |
-- Creates an array with all integers from $1 (inclusive) $2 (exclusive) |
| 34 |
create function range_array(int, int) returns int[] language sql immutable as $$ |
| 35 |
select array_agg(g) from generate_series($1, $2 - 1) g |
| 36 |
$$; |
| 37 |
|
| 38 |
-- Inserts an array with 'n' rows to the test table. Pass :next_i as |
| 39 |
-- the first argument, returns the new value for :next_i. |
| 40 |
create function insert_n(first_i int, n int) returns int language plpgsql as $$ |
| 41 |
begin |
| 42 |
insert into gin_incomplete_splits values (range_array(first_i, first_i+n)); |
| 43 |
return first_i + n; |
| 44 |
end; |
| 45 |
$$; |
| 46 |
|
| 47 |
-- Inserts to the table until an insert fails. Like insert_n(), returns the |
| 48 |
-- new value for :next_i. |
| 49 |
create function insert_until_fail(next_i int, step int default 1) returns int language plpgsql as $$ |
| 50 |
declare |
| 51 |
i integer; |
| 52 |
begin |
| 53 |
-- Insert arrays with 'step' elements each, until an error occurs. |
| 54 |
i := 0; |
| 55 |
loop |
| 56 |
begin |
| 57 |
select insert_n(next_i, step) into next_i; |
| 58 |
exception when others then |
| 59 |
raise notice 'failed with: %', sqlerrm; |
| 60 |
exit; |
| 61 |
end; |
| 62 |
|
| 63 |
-- The caller is expected to set an injection point that eventually |
| 64 |
-- causes an error. But bail out if still no error after 10000 |
| 65 |
-- attempts, so that we don't get stuck in an infinite loop. |
| 66 |
i := i + 1; |
| 67 |
if i = 10000 then |
| 68 |
raise 'no error on inserts after % iterations', i; |
| 69 |
end if; |
| 70 |
end loop; |
| 71 |
|
| 72 |
return next_i; |
| 73 |
end; |
| 74 |
$$; |
| 75 |
|
| 76 |
-- Check the invariants. |
| 77 |
create function verify(next_i int) returns bool language plpgsql as $$ |
| 78 |
declare |
| 79 |
a integer[]; |
| 80 |
elem integer; |
| 81 |
c integer; |
| 82 |
begin |
| 83 |
-- Perform a scan over the trailing part of the index, where the |
| 84 |
-- possible incomplete splits are. (We don't check the whole table, |
| 85 |
-- because that'd be pretty slow.) |
| 86 |
c := 0; |
| 87 |
-- Find all arrays that overlap with the last 200 inserted integers. Or |
| 88 |
-- the next 100, which shouldn't exist. |
| 89 |
for a in select i from gin_incomplete_splits where i && range_array(next_i - 200, next_i + 100) |
| 90 |
loop |
| 91 |
-- count all elements in those arrays in the window. |
| 92 |
foreach elem in ARRAY a loop |
| 93 |
if elem >= next_i then |
| 94 |
raise 'unexpected value % in array', elem; |
| 95 |
end if; |
| 96 |
if elem >= next_i - 200 then |
| 97 |
c := c + 1; |
| 98 |
end if; |
| 99 |
end loop; |
| 100 |
end loop; |
| 101 |
if c <> 200 then |
| 102 |
raise 'unexpected count % ', c; |
| 103 |
end if; |
| 104 |
|
| 105 |
return true; |
| 106 |
end; |
| 107 |
$$; |
| 108 |
|
| 109 |
-- Insert one array to get started. |
| 110 |
select insert_n(1, 1000) as next_i |
| 111 |
\gset |
| 112 |
select verify(:next_i); |
| 113 |
|
| 114 |
|
| 115 |
-- |
| 116 |
-- Test incomplete leaf split |
| 117 |
-- |
| 118 |
SELECT injection_points_attach('gin-leave-leaf-split-incomplete', 'error'); |
| 119 |
select insert_until_fail(:next_i) as next_i |
| 120 |
\gset |
| 121 |
SELECT injection_points_detach('gin-leave-leaf-split-incomplete'); |
| 122 |
|
| 123 |
-- Verify that a scan works even though there's an incomplete split |
| 124 |
select verify(:next_i); |
| 125 |
|
| 126 |
-- Insert some more rows, finishing the split |
| 127 |
select insert_n(:next_i, 10) as next_i |
| 128 |
\gset |
| 129 |
-- Verify that a scan still works |
| 130 |
select verify(:next_i); |
| 131 |
|
| 132 |
|
| 133 |
-- |
| 134 |
-- Test incomplete internal page split |
| 135 |
-- |
| 136 |
SELECT injection_points_attach('gin-leave-internal-split-incomplete', 'error'); |
| 137 |
select insert_until_fail(:next_i, 100) as next_i |
| 138 |
\gset |
| 139 |
SELECT injection_points_detach('gin-leave-internal-split-incomplete'); |
| 140 |
|
| 141 |
-- Verify that a scan works even though there's an incomplete split |
| 142 |
select verify(:next_i); |
| 143 |
|
| 144 |
-- Insert some more rows, finishing the split |
| 145 |
select insert_n(:next_i, 10) as next_i |
| 146 |
\gset |
| 147 |
-- Verify that a scan still works |
| 148 |
select verify(:next_i); |
| 149 |
|
| 150 |
SELECT injection_points_detach('gin-finish-incomplete-split'); |
| 151 |
|
| 152 |
drop extension injection_points; |