Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
-- Test hash index scans of a bucket pair whose split is incomplete.
--
-- A scan whose target bucket is still being populated by a split must read
-- both buckets: the one being populated, skipping the tuples moved there,
-- and the one being split.
CREATE EXTENSION injection_points;
SELECT injection_points_set_local();
 injection_points_set_local 
----------------------------
 
(1 row)
 
SELECT injection_points_attach('hash-finish-incomplete-split', 'notice');
 injection_points_attach 
-------------------------
 
(1 row)
 
-- Build an index on an analyzed 10-row table, so that it starts out with
-- just two buckets (metapage, two bucket pages, bitmap page).  Rows with
-- v = 1 hash to bucket 0, and to bucket 2 once bucket 0 splits.
CREATE TABLE hash_split_test (v int4) WITH (autovacuum_enabled = false);
INSERT INTO hash_split_test SELECT 1 FROM generate_series(1, 10);
ANALYZE hash_split_test;
CREATE INDEX hash_split_index ON hash_split_test USING hash (v);
SELECT pg_relation_size('hash_split_index') /
       current_setting('block_size')::int AS index_pages;
 index_pages 
-------------
           4
(1 row)
 
-- Error out during the first bucket split, leaving it incomplete
SELECT injection_points_attach('hash-split-before-relocation', 'error');
 injection_points_attach 
-------------------------
 
(1 row)
 
INSERT INTO hash_split_test SELECT g FROM generate_series(1000001, 1005000) g;
ERROR:  error triggered for injection point hash-split-before-relocation
SELECT injection_points_detach('hash-split-before-relocation');
 injection_points_detach 
-------------------------
 
(1 row)
 
-- This row goes to the new bucket, which is still flagged as being populated
INSERT INTO hash_split_test VALUES (1);
-- Forward and backward index scans must find it there, plus the 10 older
-- rows that remain in bucket 0
SET enable_seqscan = off;
SET enable_bitmapscan = off;
EXPLAIN (COSTS OFF) SELECT count(*) FROM hash_split_test WHERE v = 1;
                         QUERY PLAN                         
------------------------------------------------------------
 Aggregate
   ->  Index Scan using hash_split_index on hash_split_test
         Index Cond: (v = 1)
(3 rows)
 
SELECT count(*) FROM hash_split_test WHERE v = 1;
 count 
-------
    11
(1 row)
 
BEGIN;
DECLARE c SCROLL CURSOR FOR SELECT v FROM hash_split_test WHERE v = 1;
MOVE FORWARD ALL IN c;
FETCH BACKWARD ALL FROM c;
 v 
---
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
(11 rows)
 
COMMIT;
-- An insert into bucket 0 (v = 5 hashes there) finishes the split.  The
-- scan must still find all 11 rows.
INSERT INTO hash_split_test VALUES (5);
NOTICE:  notice triggered for injection point hash-finish-incomplete-split
SELECT count(*) FROM hash_split_test WHERE v = 1;
 count 
-------
    11
(1 row)
 
-- The heap agrees
SET enable_seqscan = on;
SET enable_indexscan = off;
SELECT count(*) FROM hash_split_test WHERE v = 1;
 count 
-------
    11
(1 row)
 
SELECT injection_points_detach('hash-finish-incomplete-split');
 injection_points_detach 
-------------------------
 
(1 row)
 
DROP TABLE hash_split_test;
DROP EXTENSION injection_points;