PostgreSQL Source Code git master
Loading...
Searching...
No Matches
postgres.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * postgres.h
4 * Primary include file for PostgreSQL server .c files
5 *
6 * This should be the first file included by PostgreSQL backend modules.
7 * Client-side code should include postgres_fe.h instead.
8 *
9 *
10 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1995, Regents of the University of California
12 *
13 * src/include/postgres.h
14 *
15 *-------------------------------------------------------------------------
16 */
17/* IWYU pragma: always_keep */
18/*
19 *----------------------------------------------------------------
20 * TABLE OF CONTENTS
21 *
22 * When adding stuff to this file, please try to put stuff
23 * into the relevant section, or add new sections as appropriate.
24 *
25 * section description
26 * ------- ------------------------------------------------
27 * 1) Datum type + support functions
28 * 2) miscellaneous
29 *
30 * NOTES
31 *
32 * In general, this file should contain declarations that are widely needed
33 * in the backend environment, but are of no interest outside the backend.
34 *
35 * Simple type definitions live in c.h, where they are shared with
36 * postgres_fe.h. We do that since those type definitions are needed by
37 * frontend modules that want to deal with binary data transmission to or
38 * from the backend. Type definitions in this file should be for
39 * representations that never escape the backend, such as Datum.
40 *
41 *----------------------------------------------------------------
42 */
43#ifndef POSTGRES_H
44#define POSTGRES_H
45
46/* IWYU pragma: begin_exports */
47
48#include "c.h"
49#include "utils/elog.h"
50#include "utils/palloc.h"
51
52/* IWYU pragma: end_exports */
53
54/* ----------------------------------------------------------------
55 * Section 1: Datum type + support functions
56 * ----------------------------------------------------------------
57 */
58
59/*
60 * A Datum contains either a value of a pass-by-value type or a pointer to a
61 * value of a pass-by-reference type. Therefore, we must have
62 * sizeof(Datum) >= sizeof(void *). No current or foreseeable Postgres
63 * platform has pointers wider than 8 bytes, and standardizing on Datum being
64 * exactly 8 bytes has advantages in reducing cross-platform differences.
65 *
66 * The functions below and the analogous functions for other types should be used to
67 * convert between a Datum and the appropriate C type.
68 */
69
71
72/*
73 * This symbol is now vestigial, but we continue to define it so as not to
74 * unnecessarily break extension code.
75 */
76#define SIZEOF_DATUM 8
77
78/*
79 * A NullableDatum is used in places where both a Datum and its nullness needs
80 * to be stored. This can be more efficient than storing datums and nullness
81 * in separate arrays, due to better spatial locality, even if more space may
82 * be wasted due to padding.
83 */
84typedef struct NullableDatum
85{
86#define FIELDNO_NULLABLE_DATUM_DATUM 0
88#define FIELDNO_NULLABLE_DATUM_ISNULL 1
89 bool isnull;
90 /* due to alignment padding this could be used for flags for free */
92
93/*
94 * DatumGetBool
95 * Returns boolean value of a datum.
96 *
97 * Note: any nonzero value will be considered true.
98 */
99static inline bool
101{
102 return (X != 0);
103}
104
105/*
106 * BoolGetDatum
107 * Returns datum representation for a boolean.
108 *
109 * Note: any nonzero value will be considered true.
110 */
111static inline Datum
113{
114 return (Datum) (X ? 1 : 0);
115}
116
117/*
118 * DatumGetChar
119 * Returns character value of a datum.
120 */
121static inline char
123{
124 return (char) X;
125}
126
127/*
128 * CharGetDatum
129 * Returns datum representation for a character.
130 */
131static inline Datum
133{
134 return (Datum) X;
135}
136
137/*
138 * DatumGetUInt8
139 * Returns 8-bit unsigned integer value of a datum.
140 */
141static inline uint8
143{
144 return (uint8) X;
145}
146
147/*
148 * UInt8GetDatum
149 * Returns datum representation for an 8-bit unsigned integer.
150 */
151static inline Datum
153{
154 return (Datum) X;
155}
156
157/*
158 * DatumGetInt16
159 * Returns 16-bit integer value of a datum.
160 */
161static inline int16
163{
164 return (int16) X;
165}
166
167/*
168 * Int16GetDatum
169 * Returns datum representation for a 16-bit integer.
170 */
171static inline Datum
173{
174 return (Datum) X;
175}
176
177/*
178 * DatumGetUInt16
179 * Returns 16-bit unsigned integer value of a datum.
180 */
181static inline uint16
183{
184 return (uint16) X;
185}
186
187/*
188 * UInt16GetDatum
189 * Returns datum representation for a 16-bit unsigned integer.
190 */
191static inline Datum
193{
194 return (Datum) X;
195}
196
197/*
198 * DatumGetInt32
199 * Returns 32-bit integer value of a datum.
200 */
201static inline int32
203{
204 return (int32) X;
205}
206
207/*
208 * Int32GetDatum
209 * Returns datum representation for a 32-bit integer.
210 */
211static inline Datum
213{
214 return (Datum) X;
215}
216
217/*
218 * DatumGetUInt32
219 * Returns 32-bit unsigned integer value of a datum.
220 */
221static inline uint32
223{
224 return (uint32) X;
225}
226
227/*
228 * UInt32GetDatum
229 * Returns datum representation for a 32-bit unsigned integer.
230 */
231static inline Datum
233{
234 return (Datum) X;
235}
236
237/*
238 * DatumGetObjectId
239 * Returns object identifier value of a datum.
240 */
241static inline Oid
243{
244 return (Oid) X;
245}
246
247/*
248 * ObjectIdGetDatum
249 * Returns datum representation for an object identifier.
250 */
251static inline Datum
253{
254 return (Datum) X;
255}
256
257/*
258 * DatumGetObjectId8
259 * Returns 8-byte object identifier value of a datum.
260 */
261static inline Oid8
263{
264 return (Oid8) X;
265}
266
267/*
268 * ObjectId8GetDatum
269 * Returns datum representation for an 8-byte object identifier
270 */
271static inline Datum
273{
274 return (Datum) X;
275}
276
277/*
278 * DatumGetTransactionId
279 * Returns transaction identifier value of a datum.
280 */
281static inline TransactionId
283{
284 return (TransactionId) X;
285}
286
287/*
288 * TransactionIdGetDatum
289 * Returns datum representation for a transaction identifier.
290 */
291static inline Datum
293{
294 return (Datum) X;
295}
296
297/*
298 * MultiXactIdGetDatum
299 * Returns datum representation for a multixact identifier.
300 */
301static inline Datum
303{
304 return (Datum) X;
305}
306
307/*
308 * DatumGetCommandId
309 * Returns command identifier value of a datum.
310 */
311static inline CommandId
313{
314 return (CommandId) X;
315}
316
317/*
318 * CommandIdGetDatum
319 * Returns datum representation for a command identifier.
320 */
321static inline Datum
323{
324 return (Datum) X;
325}
326
327/*
328 * DatumGetPointer
329 * Returns pointer value of a datum.
330 */
331static inline Pointer
333{
334 return (Pointer) (uintptr_t) X;
335}
336
337/*
338 * PointerGetDatum
339 * Returns datum representation for a pointer.
340 */
341static inline Datum
342PointerGetDatum(const void *X)
343{
344 return (Datum) (uintptr_t) X;
345}
346
347/*
348 * DatumGetCString
349 * Returns C string (null-terminated string) value of a datum.
350 *
351 * Note: C string is not a full-fledged Postgres type at present,
352 * but type input functions use this conversion for their inputs.
353 */
354static inline char *
356{
357 return (char *) DatumGetPointer(X);
358}
359
360/*
361 * CStringGetDatum
362 * Returns datum representation for a C string (null-terminated string).
363 *
364 * Note: C string is not a full-fledged Postgres type at present,
365 * but type output functions use this conversion for their outputs.
366 * Note: CString is pass-by-reference; caller must ensure the pointed-to
367 * value has adequate lifetime.
368 */
369static inline Datum
370CStringGetDatum(const char *X)
371{
372 return PointerGetDatum(X);
373}
374
375/*
376 * DatumGetName
377 * Returns name value of a datum.
378 */
379static inline Name
381{
382 return (Name) DatumGetPointer(X);
383}
384
385/*
386 * NameGetDatum
387 * Returns datum representation for a name.
388 *
389 * Note: Name is pass-by-reference; caller must ensure the pointed-to
390 * value has adequate lifetime.
391 */
392static inline Datum
394{
395 return CStringGetDatum(NameStr(*X));
396}
397
398/*
399 * DatumGetInt64
400 * Returns 64-bit integer value of a datum.
401 */
402static inline int64
404{
405 return (int64) X;
406}
407
408/*
409 * Int64GetDatum
410 * Returns datum representation for a 64-bit integer.
411 */
412static inline Datum
414{
415 return (Datum) X;
416}
417
418/*
419 * DatumGetUInt64
420 * Returns 64-bit unsigned integer value of a datum.
421 */
422static inline uint64
424{
425 return (uint64) X;
426}
427
428/*
429 * UInt64GetDatum
430 * Returns datum representation for a 64-bit unsigned integer.
431 */
432static inline Datum
434{
435 return (Datum) X;
436}
437
438/*
439 * Float <-> Datum conversions
440 *
441 * These have to be implemented as inline functions rather than macros, when
442 * passing by value, because many machines pass int and float function
443 * parameters/results differently; so we need to play weird games with unions.
444 */
445
446/*
447 * DatumGetFloat4
448 * Returns 4-byte floating point value of a datum.
449 */
450static inline float4
452{
453 union
454 {
455 int32 value;
456 float4 retval;
457 } myunion;
458
459 myunion.value = DatumGetInt32(X);
460 return myunion.retval;
461}
462
463/*
464 * Float4GetDatum
465 * Returns datum representation for a 4-byte floating point number.
466 */
467static inline Datum
469{
470 union
471 {
473 int32 retval;
474 } myunion;
475
476 myunion.value = X;
477 return Int32GetDatum(myunion.retval);
478}
479
480/*
481 * DatumGetFloat8
482 * Returns 8-byte floating point value of a datum.
483 */
484static inline float8
486{
487 union
488 {
489 int64 value;
490 float8 retval;
491 } myunion;
492
493 myunion.value = DatumGetInt64(X);
494 return myunion.retval;
495}
496
497/*
498 * Float8GetDatum
499 * Returns datum representation for an 8-byte floating point number.
500 */
501static inline Datum
503{
504 union
505 {
507 int64 retval;
508 } myunion;
509
510 myunion.value = X;
511 return Int64GetDatum(myunion.retval);
512}
513
514/*
515 * Int64GetDatumFast
516 * Float8GetDatumFast
517 *
518 * These macros were intended to allow writing code that does not depend on
519 * whether int64 and float8 are pass-by-reference types, while not
520 * sacrificing performance when they are. They are no longer different
521 * from the regular functions, though we keep the assertions to protect
522 * code that might get back-patched into older branches.
523 */
524
525#define Int64GetDatumFast(X) \
526 (StaticAssertVariableIsOfTypeMacro(X, int64), Int64GetDatum(X))
527#define Float8GetDatumFast(X) \
528 (StaticAssertVariableIsOfTypeMacro(X, double), Float8GetDatum(X))
529
530
531/* ----------------------------------------------------------------
532 * Section 2: miscellaneous
533 * ----------------------------------------------------------------
534 */
535
536/*
537 * pg_ternary
538 * Boolean value with an extra "unset" value
539 *
540 * This enum can be used for values that want to distinguish between true,
541 * false, and unset.
542*/
549
550/*
551 * NON_EXEC_STATIC: It's sometimes useful to define a variable or function
552 * that is normally static but extern when using EXEC_BACKEND (see
553 * pg_config_manual.h). There would then typically be some code in
554 * postmaster.c that uses those extern symbols to transfer state between
555 * processes or do whatever other things it needs to do in EXEC_BACKEND mode.
556 */
557#ifdef EXEC_BACKEND
558#define NON_EXEC_STATIC
559#else
560#define NON_EXEC_STATIC static
561#endif
562
563#endif /* POSTGRES_H */
#define NameStr(name)
Definition c.h:837
uint64 Oid8
Definition c.h:758
uint8_t uint8
Definition c.h:616
int64_t int64
Definition c.h:615
double float8
Definition c.h:716
TransactionId MultiXactId
Definition c.h:748
int16_t int16
Definition c.h:613
int32_t int32
Definition c.h:614
uint64_t uint64
Definition c.h:619
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
float float4
Definition c.h:715
void * Pointer
Definition c.h:609
uint32 CommandId
Definition c.h:752
uint32 TransactionId
Definition c.h:738
static struct @174 value
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:222
static Datum Int64GetDatum(int64 X)
Definition postgres.h:413
static uint64 DatumGetUInt64(Datum X)
Definition postgres.h:423
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static int64 DatumGetInt64(Datum X)
Definition postgres.h:403
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Name DatumGetName(Datum X)
Definition postgres.h:380
static Datum Float4GetDatum(float4 X)
Definition postgres.h:468
static Datum TransactionIdGetDatum(TransactionId X)
Definition postgres.h:292
static float4 DatumGetFloat4(Datum X)
Definition postgres.h:451
static Datum CommandIdGetDatum(CommandId X)
Definition postgres.h:322
pg_ternary
Definition postgres.h:544
@ PG_TERNARY_FALSE
Definition postgres.h:545
@ PG_TERNARY_TRUE
Definition postgres.h:546
@ PG_TERNARY_UNSET
Definition postgres.h:547
static Oid DatumGetObjectId(Datum X)
Definition postgres.h:242
static Datum UInt64GetDatum(uint64 X)
Definition postgres.h:433
static Datum Int16GetDatum(int16 X)
Definition postgres.h:172
static Datum MultiXactIdGetDatum(MultiXactId X)
Definition postgres.h:302
static CommandId DatumGetCommandId(Datum X)
Definition postgres.h:312
static Datum UInt16GetDatum(uint16 X)
Definition postgres.h:192
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
static float8 DatumGetFloat8(Datum X)
Definition postgres.h:485
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
static char * DatumGetCString(Datum X)
Definition postgres.h:355
static Datum ObjectId8GetDatum(Oid8 X)
Definition postgres.h:272
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:393
static uint16 DatumGetUInt16(Datum X)
Definition postgres.h:182
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static TransactionId DatumGetTransactionId(Datum X)
Definition postgres.h:282
static uint8 DatumGetUInt8(Datum X)
Definition postgres.h:142
static Datum UInt8GetDatum(uint8 X)
Definition postgres.h:152
static Datum Float8GetDatum(float8 X)
Definition postgres.h:502
static char DatumGetChar(Datum X)
Definition postgres.h:122
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
static int16 DatumGetInt16(Datum X)
Definition postgres.h:162
static Datum UInt32GetDatum(uint32 X)
Definition postgres.h:232
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
static Datum CharGetDatum(char X)
Definition postgres.h:132
static Oid8 DatumGetObjectId8(Datum X)
Definition postgres.h:262
unsigned int Oid
static int fb(int x)
Datum value
Definition postgres.h:87
Definition c.h:832