Исходный код СУБД 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
/*--------------------------------------------------------------------------
 *
 * test_custom_types.c
 *      Test module for a set of functions for custom types.
 *
 * The custom type used in this module is similar to int4 for simplicity,
 * except that it is able to use various typanalyze functions to enforce
 * check patterns with ANALYZE.
 *
 * Copyright (c) 1996-2026, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *      src/test/modules/test_custom_types/test_custom_types.c
 *
 *--------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "fmgr.h"
#include "commands/vacuum.h"
#include "utils/builtins.h"
 
PG_MODULE_MAGIC;
 
/* Function declarations */
PG_FUNCTION_INFO_V1(int_custom_in);
PG_FUNCTION_INFO_V1(int_custom_out);
PG_FUNCTION_INFO_V1(int_custom_typanalyze_false);
PG_FUNCTION_INFO_V1(int_custom_typanalyze_invalid);
PG_FUNCTION_INFO_V1(int_custom_typanalyze_false_target);
PG_FUNCTION_INFO_V1(int_custom_typanalyze_false_second);
PG_FUNCTION_INFO_V1(int_custom_eq);
PG_FUNCTION_INFO_V1(int_custom_ne);
PG_FUNCTION_INFO_V1(int_custom_lt);
PG_FUNCTION_INFO_V1(int_custom_le);
PG_FUNCTION_INFO_V1(int_custom_gt);
PG_FUNCTION_INFO_V1(int_custom_ge);
PG_FUNCTION_INFO_V1(int_custom_cmp);
 
/*
 * int_custom_in - input function for int_custom type
 *
 * Converts a string to a int_custom (which is just an int32 internally).
 */
Datum
int_custom_in(PG_FUNCTION_ARGS)
{
    char       *num = PG_GETARG_CSTRING(0);
 
    PG_RETURN_INT32(pg_strtoint32_safe(num, fcinfo->context));
}
 
/*
 * int_custom_out - output function for int_custom type
 *
 * Converts a int_custom to a string.
 */
Datum
int_custom_out(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    char       *result = (char *) palloc(12);   /* sign, 10 digits, '\0' */
 
    pg_ltoa(arg1, result);
    PG_RETURN_CSTRING(result);
}
 
/*
 * int_custom_typanalyze_false - typanalyze function that returns false
 *
 * This function returns false, to simulate a type that cannot be analyzed.
 */
Datum
int_custom_typanalyze_false(PG_FUNCTION_ARGS)
{
    PG_RETURN_BOOL(false);
}
 
/*
 * Callback used to compute invalid statistics.
 */
static void
int_custom_invalid_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
                         int samplerows, double totalrows)
{
    /* We are not valid, and do not want to be. */
    stats->stats_valid = false;
}
 
/*
 * int_custom_typanalyze_invalid
 *
 * This function sets some invalid stats data, letting the caller know that
 * we are safe for an analyze, returning true.
 */
Datum
int_custom_typanalyze_invalid(PG_FUNCTION_ARGS)
{
    VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0);
 
    /* If the attstattarget column is negative, use the default value */
    if (stats->attstattarget < 0)
        stats->attstattarget = default_statistics_target;
 
    /* Buggy number, no need to care as long as it is positive */
    stats->minrows = 300;
 
    /* Set callback to compute some invalid stats */
    stats->compute_stats = int_custom_invalid_stats;
 
    PG_RETURN_BOOL(true);
}
 
/*
 * int_custom_typanalyze_false_target
 *
 * Returns false with a stats target.
 */
Datum
int_custom_typanalyze_false_target(PG_FUNCTION_ARGS)
{
    VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0);
 
    if (stats->attstattarget >= 0)
        PG_RETURN_BOOL(false);
 
    PG_RETURN_BOOL(std_typanalyze(stats));
}
 
/*
 * int_custom_typanalyze_false_second
 *
 * Returns false on the second call with a stats target.
 */
Datum
int_custom_typanalyze_false_second(PG_FUNCTION_ARGS)
{
    static int  ncalls = 0;
    VacAttrStats *stats = (VacAttrStats *) PG_GETARG_POINTER(0);
 
    if (stats->attstattarget < 0)
        ncalls = 0;
    else if (++ncalls > 1)
        PG_RETURN_BOOL(false);
 
    PG_RETURN_BOOL(std_typanalyze(stats));
}
 
/*
 * Comparison functions for int_custom type
 */
Datum
int_custom_eq(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    PG_RETURN_BOOL(arg1 == arg2);
}
 
Datum
int_custom_ne(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    PG_RETURN_BOOL(arg1 != arg2);
}
 
Datum
int_custom_lt(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    PG_RETURN_BOOL(arg1 < arg2);
}
 
Datum
int_custom_le(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    PG_RETURN_BOOL(arg1 <= arg2);
}
 
Datum
int_custom_gt(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    PG_RETURN_BOOL(arg1 > arg2);
}
 
Datum
int_custom_ge(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    PG_RETURN_BOOL(arg1 >= arg2);
}
 
Datum
int_custom_cmp(PG_FUNCTION_ARGS)
{
    int32       arg1 = PG_GETARG_INT32(0);
    int32       arg2 = PG_GETARG_INT32(1);
 
    if (arg1 < arg2)
        PG_RETURN_INT32(-1);
    else if (arg1 > arg2)
        PG_RETURN_INT32(1);
    else
        PG_RETURN_INT32(0);
}