Исходный код СУБД 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include "dbms_pipe.hpp"
#include "dbms_pipe_buffer.hpp"
#include "dbms_pipe_queue.hpp"
#include "dbms_pipe_registry.hpp"
 
extern "C"
{
#include "catalog/objectaccess.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type_d.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/elog.h"
#include "utils/lsyscache.h"
#include "utils/timestamp.h"
#include "utils/varlena.h"
}
 
extern "C"
{
PG_FUNCTION_INFO_V1(dbms_pipe_maxwait);
PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_varchar2);
PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_number);
PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_date);
PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_timestamp);
PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_raw);
PG_FUNCTION_INFO_V1(dbms_pipe_pack_message_rowid);
PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_varchar2);
PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_number);
PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_date);
PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_raw);
PG_FUNCTION_INFO_V1(dbms_pipe_unpack_message_rowid);
PG_FUNCTION_INFO_V1(dbms_pipe_next_item_type);
PG_FUNCTION_INFO_V1(dbms_pipe_reset_buffer);
PG_FUNCTION_INFO_V1(dbms_pipe_create_pipe);
PG_FUNCTION_INFO_V1(dbms_pipe_remove_pipe);
PG_FUNCTION_INFO_V1(dbms_pipe_send_message_ovr1);
PG_FUNCTION_INFO_V1(dbms_pipe_send_message_ovr2);
PG_FUNCTION_INFO_V1(dbms_pipe_send_message_ovr3);
PG_FUNCTION_INFO_V1(dbms_pipe_receive_message_ovr1);
PG_FUNCTION_INFO_V1(dbms_pipe_receive_message_ovr2);
PG_FUNCTION_INFO_V1(dbms_pipe_receive_message_ovr3);
PG_FUNCTION_INFO_V1(dbms_pipe_set_credential_name);
PG_FUNCTION_INFO_V1(dbms_pipe_set_location_uri);
PG_FUNCTION_INFO_V1(dbms_pipe_get_credential_name);
PG_FUNCTION_INFO_V1(dbms_pipe_get_location_uri);
PG_FUNCTION_INFO_V1(dbms_pipe_purge_ovr1);
PG_FUNCTION_INFO_V1(dbms_pipe_purge_ovr2);
PG_FUNCTION_INFO_V1(dbms_pipe_purge_ovr3);
PG_FUNCTION_INFO_V1(dbms_pipe_unique_session_name);
}
 
static void
dbms_pipe_report_not_supported(const char *name)
{
    ereport(NOTICE, (errmsg("Функция DBMS_PIPE.%s не поддерживается.", name)));
}
 
static void
dbms_pipe_raise_invalid_null_parameter()
{
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
             errmsg("ORA-23319: parameter value null is not appropriate")));
}
 
static void
dbms_pipe_validate_nonnegative(int32 value)
{
    if (value < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("ORA-23319: parameter value %d is not appropriate",
                        value)));
}
 
static void
dbms_pipe_raise_privilege_error()
{
    ereport(ERROR,
            (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
             errmsg("ORA-23322: Privilege error accessing pipe")));
}
 
static int32
dbms_pipe_invoke_cache_function(const text *cache_func, const text *pipename)
{
    Oid argument_types[1] = {VARCHAR2OID};
    List *const function_name =
        textToQualifiedNameList(const_cast<text *>(cache_func));
    const Oid function_oid =
        LookupFuncName(function_name, 1, argument_types, false);
 
    if (get_func_rettype(function_oid) != INT4OID)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("DBMS_PIPE cache function must return integer")));
 
    const AclResult acl_result = object_aclcheck(
        ProcedureRelationId, function_oid, GetUserId(), ACL_EXECUTE);
    if (acl_result != ACLCHECK_OK)
        aclcheck_error(acl_result, OBJECT_FUNCTION, get_func_name(function_oid));
 
    InvokeFunctionExecuteHook(function_oid);
 
    FmgrInfo function_info;
    fmgr_info(function_oid, &function_info);
    return DatumGetInt32(FunctionCall1(
        &function_info, PointerGetDatum(const_cast<text *>(pipename))));
}
 
/* Значение DATE передаётся с точностью до секунды, без округления. */
static Timestamp
dbms_pipe_truncate_date_seconds(Timestamp value)
{
    if (TIMESTAMP_NOT_FINITE(value))
        return value;
 
    const int64 fraction = value % USECS_PER_SEC;
    value -= fraction;
    /* Для отрицательной эпохи нужен floor, а не округление к нулю. */
    if (fraction < 0)
        value -= USECS_PER_SEC;
 
    return value;
}
 
