Исходный код СУБД 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
#ifndef DBMS_VECTOR_ONNX_INTERNAL_HPP
#define DBMS_VECTOR_ONNX_INTERNAL_HPP
 
/*
 * DBMS_VECTOR local ONNX inference core (provider 'database').
 *
 * Wraps the ONNX Runtime C API.  The module is compiled both with and without
 * --with-onnxruntime: without the flag every call degrades gracefully (the
 * provider dispatch in dbms_vector.cpp raises the deferred-inference error
 * instead), so builds without the runtime keep working unchanged.
 */
 
#include <string>
#include <vector>
 
extern "C"
{
 
#include "postgres.h"
#include "utils/jsonb.h"
 
}
 
/// True when this server was built with the ONNX runtime (--with-onnxruntime).
bool dbms_vector_onnx_available();
 
/*
 * Validate ONNX model bytes with the runtime before they are stored:
 * the protobuf must parse (an OrtSession must initialize from the bytes) and
 * the model must satisfy the embedding contract - a string input (the
 * tokenizer fused into the graph, Oracle 23ai parity) and a float output.
 * Input/output selection honors the Oracle metadata mapping
 * {"input":{"<graph input>":["DATA"]},"embeddingOutput":"<graph output>"}.
 * Raises ereport ERROR on any violation; a no-op without the runtime.
 */
void dbms_vector_onnx_validate(bytea *model_data, Jsonb *metadata);
 
/*
 * Run the embedding model over a batch of texts.  A fresh session is created
 * per call (the one-shot path for unpinned models) and destroyed before
 * returning or raising.  Returns one vector text form "[%.8g,...]" per
 * input.
 */
std::vector<std::string> dbms_vector_onnx_run(const bytea *model_data,
                                              Jsonb *metadata,
                                              const std::vector<std::string> &inputs);
 
/*
 * Cache-aware run for the provider 'database' path: when the model is
 * pinned in this backend's session cache with the same owner, name and
 * content digest, the cached session runs the batch (no per-call session
 * creation); otherwise the call degrades to the one-shot path.  Cache
 * entries older than the shared generation counter (bumped by
 * DROP_ONNX_MODEL) are discarded first.  Without the runtime this is
 * never reached (the provider dispatch raises first).
 */
std::vector<std::string> dbms_vector_onnx_run_cached(
    const char *owner, const char *model_name, const bytea *model_data,
    Jsonb *metadata, const std::vector<std::string> &inputs);
 
/*
 * INMEMORY_ONNX_MODEL: create the session of a stored model once and keep
 * it in this backend's session cache for the lifetime of the backend (the
 * pin).  Raises when the runtime is absent (same flavor error as the
 * provider dispatch).  The params argument of the SQL surface is accepted
 * and ignored.
 */
void dbms_vector_onnx_pin(const char *owner, const char *model_name,
                          const bytea *model_data, Jsonb *metadata);
 
/*
 * Bump the cluster-wide model generation counter; every backend discards
 * its cached sessions once it observes the new value.  Called by
 * DROP_ONNX_MODEL.  Cheap and runtime-independent.
 */
void dbms_vector_onnx_bump_generation();
 
#endif // DBMS_VECTOR_ONNX_INTERNAL_HPP