Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2026, PostgreSQL Global Development Group
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
plan skip_all => 'Injection points not supported by this build'
  unless $ENV{enable_injection_points} eq 'yes';
my $node = PostgreSQL::Test::Cluster->new('rowtype');
$node->init;
$node->start;
plan skip_all => 'Extension injection_points not installed'
  unless $node->check_extension('injection_points');
sub sql { return $node->safe_psql('postgres', $_[0]); }
sql(q[
CREATE EXTENSION injection_points;
CREATE TABLE rt(a int, b int, PRIMARY KEY(a,b));
CREATE INDEX rt_ix ON rt(a,b);
CREATE INDEX rt_one ON rt(a);
CREATE TABLE rt_values(v rt_ix, av rt_ix[]);
INSERT INTO rt_values VALUES (ROW(1,2)::rt_ix, ARRAY[ROW(3,4)::rt_ix]);
INSERT INTO rt VALUES (1,2);
]);
sub identity
{
    my ($name) = @_;
    return sql(qq[
SELECT c.reltype, t.typarray, t.typrelid = c.oid,
       (SELECT count(*) FROM pg_depend
        WHERE classid = 'pg_type'::regclass AND objid = t.oid
          AND refclassid = 'pg_class'::regclass AND refobjid = c.oid
          AND deptype = 'i')
FROM pg_class c JOIN pg_type t ON t.oid = c.reltype
WHERE c.oid = '$name'::regclass]);
}
sub versions
{
    return sql(q[
SELECT attnum, coalesce(attoptions::text, '<null>')
FROM pg_attribute WHERE attrelid = 'rt_ix'::regclass AND attnum > 0
ORDER BY attnum]);
}
my $identity = identity('rt_ix');
like($identity, qr/^\d+\|\d+\|t\|1$/, 'typed index identity and dependency');
my $versions = versions();
like($versions, qr/pg_attver=/, 'version metadata exists');
my $reader = $node->background_psql('postgres');
$reader->query_safe('PREPARE rt_read AS SELECT v::text, av::text FROM rt_values');
my $data = $reader->query_safe('EXECUTE rt_read');
is($data, '(1,2)|{"(3,4)"}', 'stored composite and array');
sql('REINDEX INDEX rt_ix');
is(identity('rt_ix'), $identity, 'ordinary reindex preserves identity');
is(versions(), $versions, 'ordinary reindex preserves versions');
my $worker = $node->background_psql('postgres', on_error_stop => 0);
my $pid = $worker->query_safe('SELECT pg_backend_pid()');
my $before = 'reindex-relation-concurrently-before-swap';
my $after = 'reindex-relation-concurrently-before-set-dead';
$worker->query_safe("SELECT injection_points_set_local(); SELECT injection_points_attach('$before', 'wait'); SELECT injection_points_attach('$after', 'wait')");
my $old = sql("SELECT 'rt_ix'::regclass::oid");
$worker->query_until(qr/starting_reindex/, "\\echo starting_reindex\nREINDEX INDEX CONCURRENTLY rt_ix;\n");
sub wait_point
{
    my ($point) = @_;
    $node->poll_query_until('postgres', "SELECT EXISTS (SELECT FROM pg_stat_activity WHERE pid = $pid AND wait_event_type = 'InjectionPoint' AND wait_event = '$point')")
      or BAIL_OUT("did not reach $point");
}
wait_point($before);
is(identity('rt_ix'), $identity, 'before swap logical identity unchanged');
is($reader->query_safe('EXECUTE rt_read'), $data, 'prepared reader before swap');
sql('INSERT INTO rt VALUES (5,6)');
sql("SELECT injection_points_wakeup('$before')");
wait_point($after);
is(identity('rt_ix'), $identity, 'after swap logical identity unchanged');
is(versions(), $versions, 'after swap exact attribute versions');
is(sql("SELECT reltype FROM pg_class WHERE oid = $old"), '0', 'old shadow has no rowtype');
is(sql("SELECT count(*) FROM pg_attribute a, unnest(a.attoptions) o WHERE a.attrelid = $old AND o LIKE 'pg_attver=%'"), '0', 'old shadow has no reserved options');
is($reader->query_safe('EXECUTE rt_read'), $data, 'prepared reader after swap');
sql('INSERT INTO rt VALUES (7,8)');
sql("SELECT injection_points_wakeup('$after')");
$worker->query_safe('SELECT 1');
$worker->quit;
is(sql("SELECT count(*) FROM pg_class WHERE oid = $old"), '0', 'old shadow dropped');
is(sql('SELECT count(*) FROM rt'), '3', 'concurrent writes preserved');
is(identity('rt_ix'), $identity, 'identity survives cleanup');
$reader->quit;
sql('REINDEX INDEX CONCURRENTLY rt_ix');
is(identity('rt_ix'), $identity, 'repeat concurrent reindex');
is(versions(), $versions, 'repeat preserves versions');
my $pk = identity('rt_pkey');
sql('REINDEX INDEX CONCURRENTLY rt_pkey');
is(identity('rt_pkey'), $pk, 'primary key rowtype preserved');
sql('REINDEX INDEX CONCURRENTLY rt_one');
is(sql("SELECT reltype FROM pg_class WHERE oid = 'rt_one'::regclass"), '0', 'untyped index remains untyped');
# A zero and an absent version model a pre-versioning upgrade fixture.
sql(q[
DELETE FROM rt_values;
SET allow_system_table_mods = on;
UPDATE pg_attribute SET attoptions = CASE attnum
  WHEN 1 THEN ARRAY['pg_attver=0']::text[] ELSE NULL END
WHERE attrelid = 'rt_ix'::regclass AND attnum > 0;
]);
# Populate values using the simulated old-format descriptor, rather than
# comparing a nonzero-version datum with an artificially zeroed catalog.
sql(q[INSERT INTO rt_values VALUES (ROW(1,2)::rt_ix, ARRAY[ROW(3,4)::rt_ix])]);
is(sql('SELECT v::text, av::text FROM rt_values'), $data, 'zero-version fixture values');
my $zero_versions = versions();
is($zero_versions, "1|{pg_attver=0}\n2|<null>", 'zero and absent fixture');
sql('REINDEX INDEX CONCURRENTLY rt_ix');
is(versions(), $zero_versions, 'exact zero and absent versions survive swap');
is(identity('rt_ix'), $identity, 'zero versions preserve type identity');
 
