-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathutils.h
More file actions
408 lines (343 loc) · 13 KB
/
Copy pathutils.h
File metadata and controls
408 lines (343 loc) · 13 KB
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
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#pragma once
#include <postgres.h>
#include <access/htup_details.h>
#include <access/tupdesc.h>
#include <catalog/namespace.h>
#include <catalog/pg_proc.h>
#include <common/int.h>
#include <debug_assert.h>
#include <foreign/foreign.h>
#include <nodes/extensible.h>
#include <nodes/pathnodes.h>
#include <optimizer/paths.h>
#include <utils/builtins.h>
#include <utils/datetime.h>
#include <utils/jsonb.h>
#include "compat/compat.h"
#include "process_utility.h"
/*
* Macro for debug messages that should *only* be present in debug builds but
* which should be removed in release builds. This is typically used for
* debug builds for development purposes.
*
* Note that some debug messages might be relevant to deploy in release build
* for debugging production systems. This macro is *not* for those cases.
*/
#ifdef TS_DEBUG
#define TS_DEBUG_LOG(FMT, ...) elog(DEBUG2, "%s - " FMT, __func__, ##__VA_ARGS__)
#else
#define TS_DEBUG_LOG(FMT, ...)
#endif
#define UnassignedDatum (Datum) 0
static inline int64
interval_to_usec(const Interval *interval)
{
return (interval->month * DAYS_PER_MONTH * USECS_PER_DAY) + (interval->day * USECS_PER_DAY) +
interval->time;
}
/*
* Get the function name in a PG_FUNCTION.
*
* The function name is resolved from the function Oid in the functioncall
* data. However, this information is not present in case of a direct function
* call, so fall back to the C-function name.
*/
#define TS_FUNCNAME() \
(psprintf("%s()", fcinfo->flinfo ? get_func_name(FC_FN_OID(fcinfo)) : __func__))
#define TS_PREVENT_FUNC_IF_READ_ONLY() PreventCommandIfReadOnly(TS_FUNCNAME())
#define TS_PREVENT_IN_TRANSACTION_BLOCK(CMD) \
do \
{ \
bool _isTopLevel = ts_process_utility_is_top_level(); \
/* Reset context before calling PreventInTransactionBlock in case it aborts. */ \
ts_process_utility_context_reset(); \
PreventInTransactionBlock(_isTopLevel, (CMD)); \
} while (0)
#define MAX(x, y) ((x) > (y) ? x : y)
#define MIN(x, y) ((x) < (y) ? x : y)
static inline bool
contains_volatile_functions_checker(Oid func_id, void *context)
{
return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
}
/* find the length of a statically sized array */
#define TS_ARRAY_LEN(array) (sizeof(array) / sizeof(*array))
extern TSDLLEXPORT bool ts_type_is_int8_binary_compatible(Oid sourcetype);
typedef bool (*proc_filter)(Form_pg_proc form, void *arg);
/*
* Convert a column value into the internal time representation.
* cannot store a timestamp earlier than MIN_TIMESTAMP, or greater than
* END_TIMESTAMP - TS_EPOCH_DIFF_MICROSECONDS
* nor dates that cannot be translated to timestamps
* Will throw an error for that, or other conversion issues.
*/
extern TSDLLEXPORT int64 ts_time_value_to_internal(Datum time_val, Oid type);
extern int64 ts_time_value_to_internal_or_infinite(Datum time_val, Oid type_oid);
extern TSDLLEXPORT int64 ts_interval_value_to_internal(Datum time_val, Oid type_oid);
/*
* Convert a column from the internal time representation into the specified type
*/
extern TSDLLEXPORT Datum ts_internal_to_time_value(int64 value, Oid type);
extern TSDLLEXPORT int64 ts_internal_to_time_int64(int64 value, Oid type);
extern TSDLLEXPORT Datum ts_internal_to_interval_value(int64 value, Oid type);
extern TSDLLEXPORT char *ts_datum_to_string(Datum value, Oid type);
extern TSDLLEXPORT char *ts_internal_to_time_string(int64 value, Oid type);
/*
* Return the period in microseconds of the first argument to date_trunc.
* This is approximate -- to be used for planning;
*/
extern int64 ts_date_trunc_interval_period_approx(text *units);
/*
* Return the interval period in microseconds.
* This is approximate -- to be used for planning;
*/
extern TSDLLEXPORT int64 ts_get_interval_period_approx(Interval *interval);
extern TSDLLEXPORT Oid ts_inheritance_parent_relid(Oid relid);
extern Oid ts_lookup_proc_filtered(const char *schema, const char *funcname, Oid *rettype,
proc_filter filter, void *filter_arg);
extern Oid ts_get_operator(const char *name, Oid namespace, Oid left, Oid right);
extern bool ts_function_types_equal(Oid left[], Oid right[], int nargs);
extern TSDLLEXPORT Oid ts_get_function_oid(const char *funcname, const char *schema_name, int nargs,
Oid arg_types[]);
extern TSDLLEXPORT Oid ts_get_cast_func(Oid source, Oid target);
typedef struct Dimension Dimension;
extern TSDLLEXPORT Oid ts_get_integer_now_func(const Dimension *open_dim, bool fail_if_not_found);
extern TSDLLEXPORT int64 ts_sub_integer_from_now(int64 interval, Oid time_dim_type, Oid now_func);
extern TSDLLEXPORT void *ts_create_struct_from_slot(TupleTableSlot *slot, MemoryContext mctx,
size_t alloc_size, size_t copy_size);
extern TSDLLEXPORT AppendRelInfo *ts_get_appendrelinfo(PlannerInfo *root, Index rti,
bool missing_ok);
extern TSDLLEXPORT Expr *ts_find_em_expr_for_rel(EquivalenceClass *ec, RelOptInfo *rel);
extern TSDLLEXPORT EquivalenceMember *ts_find_em_for_rel(EquivalenceClass *ec, RelOptInfo *rel);
extern TSDLLEXPORT bool ts_has_row_security(Oid relid);
extern TSDLLEXPORT bool ts_has_owner_privs(Oid userid, Oid ownerid);
extern TSDLLEXPORT List *ts_get_reloptions(Oid relid);
#define STRUCT_FROM_SLOT(slot, mctx, to_type, form_type) \
(to_type *) ts_create_struct_from_slot(slot, mctx, sizeof(to_type), sizeof(form_type));
/* note PG10 has_superclass but PG96 does not so use this */
#define is_inheritance_child(relid) (OidIsValid(ts_inheritance_parent_relid((relid))))
#define is_inheritance_parent(relid) \
(find_inheritance_children(table_relid, AccessShareLock) != NIL)
#define is_inheritance_table(relid) (is_inheritance_child(relid) || is_inheritance_parent(relid))
#define INIT_NULL_DATUM \
{ \
.value = 0, .isnull = true \
}
static inline Datum
ts_fetch_att(const void *T, bool attbyval, int attlen)
{
/* Length should be set to something sensible, otherwise an error will be
* raised by fetch_att, so we assert this here to get a stack for
* violations. */
Assert(!attbyval || (attlen > 0 && attlen <= 8));
return fetch_att(T, attbyval, attlen);
}
static inline int64
int64_min(int64 a, int64 b)
{
if (a <= b)
{
return a;
}
return b;
}
static inline int64
int64_saturating_add(int64 a, int64 b)
{
int64 result;
bool overflowed = pg_add_s64_overflow(a, b, &result);
if (overflowed)
{
result = a < 0 ? PG_INT64_MIN : PG_INT64_MAX;
}
return result;
}
static inline int64
int64_saturating_sub(int64 a, int64 b)
{
int64 result;
bool overflowed = pg_sub_s64_overflow(a, b, &result);
if (overflowed)
{
result = b < 0 ? PG_INT64_MAX : PG_INT64_MIN;
}
return result;
}
static inline bool
ts_flags_are_set_32(uint32 bitmap, uint32 flags)
{
return (bitmap & flags) == flags;
}
static inline pg_nodiscard uint32
ts_set_flags_32(uint32 bitmap, uint32 flags)
{
return bitmap | flags;
}
static inline uint32
ts_clear_flags_32(uint32 bitmap, uint32 flags)
{
return bitmap & ~flags;
}
/**
* Try to register a custom scan method.
*
* When registering a custom scan node, it might be called multiple times when
* different databases have different versions of the extension installed, so
* this function can be used to try to register a custom scan method but not
* fail if it has already been registered.
*/
static inline void
TryRegisterCustomScanMethods(const CustomScanMethods *methods)
{
if (!GetCustomScanMethods(methods->CustomName, true))
{
RegisterCustomScanMethods(methods);
}
}
typedef struct RelationSize
{
int64 total_size;
int64 heap_size;
int64 toast_size;
int64 index_size;
} RelationSize;
extern TSDLLEXPORT RelationSize ts_relation_size_impl(Oid relid);
extern TSDLLEXPORT const char *ts_get_node_name(Node *node);
extern TSDLLEXPORT int ts_get_relnatts(Oid relid);
extern TSDLLEXPORT void ts_alter_table_with_event_trigger(Oid relid, Node *cmd, List *cmds,
bool recurse);
extern TSDLLEXPORT void ts_copy_relation_acl(const Oid source_relid, const Oid target_relid,
const Oid owner_id);
extern TSDLLEXPORT bool ts_relation_has_tuples(Relation rel);
extern TSDLLEXPORT bool ts_table_has_tuples(Oid table_relid, LOCKMODE lockmode);
extern TSDLLEXPORT AttrNumber ts_map_attno(Oid src_rel, Oid dst_rel, AttrNumber attno);
/*
* Return Oid for a schema-qualified relation.
*/
static inline Oid
ts_get_relation_relid(char const *schema_name, char const *relation_name, bool return_invalid)
{
Oid schema_oid = get_namespace_oid(schema_name, true);
if (OidIsValid(schema_oid))
{
Oid rel_oid = get_relname_relid(relation_name, schema_oid);
if (!return_invalid)
{
Ensure(OidIsValid(rel_oid), "relation \"%s.%s\" not found", schema_name, relation_name);
}
return rel_oid;
}
else
{
if (!return_invalid)
{
Ensure(OidIsValid(schema_oid),
"schema \"%s\" not found (during lookup of relation \"%s.%s\")",
schema_name,
schema_name,
relation_name);
}
return InvalidOid;
}
}
struct Hypertable;
void replace_now_mock_walker(PlannerInfo *root, Node *clause, Oid funcid);
extern TSDLLEXPORT HeapTuple ts_heap_form_tuple(TupleDesc tupleDescriptor, NullableDatum *datums);
static inline void
ts_datum_set_text_from_cstring(const AttrNumber attno, NullableDatum *datums, const char *value)
{
if (value != NULL)
{
datums[AttrNumberGetAttrOffset(attno)].value = PointerGetDatum(cstring_to_text(value));
datums[AttrNumberGetAttrOffset(attno)].isnull = false;
}
else
{
datums[AttrNumberGetAttrOffset(attno)].isnull = true;
}
}
static inline void
ts_datum_set_bool(const AttrNumber attno, NullableDatum *datums, const bool value,
const bool isnull)
{
if (!isnull)
{
datums[AttrNumberGetAttrOffset(attno)].value = BoolGetDatum(value);
}
datums[AttrNumberGetAttrOffset(attno)].isnull = isnull;
}
static inline void
ts_datum_set_int32(const AttrNumber attno, NullableDatum *datums, const int32 value,
const bool isnull)
{
if (!isnull)
{
datums[AttrNumberGetAttrOffset(attno)].value = Int32GetDatum(value);
}
datums[AttrNumberGetAttrOffset(attno)].isnull = isnull;
}
static inline void
ts_datum_set_int64(const AttrNumber attno, NullableDatum *datums, const int64 value,
const bool isnull)
{
if (!isnull)
{
datums[AttrNumberGetAttrOffset(attno)].value = Int64GetDatum(value);
}
datums[AttrNumberGetAttrOffset(attno)].isnull = isnull;
}
static inline void
ts_datum_set_timestamptz(const AttrNumber attno, NullableDatum *datums, const TimestampTz value,
const bool isnull)
{
if (!isnull)
{
datums[AttrNumberGetAttrOffset(attno)].value = TimestampTzGetDatum(value);
}
datums[AttrNumberGetAttrOffset(attno)].isnull = isnull;
}
static inline void
ts_datum_set_jsonb(const AttrNumber attno, NullableDatum *datums, const Jsonb *value)
{
if (value != NULL)
{
datums[AttrNumberGetAttrOffset(attno)].value = JsonbPGetDatum(value);
datums[AttrNumberGetAttrOffset(attno)].isnull = false;
}
else
{
datums[AttrNumberGetAttrOffset(attno)].isnull = true;
}
}
static inline void
ts_datum_set_objectid(const AttrNumber attno, NullableDatum *datums, const Oid value)
{
if (OidIsValid(value))
{
datums[AttrNumberGetAttrOffset(attno)].value = ObjectIdGetDatum(value);
datums[AttrNumberGetAttrOffset(attno)].isnull = false;
}
else
{
datums[AttrNumberGetAttrOffset(attno)].isnull = true;
}
}
typedef void (*append_cell_func)(StringInfo, ListCell *);
extern TSDLLEXPORT void ts_get_rel_info_by_name(const char *relnamespace, const char *relname,
Oid *relid, char *relkind);
extern TSDLLEXPORT Oid ts_get_rel_am(Oid relid);
extern TSDLLEXPORT void ts_relation_set_reloption(Relation rel, List *options, LOCKMODE lockmode);
extern TSDLLEXPORT Jsonb *ts_errdata_to_jsonb(ErrorData *edata, Name proc_schema, Name proc_name);
extern TSDLLEXPORT char *ts_get_attr_expr(Relation rel, AttrNumber attno);
extern TSDLLEXPORT char *ts_list_to_string(List *list, append_cell_func append);
extern TSDLLEXPORT List *ts_find_aggrefs(Node *node);
extern TSDLLEXPORT bool ts_is_time_bucket_function(Expr *node);
extern TSDLLEXPORT bool ts_get_attnotnull(Oid relid, AttrNumber attno);
extern TransactionId ts_relation_max_modifying_xid(Oid relid);
extern bool ts_relation_xid_blocks_logical_slot(TransactionId max_xid, NameData *blocking_slot,
TransactionId *blocking_catalog_xmin);