static int32
dbms_pipe_receive_to_local_buffer(const DbmsPipeNameKey *key,
                              int32 timeout,
                              DbmsPipeReceiveMode mode)
{
    const int32 result = dbms_pipe_queue_receive(key, timeout, mode);
 
    if (result != 0)
        dbms_pipe_buffer_receive_failed();
 
    return result;
}
 
Datum
dbms_pipe_maxwait(PG_FUNCTION_ARGS)
{
    PG_RETURN_INT32(86400000);
}
 
Datum
dbms_pipe_pack_message_varchar2(PG_FUNCTION_ARGS)
{
    const bool is_null = PG_ARGISNULL(0);
    const text *item = is_null ? nullptr : PG_GETARG_TEXT_PP(0);
 
    dbms_pipe_buffer_pack_varchar2(item, is_null);
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_pack_message_number(PG_FUNCTION_ARGS)
{
    const bool is_null = PG_ARGISNULL(0);
    Numeric item = is_null ? nullptr : PG_GETARG_NUMERIC(0);
 
    dbms_pipe_buffer_pack_number(item, is_null);
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_pack_message_date(PG_FUNCTION_ARGS)
{
    const bool is_null = PG_ARGISNULL(0);
    const Timestamp item = is_null ? 0 :
        date2timestamp_safe(PG_GETARG_DATEADT(0), nullptr);
 
    dbms_pipe_buffer_pack_date(item, is_null);
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_pack_message_timestamp(PG_FUNCTION_ARGS)
{
    const bool is_null = PG_ARGISNULL(0);
    const Timestamp item = is_null ? 0 :
        dbms_pipe_truncate_date_seconds(PG_GETARG_TIMESTAMP(0));
 
    dbms_pipe_buffer_pack_date(item, is_null);
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_pack_message_raw(PG_FUNCTION_ARGS)
{
    const bool is_null = PG_ARGISNULL(0);
    const bytea *item = is_null ? nullptr : PG_GETARG_BYTEA_PP(0);
 
    dbms_pipe_buffer_pack_raw(item, is_null);
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_pack_message_rowid(PG_FUNCTION_ARGS)
{
    const bool is_null = PG_ARGISNULL(0);
    const text *item =
        is_null ? nullptr : DatumGetTextPP(PG_GETARG_DATUM(0));
 
    dbms_pipe_buffer_pack_rowid(item, is_null);
    PG_RETURN_VOID();
}
 
/*
 * SQL-результат не должен ссылаться на долгоживущий входящий буфер:
 * следующий RECEIVE_MESSAGE или PURGE может сбросить его контекст.
 * Копия принадлежит контексту текущего SQL-вызова.
 */
static void *
dbms_pipe_copy_unpacked_value(const DbmsPipeUnpackedItem &item)
{
    void *const result = palloc(item.data_bytes);
 
    memcpy(result, item.data, item.data_bytes);
    return result;
}
 
Datum
dbms_pipe_unpack_message_varchar2(PG_FUNCTION_ARGS)
{
    const DbmsPipeUnpackedItem item =
        dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_VARCHAR2);
 
    if (item.is_null)
        PG_RETURN_NULL();
 
    PG_RETURN_TEXT_P(static_cast<text *>(dbms_pipe_copy_unpacked_value(item)));
}
 
Datum
dbms_pipe_unpack_message_number(PG_FUNCTION_ARGS)
{
    const DbmsPipeUnpackedItem item =
        dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_NUMBER);
 
    if (item.is_null)
        PG_RETURN_NULL();
 
    PG_RETURN_NUMERIC(static_cast<Numeric>(dbms_pipe_copy_unpacked_value(item)));
}
 
Datum
dbms_pipe_unpack_message_date(PG_FUNCTION_ARGS)
{
    const DbmsPipeUnpackedItem item =
        dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_DATE);
 
    if (item.is_null)
        PG_RETURN_NULL();
 
    if (item.data_bytes != sizeof(Timestamp))
        ereport(ERROR,
                (errcode(ERRCODE_DATA_CORRUPTED),
                 errmsg("corrupt DBMS_PIPE DATE payload")));
 
    Timestamp value;
    memcpy(&value, item.data, sizeof(value));
    PG_RETURN_TIMESTAMP(value);
}
 
Datum
dbms_pipe_unpack_message_raw(PG_FUNCTION_ARGS)
{
    const DbmsPipeUnpackedItem item =
        dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_RAW);
 
    if (item.is_null)
        PG_RETURN_NULL();
 
    PG_RETURN_BYTEA_P(static_cast<bytea *>(dbms_pipe_copy_unpacked_value(item)));
}
 
Datum
dbms_pipe_unpack_message_rowid(PG_FUNCTION_ARGS)
{
    const DbmsPipeUnpackedItem item =
        dbms_pipe_buffer_unpack(DBMS_PIPE_ITEM_ROWID);
 
    if (item.is_null)
        PG_RETURN_NULL();
 
    PG_RETURN_POINTER(dbms_pipe_copy_unpacked_value(item));
}
 
Datum
dbms_pipe_next_item_type(PG_FUNCTION_ARGS)
{
    PG_RETURN_INT32(dbms_pipe_buffer_next_item_type());
}
 
Datum
dbms_pipe_reset_buffer(PG_FUNCTION_ARGS)
{
    dbms_pipe_buffer_reset();
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_create_pipe(PG_FUNCTION_ARGS)
{
    const bool pipename_is_null = PG_ARGISNULL(0);
    const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0);
    const DbmsPipeNameKey key =
        dbms_pipe_make_name_key(pipename, pipename_is_null);
 
    if (PG_ARGISNULL(1))
        dbms_pipe_raise_invalid_null_parameter();
    const int32 maxpipesize = PG_GETARG_INT32(1);
    dbms_pipe_validate_nonnegative(maxpipesize);
 
    const bool is_private = PG_ARGISNULL(2) ? true : PG_GETARG_BOOL(2);
    const bool singleton = PG_ARGISNULL(3) ? false : PG_GETARG_BOOL(3);
 
    if (PG_ARGISNULL(4))
        dbms_pipe_raise_invalid_null_parameter();
    const int32 shelflife = PG_GETARG_INT32(4);
    dbms_pipe_validate_nonnegative(shelflife);
 
    if (!singleton && shelflife > 0)
        PG_RETURN_INT32(7);
 
    dbms_pipe_queue_prepare();
    bool found;
    DbmsPipeRegistryEntry *const entry =
        dbms_pipe_registry_find_or_insert(&key, &found);
    const Oid current_user = GetUserId();
 
    if (!found)
    {
        dbms_pipe_registry_initialize_entry(entry, maxpipesize, is_private,
                                    singleton, shelflife);
        dbms_pipe_registry_release(entry);
        PG_RETURN_INT32(0);
    }
 
    if (entry->is_private != is_private ||
        (entry->is_private && entry->owner_id != current_user))
    {
        dbms_pipe_registry_release(entry);
        dbms_pipe_raise_privilege_error();
    }
 
    if (!entry->singleton && singleton)
    {
        if (entry->message_count > 1)
        {
            dbms_pipe_registry_release(entry);
            PG_RETURN_INT32(6);
        }
 
        entry->singleton = true;
        entry->shelf_life = shelflife;
    }
    else if (entry->singleton && !singleton)
        dbms_pipe_queue_convert_singleton_to_ordinary(entry);
    else if (entry->singleton)
        entry->shelf_life = shelflife;
    dbms_pipe_registry_release(entry);
    PG_RETURN_INT32(0);
}
 
Datum
dbms_pipe_remove_pipe(PG_FUNCTION_ARGS)
{
    const bool pipename_is_null = PG_ARGISNULL(0);
    const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0);
    const DbmsPipeNameKey key =
        dbms_pipe_make_name_key(pipename, pipename_is_null);
 
    dbms_pipe_queue_prepare();
    DbmsPipeRegistryEntry *const entry = dbms_pipe_registry_find(&key, true);
 
    if (entry == nullptr)
        PG_RETURN_INT32(0);
 
    if (entry->is_private && entry->owner_id != GetUserId())
    {
        dbms_pipe_registry_release(entry);
        dbms_pipe_raise_privilege_error();
    }
 
    dbms_pipe_queue_discard_messages(entry);
    dbms_pipe_registry_delete(entry);
    PG_RETURN_INT32(0);
}
 