# Cancel at the committed swap boundary and remove only the old shadow.
$worker = $node->background_psql('postgres', on_error_stop => 0);
$pid = $worker->query_safe('SELECT pg_backend_pid()');
$worker->query_safe("SELECT injection_points_set_local(); SELECT injection_points_attach('$after', 'wait')");
$old = sql("SELECT 'rt_ix'::regclass::oid");
$worker->query_until(qr/cancel_reindex/, "\\echo cancel_reindex\nREINDEX INDEX CONCURRENTLY rt_ix;\n");
wait_point($after);
is(sql("SELECT pg_cancel_backend($pid)"), 't', 'cancel after committed swap');
my ($out, $error) = $worker->query('SELECT 1');
ok($error, 'reindex reports cancellation');
like($worker->{stderr}, qr/canceling statement due to user request/, 'cancel error');
$worker->quit;
is(identity('rt_ix'), $identity, 'cancel preserves logical identity');
is(sql("SELECT reltype FROM pg_class WHERE oid = $old"), '0', 'cancel leaves untyped shadow');
my $shadow = sql("SELECT oid::regclass FROM pg_class WHERE oid = $old");
sql("DROP INDEX $shadow");
sql('REINDEX INDEX CONCURRENTLY rt_ix');
is(identity('rt_ix'), $identity, 'reindex succeeds after cancel and drop');
is(versions(), $zero_versions, 'retry preserves zero and absent versions');
is(sql('SELECT v::text, av::text FROM rt_values'), $data, 'retry preserves stored values');
 
sql(q[
CREATE TABLE rt_part(a int, b int) PARTITION BY RANGE (a);
CREATE TABLE rt_part1 PARTITION OF rt_part FOR VALUES FROM (0) TO (10);
CREATE INDEX rt_part_ix ON rt_part(a,b);
INSERT INTO rt_part VALUES (1,2);
]);
my $child = sql("SELECT inhrelid::regclass FROM pg_inherits WHERE inhparent = 'rt_part_ix'::regclass");
my $child_identity = identity($child);
like($child_identity, qr/^\d+\|\d+\|t\|1$/, 'partition index has rowtype');
sql("REINDEX INDEX CONCURRENTLY $child");
is(identity($child), $child_identity, 'partition child keeps logical identity');
is(sql("SELECT count(*) FROM pg_inherits WHERE inhparent = 'rt_part_ix'::regclass AND inhrelid = '$child'::regclass"), '1', 'partition child stays attached');
 
# Use the existing option parser as an actual btree support procedure
# for this test opclass; no test-only catalog option is fabricated.
sql(q[
CREATE OPERATOR CLASS rt_int_opts FOR TYPE int4 USING btree AS
  OPERATOR 1 <(int4,int4), OPERATOR 2 <=(int4,int4),
  OPERATOR 3 =(int4,int4), OPERATOR 4 >=(int4,int4),
  OPERATOR 5 >(int4,int4), FUNCTION 1 btint4cmp(int4,int4),
  FUNCTION 5 brin_minmax_multi_options(internal);
CREATE INDEX rt_opt ON rt ((a+1) rt_int_opts(values_per_range=32), b);
ALTER INDEX rt_opt ALTER COLUMN 1 SET STATISTICS 123;
]);
my $opt_identity = identity('rt_opt');
my $opt_attrs = q[SELECT attoptions::text, attstattarget
 FROM pg_attribute WHERE attrelid = 'rt_opt'::regclass AND attnum = 1];
my $opts = sql($opt_attrs);
like($opts, qr/values_per_range=32.*pg_attver=.*\|123$/, 'nonempty options and statistics fixture');
$worker = $node->background_psql('postgres', on_error_stop => 0);
$pid = $worker->query_safe('SELECT pg_backend_pid()');
$worker->query_safe("SELECT injection_points_set_local(); SELECT injection_points_attach('$after', 'wait')");
$old = sql("SELECT 'rt_opt'::regclass::oid");
$worker->query_until(qr/options_reindex/, "\\echo options_reindex\nREINDEX INDEX CONCURRENTLY rt_opt;\n");
wait_point($after);
is(sql($opt_attrs), $opts, 'new index preserves raw options and statistics');
is(sql("SELECT attoptions::text, attstattarget FROM pg_attribute WHERE attrelid = $old AND attnum = 1"), '{values_per_range=32}|123', 'old index keeps real options and statistics only');
sql("SELECT injection_points_wakeup('$after')");
$worker->query_safe('SELECT 1');
$worker->quit;
is(identity('rt_opt'), $opt_identity, 'option-bearing index preserves identity');
 
$node->stop;
done_testing();