Исходный код СУБД 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
# Copyright (c) 2026, PostgreSQL Global Development Group
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
# Test only temporary, isolated instances; never migrate user clusters.
my $node = PostgreSQL::Test::Cluster->new('temp_catalog_rbtree');
$node->init;
$node->start;
my $a = $node->background_psql('postgres');
my $b = $node->background_psql('postgres');
 
is($a->query_safe('SHOW enable_temp_memory_catalog'), 'off',
   'default GUC remains off');
$a->query_safe(q{
SET enable_temp_memory_catalog=on;
DO $body$
BEGIN
  FOR i IN 1..64 LOOP
    EXECUTE format('CREATE TEMP TABLE tc_rb_%s(id integer, v integer DEFAULT 7)', i);
  END LOOP;
END
$body$;
});
is($a->query_safe(q{
SELECT count(*) FROM generate_series(1,64) g
WHERE to_regclass(format('pg_temp.tc_rb_%s',g)) IS NOT NULL
}), '64', '64 memory catalog objects resolve after indexed lookups');
 
is($a->query_safe(q{
SELECT EXISTS (SELECT FROM pg_backend_memory_contexts
               WHERE name='TempCatalogIndexContext')
}), 't', 'derived RBTree index was created, not only a list scan');
 
is($b->query_safe(q{
SELECT to_regclass('pg_temp.tc_rb_1') IS NULL
}), 't', 'other session does not see temporary catalog objects');
$b->query_safe(q{
SET enable_temp_memory_catalog=on;
CREATE TEMP TABLE tc_rb_1(id integer,v integer);
INSERT INTO tc_rb_1 VALUES(42,84);
});
is($b->query_safe('SELECT v FROM tc_rb_1'), '84',
   'second session has independent temporary relation');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_1') IS NOT NULL}),
   't', 'first session preserved same-named temporary relation');
 
$a->query_safe(q{
DROP TABLE tc_rb_32;
DROP TABLE tc_rb_16;
DROP TABLE tc_rb_48;
});
is($a->query_safe(q{
SELECT (to_regclass('pg_temp.tc_rb_32') IS NULL
        AND to_regclass('pg_temp.tc_rb_31') IS NOT NULL
        AND to_regclass('pg_temp.tc_rb_33') IS NOT NULL)
}), 't', 'deleted middle key does not corrupt its RBTree neighbors');
is($a->query_safe(q{
SELECT (to_regclass('pg_temp.tc_rb_16') IS NULL
        AND to_regclass('pg_temp.tc_rb_48') IS NULL)
}), 't', 'independent deletions remove only matching keys');
 
$a->query_safe('ALTER TABLE tc_rb_31 RENAME TO tc_rb_31_renamed');
is($a->query_safe(q{
SELECT (to_regclass('pg_temp.tc_rb_31') IS NULL
        AND to_regclass('pg_temp.tc_rb_31_renamed') IS NOT NULL)
}), 't', 'catalog rename invalidates old indexed key');
 
$a->query_safe(q{
BEGIN;
SAVEPOINT rb_created;
CREATE TEMP TABLE tc_rb_transient(x integer);
});
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_transient') IS NOT NULL}),
   't', 'created tuple visible before subtransaction rollback');
$a->query_safe('ROLLBACK TO SAVEPOINT rb_created; RELEASE SAVEPOINT rb_created; COMMIT');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_transient') IS NULL}),
   't', 'rollback removed tuple and its indexed key');
 
$a->query_safe(q{
BEGIN;
SAVEPOINT rb_deleted;
DROP TABLE tc_rb_33;
});
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_33') IS NULL}),
   't', 'deleted tuple invisible in current subtransaction');
$a->query_safe('ROLLBACK TO SAVEPOINT rb_deleted; RELEASE SAVEPOINT rb_deleted; COMMIT');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_33') IS NOT NULL}),
   't', 'undo restores deleted tuple and indexed key');
 
$a->query_safe('INSERT INTO tc_rb_34(id,v) VALUES (1,700)');
$a->query_safe('PREPARE rb_reader AS SELECT v FROM tc_rb_34 WHERE id=1');
is($a->query_safe('EXECUTE rb_reader'), '700',
   'prepared reader accesses live temporary relation');
$a->query_safe('ALTER TABLE tc_rb_30 RENAME TO tc_rb_30_renamed');
is($a->query_safe('EXECUTE rb_reader'), '700',
   'prepared reader survives an unrelated catalog rename');
 
$a->query_safe(q{
SET enable_temp_memory_catalog=off;
ALTER TABLE tc_rb_34 ALTER COLUMN v TYPE bigint USING v::bigint;
SET enable_temp_memory_catalog=on;
});
is($a->query_safe('SELECT pg_typeof(v)::text FROM tc_rb_34 LIMIT 1'),
   'bigint', 'column type update is visible through catalog lookup');
is($a->query_safe('SELECT v FROM tc_rb_34 WHERE id=1'), '700',
   'column type update preserves existing data');
 
$a->query_safe(q{
BEGIN;
CREATE TEMP TABLE tc_rb_top_abort(x integer);
});
is($a->query_safe(q{
SELECT to_regclass('pg_temp.tc_rb_top_abort') IS NOT NULL
}), 't', 'top-level transaction sees its new temporary table');
$a->query_safe('ROLLBACK');
is($a->query_safe(q{
SELECT to_regclass('pg_temp.tc_rb_top_abort') IS NULL
}), 't', 'top-level abort removes all newly created catalog tuples');
 
$a->query_safe('BEGIN; DROP TABLE tc_rb_35; COMMIT');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_35') IS NULL}),
   't', 'committed deletion does not leave stale RBTree entry');
 
$a->query_safe('CREATE TEMP TABLE tc_rb_32(id integer,v integer)');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_32') IS NOT NULL}),
   't', 'previously deleted name may be reused');
$a->query_safe('DROP TABLE tc_rb_32');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_32') IS NULL}),
   't', 'reused key removed without touching other buckets');
 
$b->query_safe('DROP TABLE tc_rb_1');
is($b->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_1') IS NULL}),
   't', 'second session removes only its own tuple');
is($a->query_safe(q{SELECT to_regclass('pg_temp.tc_rb_1') IS NOT NULL}),
   't', 'first session remains unaffected by second session cleanup');
 
# Clean up inside the test connection, not by deleting a foreign PGDATA.
$a->query_safe(q{
DO $body$
BEGIN
  FOR i IN 1..64 LOOP
    IF to_regclass(format('pg_temp.tc_rb_%s', i)) IS NOT NULL THEN
      EXECUTE format('DROP TABLE pg_temp.tc_rb_%s', i);
    END IF;
  END LOOP;
END
$body$;
DROP TABLE tc_rb_31_renamed;
DROP TABLE tc_rb_30_renamed;
});
is($a->query_safe(q{
SELECT (to_regclass('pg_temp.tc_rb_1') IS NULL AND
        to_regclass('pg_temp.tc_rb_31_renamed') IS NULL AND
        to_regclass('pg_temp.tc_rb_30_renamed') IS NULL)
}), 't', 'all task-owned temporary table names are removed');
 
$a->quit;
$b->quit;
$node->stop;
done_testing();