Datum
dbms_pipe_send_message_ovr1(PG_FUNCTION_ARGS)
{
    const bool pipename_is_null = PG_ARGISNULL(0);
    const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0);
    const DbmsPipeNameKey key =
        dbms_pipe_make_name_key(pipename, pipename_is_null);
 
    if (PG_ARGISNULL(1))
        dbms_pipe_raise_invalid_null_parameter();
    const int32 timeout = PG_GETARG_INT32(1);
    dbms_pipe_validate_nonnegative(timeout);
 
    if (PG_ARGISNULL(2))
        dbms_pipe_raise_invalid_null_parameter();
    const int32 maxpipesize = PG_GETARG_INT32(2);
    dbms_pipe_validate_nonnegative(maxpipesize);
 
    const bool singleton = PG_ARGISNULL(3) ? false : PG_GETARG_BOOL(3);
 
    if (PG_ARGISNULL(4))
        dbms_pipe_raise_invalid_null_parameter();
    const int32 shelflife = PG_GETARG_INT32(4);
    dbms_pipe_validate_nonnegative(shelflife);
 
    DbmsPipeSerializedMessage message = dbms_pipe_buffer_serialize_outgoing();
    const int32 result = dbms_pipe_queue_send(
        &key, &message, timeout, maxpipesize, singleton, shelflife);
 
    if (message.data != nullptr)
        pfree(message.data);
 
    if (result == 0)
        dbms_pipe_buffer_send_succeeded();
 
    PG_RETURN_INT32(result);
}
 
