Исходный код СУБД 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
-- Primary key types test
-- This test verifies that vectorization works with various primary key types
 
-- ============================================================================
-- Test 1: UUID primary key
-- ============================================================================
CREATE TABLE test_uuid_pk (
    id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
    content TEXT
);
 
-- Enable vectorization
SELECT pgedge_vectorizer.enable_vectorization(
    'test_uuid_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Verify source_id column type matches UUID
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_uuid_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document
INSERT INTO test_uuid_pk (content)
VALUES ('This document has a UUID primary key.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_uuid_pk_content_chunks;
 
-- Test UPDATE
UPDATE test_uuid_pk SET content = 'Updated UUID document content.';
 
-- Verify chunks still exist after update
SELECT COUNT(*) > 0 AS chunks_after_update FROM test_uuid_pk_content_chunks;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_uuid_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_uuid_pk'::regclass, 'content', true);
DROP TABLE test_uuid_pk;
 
-- ============================================================================
-- Test 2: INTEGER (SERIAL) primary key
-- ============================================================================
CREATE TABLE test_serial_pk (
    id SERIAL PRIMARY KEY,
    content TEXT
);
 
-- Enable vectorization
SELECT pgedge_vectorizer.enable_vectorization(
    'test_serial_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Verify source_id column type matches integer
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_serial_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document
INSERT INTO test_serial_pk (content)
VALUES ('This document has an integer primary key.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_serial_pk_content_chunks;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_serial_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_serial_pk'::regclass, 'content', true);
DROP TABLE test_serial_pk;
 
-- ============================================================================
-- Test 3: BIGINT primary key (regression test)
-- ============================================================================
CREATE TABLE test_bigint_pk (
    id BIGSERIAL PRIMARY KEY,
    content TEXT
);
 
-- Enable vectorization
SELECT pgedge_vectorizer.enable_vectorization(
    'test_bigint_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Verify source_id column type matches bigint
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_bigint_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document
INSERT INTO test_bigint_pk (content)
VALUES ('This document has a bigint primary key.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_bigint_pk_content_chunks;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_bigint_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_bigint_pk'::regclass, 'content', true);
DROP TABLE test_bigint_pk;
 
-- ============================================================================
-- Test 4: TEXT primary key (slug-style)
-- ============================================================================
CREATE TABLE test_text_pk (
    slug TEXT PRIMARY KEY,
    content TEXT
);
 
-- Enable vectorization
SELECT pgedge_vectorizer.enable_vectorization(
    'test_text_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Verify source_id column type matches text
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_text_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document with a slug value
INSERT INTO test_text_pk (slug, content)
VALUES ('my-great-article', 'This document uses a text slug as primary key.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_text_pk_content_chunks;
 
-- Verify the actual source_id value stored
SELECT source_id FROM test_text_pk_content_chunks LIMIT 1;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_text_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_text_pk'::regclass, 'content', true);
DROP TABLE test_text_pk;
 
-- ============================================================================
-- Test 5: VARCHAR(26) primary key (ULID-style)
-- ============================================================================
CREATE TABLE test_varchar_pk (
    id VARCHAR(26) PRIMARY KEY,
    content TEXT
);
 
-- Enable vectorization
SELECT pgedge_vectorizer.enable_vectorization(
    'test_varchar_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Verify source_id column type matches character varying
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_varchar_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document with a ULID-style value
INSERT INTO test_varchar_pk (id, content)
VALUES ('01HZXK5V3RQJF8N2GYTP4M', 'This document uses a VARCHAR ULID-style primary key.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_varchar_pk_content_chunks;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_varchar_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_varchar_pk'::regclass, 'content', true);
DROP TABLE test_varchar_pk;
 
-- ============================================================================
-- Test 6: BIGINT primary key named record_num (auto-detection)
-- ============================================================================
CREATE TABLE test_autodetect_pk (
    record_num BIGSERIAL PRIMARY KEY,
    content TEXT
);
 
-- Enable vectorization WITHOUT passing source_pk
-- Auto-detection should find 'record_num' from pg_index
SELECT pgedge_vectorizer.enable_vectorization(
    'test_autodetect_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Verify source_id column type matches bigint
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_autodetect_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document
INSERT INTO test_autodetect_pk (content)
VALUES ('This document uses an auto-detected primary key.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_autodetect_pk_content_chunks;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_autodetect_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_autodetect_pk'::regclass, 'content', true);
DROP TABLE test_autodetect_pk;
 
-- ============================================================================
-- Test 7: Composite primary key (error case)
-- ============================================================================
CREATE TABLE test_composite_pk (
    tenant_id INT NOT NULL,
    item_id INT NOT NULL,
    content TEXT,
    PRIMARY KEY (tenant_id, item_id)
);
 
-- This should raise an error because composite PKs are not supported
SELECT pgedge_vectorizer.enable_vectorization(
    'test_composite_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Clean up (no vectorization to disable, just drop the table)
DROP TABLE test_composite_pk;
 
-- ============================================================================
-- Test 7a: Composite primary key with explicit source_pk (should succeed)
-- ============================================================================
CREATE TABLE test_composite_override (
    tenant_id INT NOT NULL,
    item_id INT NOT NULL,
    content TEXT,
    PRIMARY KEY (tenant_id, item_id)
);
 
-- This should succeed because we specify which column to use
-- Note: source_pk must be globally unique to avoid UNIQUE(source_id, chunk_index) conflicts
SELECT pgedge_vectorizer.enable_vectorization(
    'test_composite_override'::regclass, 'content', 'token_based', 100, 10, 1536, NULL, 'item_id'
);
 
-- Verify source_id column type matches integer
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_composite_override_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document with unique item_id
INSERT INTO test_composite_override (tenant_id, item_id, content)
VALUES (1, 42, 'Document in a composite PK table with explicit source_pk.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_composite_override_content_chunks;
 
-- Insert a second row with the SAME item_id but different tenant_id.
-- This demonstrates the uniqueness conflict: source_pk must be globally
-- unique, not just unique within the composite key.
INSERT INTO test_composite_override (tenant_id, item_id, content)
VALUES (2, 42, 'Different tenant, same item_id — will conflict on chunk table.');
 
-- Verify the conflict: the second insert's trigger overwrites the first
-- row's chunks because they share the same source_id (42)
SELECT COUNT(*) AS chunk_count FROM test_composite_override_content_chunks WHERE source_id = 42;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_composite_override_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_composite_override'::regclass, 'content', true);
DROP TABLE test_composite_override;
 
-- ============================================================================
-- Test 7b: No primary key (error case)
-- ============================================================================
CREATE TABLE test_no_pk (
    item_id INT NOT NULL,
    content TEXT
);
 
-- This should raise an error because the table has no primary key
SELECT pgedge_vectorizer.enable_vectorization(
    'test_no_pk'::regclass,
    'content',
    'token_based',
    100,
    10,
    1536
);
 
-- Clean up
DROP TABLE test_no_pk;
 
-- ============================================================================
-- Test 8: Explicit source_pk override
-- ============================================================================
CREATE TABLE test_override_pk (
    id BIGSERIAL PRIMARY KEY,
    external_id UUID NOT NULL DEFAULT gen_random_uuid(),
    content TEXT
);
 
-- Enable vectorization with explicit source_pk override using positional args
-- Pass NULL for chunk_table_name, 'external_id' for source_pk
SELECT pgedge_vectorizer.enable_vectorization(
    'test_override_pk'::regclass, 'content', 'token_based', 100, 10, 1536, NULL, 'external_id'
);
 
-- Verify source_id column type matches uuid (from external_id, not bigint from id)
SELECT data_type
FROM information_schema.columns
WHERE table_name = 'test_override_pk_content_chunks'
AND column_name = 'source_id';
 
-- Insert a test document
INSERT INTO test_override_pk (content)
VALUES ('This document uses an overridden primary key column.');
 
-- Verify chunks were created
SELECT COUNT(*) > 0 AS chunks_created FROM test_override_pk_content_chunks;
 
-- Clean up
DELETE FROM pgedge_vectorizer.queue WHERE chunk_table = 'test_override_pk_content_chunks';
SELECT pgedge_vectorizer.disable_vectorization('test_override_pk'::regclass, 'content', true);
DROP TABLE test_override_pk;