Datum
dbms_pipe_send_message_ovr2(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("SEND_MESSAGE");
    PG_RETURN_NULL();
}
 
Datum
dbms_pipe_send_message_ovr3(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("SEND_MESSAGE");
    PG_RETURN_NULL();
}
 
Datum
dbms_pipe_receive_message_ovr1(PG_FUNCTION_ARGS)
{
    const bool pipename_is_null = PG_ARGISNULL(0);
    const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0);
    const DbmsPipeNameKey key =
        dbms_pipe_make_name_key(pipename, pipename_is_null);
 
    if (PG_ARGISNULL(1))
        dbms_pipe_raise_invalid_null_parameter();
    const int32 timeout = PG_GETARG_INT32(1);
    dbms_pipe_validate_nonnegative(timeout);
 
    if (PG_ARGISNULL(2))
        PG_RETURN_INT32(dbms_pipe_receive_to_local_buffer(
            &key, timeout, DbmsPipeReceiveMode::NORMAL));
 
    const text *const cache_func = PG_GETARG_TEXT_PP(2);
    int32 result = dbms_pipe_receive_to_local_buffer(
        &key, 0, DbmsPipeReceiveMode::SINGLETON_CACHE);
 
    if (result == 0 || result == 8)
        PG_RETURN_INT32(result);
 
    const int32 cache_result =
        dbms_pipe_invoke_cache_function(cache_func, pipename);
    if (cache_result != 0)
    {
        dbms_pipe_buffer_receive_failed();
        PG_RETURN_INT32(cache_result);
    }
 
    /* Время выполнения cache_func не расходует RECEIVE_MESSAGE timeout. */
    result = dbms_pipe_receive_to_local_buffer(
        &key, timeout, DbmsPipeReceiveMode::SINGLETON_CACHE);
    PG_RETURN_INT32(result);
}
 
Datum
dbms_pipe_receive_message_ovr2(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("RECEIVE_MESSAGE");
    PG_RETURN_NULL();
}
 
Datum
dbms_pipe_receive_message_ovr3(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("RECEIVE_MESSAGE");
    PG_RETURN_NULL();
}
 
Datum
dbms_pipe_set_credential_name(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("SET_CREDENTIAL_NAME");
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_set_location_uri(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("SET_LOCATION_URI");
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_get_credential_name(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("GET_CREDENTIAL_NAME");
    PG_RETURN_NULL();
}
 
Datum
dbms_pipe_get_location_uri(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("GET_LOCATION_URI");
    PG_RETURN_NULL();
}
 
Datum
dbms_pipe_purge_ovr1(PG_FUNCTION_ARGS)
{
    const bool pipename_is_null = PG_ARGISNULL(0);
    const text *pipename = pipename_is_null ? nullptr : PG_GETARG_TEXT_PP(0);
    const DbmsPipeNameKey key =
        dbms_pipe_make_name_key(pipename, pipename_is_null);
 
    dbms_pipe_queue_purge(&key);
    dbms_pipe_buffer_purge_read_state();
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_purge_ovr2(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("PURGE");
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_purge_ovr3(PG_FUNCTION_ARGS)
{
    dbms_pipe_report_not_supported("PURGE");
    PG_RETURN_VOID();
}
 
Datum
dbms_pipe_unique_session_name(PG_FUNCTION_ARGS)
{
    static constexpr uint64 max_session_id = UINT64CONST(0xFFFFFFFFFFFF);
    static char session_name[sizeof("ORA$PIPE$000000000000")] = {};
 
    if (session_name[0] == '\0')
    {
        const uint64 session_id = dbms_pipe_registry_next_session_id();
 
        if (session_id == 0 || session_id > max_session_id)
            ereport(ERROR,
                    (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                     errmsg("DBMS_PIPE session name space is exhausted")));
 
        const int written = snprintf(session_name, sizeof(session_name),
                                 "ORA$PIPE$%012" PRIX64, session_id);
        if (written != static_cast<int>(sizeof(session_name) - 1))
            elog(ERROR, "could not format DBMS_PIPE session name");
    }
 
    PG_RETURN_TEXT_P(cstring_to_text(session_name));
}