PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oid.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * oid.c
4 * Functions for the built-in type Oid ... also oidvector.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/utils/adt/oid.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include <ctype.h>
18#include <limits.h>
19
20#include "catalog/pg_type.h"
21#include "common/int.h"
22#include "libpq/pqformat.h"
23#include "nodes/miscnodes.h"
24#include "nodes/value.h"
25#include "utils/array.h"
26#include "utils/builtins.h"
27
28
29#define OidVectorSize(n) (offsetof(oidvector, values) + (n) * sizeof(Oid))
30
31
32/*****************************************************************************
33 * USER I/O ROUTINES *
34 *****************************************************************************/
35
38{
39 char *s = PG_GETARG_CSTRING(0);
40 Oid result;
41
42 result = uint32in_subr(s, NULL, "oid", fcinfo->context);
43 PG_RETURN_OID(result);
44}
45
48{
49 Oid o = PG_GETARG_OID(0);
50 char *result = (char *) palloc(12);
51
52 snprintf(result, 12, "%u", o);
53 PG_RETURN_CSTRING(result);
54}
55
56/*
57 * oidrecv - converts external binary format to oid
58 */
66
67/*
68 * oidsend - converts oid to binary format
69 */
80
81/*
82 * construct oidvector given a raw array of Oids
83 *
84 * If oids is NULL then caller must fill values[] afterward
85 */
87buildoidvector(const Oid *oids, int n)
88{
89 oidvector *result;
90
91 result = (oidvector *) palloc0(OidVectorSize(n));
92
93 if (n > 0 && oids)
94 memcpy(result->values, oids, n * sizeof(Oid));
95
96 /*
97 * Attach standard array header. For historical reasons, we set the index
98 * lower bound to 0 not 1.
99 */
100 SET_VARSIZE(result, OidVectorSize(n));
101 result->ndim = 1;
102 result->dataoffset = 0; /* never any nulls */
103 result->elemtype = OIDOID;
104 result->dim1 = n;
105 result->lbound1 = 0;
106
107 return result;
108}
109
110/*
111 * validate that an array object meets the restrictions of oidvector
112 *
113 * We need this because there are pathways by which a general oid[] array can
114 * be cast to oidvector, allowing the type's restrictions to be violated.
115 * All code that receives an oidvector as a SQL parameter should check this.
116 */
117void
119{
120 /*
121 * We insist on ndim == 1 and dataoffset == 0 (that is, no nulls) because
122 * otherwise the array's layout will not be what calling code expects. We
123 * needn't be picky about the index lower bound though. Checking elemtype
124 * is just paranoia.
125 */
126 if (oidArray->ndim != 1 ||
127 oidArray->dataoffset != 0 ||
128 oidArray->elemtype != OIDOID)
131 errmsg("array is not a valid oidvector")));
132}
133
134/*
135 * oidvectorin - converts "num num ..." to internal form
136 */
137Datum
139{
140 char *oidString = PG_GETARG_CSTRING(0);
141 Node *escontext = fcinfo->context;
142 oidvector *result;
143 int nalloc;
144 int n;
145
146 nalloc = 32; /* arbitrary initial size guess */
147 result = (oidvector *) palloc0(OidVectorSize(nalloc));
148
149 for (n = 0;; n++)
150 {
151 while (*oidString && isspace((unsigned char) *oidString))
152 oidString++;
153 if (*oidString == '\0')
154 break;
155
156 if (n >= nalloc)
157 {
158 nalloc *= 2;
159 result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
160 }
161
163 "oid", escontext);
164 if (SOFT_ERROR_OCCURRED(escontext))
166 }
167
168 SET_VARSIZE(result, OidVectorSize(n));
169 result->ndim = 1;
170 result->dataoffset = 0; /* never any nulls */
171 result->elemtype = OIDOID;
172 result->dim1 = n;
173 result->lbound1 = 0;
174
175 PG_RETURN_POINTER(result);
176}
177
178/*
179 * oidvectorout - converts internal form to "num num ..."
180 */
181Datum
183{
185 int num,
186 nnums;
187 char *rp;
188 char *result;
189
190 /* validate input before fetching dim1 */
192 nnums = oidArray->dim1;
193
194 /* assumes sign, 10 digits, ' ' */
195 rp = result = (char *) palloc(nnums * 12 + 1);
196 for (num = 0; num < nnums; num++)
197 {
198 if (num != 0)
199 *rp++ = ' ';
200 sprintf(rp, "%u", oidArray->values[num]);
201 while (*++rp != '\0')
202 ;
203 }
204 *rp = '\0';
205 PG_RETURN_CSTRING(result);
206}
207
208/*
209 * oidvectorrecv - converts external binary format to oidvector
210 */
211Datum
213{
216 oidvector *result;
217
218 /*
219 * Normally one would call array_recv() using DirectFunctionCall3, but
220 * that does not work since array_recv wants to cache some data using
221 * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
222 * parameter.
223 */
224 InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
226
227 locfcinfo->args[0].value = PointerGetDatum(buf);
228 locfcinfo->args[0].isnull = false;
229 locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
230 locfcinfo->args[1].isnull = false;
231 locfcinfo->args[2].value = Int32GetDatum(-1);
232 locfcinfo->args[2].isnull = false;
233
235
236 Assert(!locfcinfo->isnull);
237
238 /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
239 if (ARR_NDIM(result) != 1 ||
240 ARR_HASNULL(result) ||
241 ARR_ELEMTYPE(result) != OIDOID ||
242 ARR_LBOUND(result)[0] != 0)
245 errmsg("invalid oidvector data")));
246
247 PG_RETURN_POINTER(result);
248}
249
250/*
251 * oidvectorsend - converts oidvector to binary format
252 */
253Datum
255{
256 /* We don't do check_valid_oidvector, since array_send won't care */
257 return array_send(fcinfo);
258}
259
260/*
261 * oidparse - get OID from ICONST/FCONST node
262 */
263Oid
265{
266 switch (nodeTag(node))
267 {
268 case T_Integer:
269 return intVal(node);
270 case T_Float:
271
272 /*
273 * Values too large for int4 will be represented as Float
274 * constants by the lexer. Accept these if they are valid OID
275 * strings.
276 */
277 return uint32in_subr(castNode(Float, node)->fval, NULL,
278 "oid", NULL);
279 default:
280 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
281 }
282 return InvalidOid; /* keep compiler quiet */
283}
284
285/* qsort comparison function for Oids */
286int
287oid_cmp(const void *p1, const void *p2)
288{
289 Oid v1 = *((const Oid *) p1);
290 Oid v2 = *((const Oid *) p2);
291
292 return pg_cmp_u32(v1, v2);
293}
294
295
296/*****************************************************************************
297 * PUBLIC ROUTINES *
298 *****************************************************************************/
299
300Datum
308
309Datum
317
318Datum
326
327Datum
335
336Datum
344
345Datum
353
354Datum
362
363Datum
371
372Datum
379
380Datum
387
388Datum
395
396Datum
403
404Datum
411
412Datum
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_HASNULL(a)
Definition array.h:291
#define ARR_LBOUND(a)
Definition array.h:296
Datum array_recv(PG_FUNCTION_ARGS)
Datum array_send(PG_FUNCTION_ARGS)
#define Assert(condition)
Definition c.h:885
int32_t int32
Definition c.h:554
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition fmgr.h:150
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define LOCAL_FCINFO(name, nargs)
Definition fmgr.h:110
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_RETURN_OID(x)
Definition fmgr.h:361
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
static int pg_cmp_u32(uint32 a, uint32 b)
Definition int.h:719
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
void * palloc0(Size size)
Definition mcxt.c:1417
void * palloc(Size size)
Definition mcxt.c:1387
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
Datum btoidvectorcmp(PG_FUNCTION_ARGS)
Definition nbtcompare.c:585
#define nodeTag(nodeptr)
Definition nodes.h:139
#define castNode(_type_, nodeptr)
Definition nodes.h:182
uint32 uint32in_subr(const char *s, char **endloc, const char *typname, Node *escontext)
Definition numutils.c:897
Datum oidvectorin(PG_FUNCTION_ARGS)
Definition oid.c:138
Datum oidvectoreq(PG_FUNCTION_ARGS)
Definition oid.c:373
Datum oidvectorle(PG_FUNCTION_ARGS)
Definition oid.c:397
Datum oidvectorout(PG_FUNCTION_ARGS)
Definition oid.c:182
Datum oidvectorgt(PG_FUNCTION_ARGS)
Definition oid.c:413
Datum oidvectorge(PG_FUNCTION_ARGS)
Definition oid.c:405
Datum oidrecv(PG_FUNCTION_ARGS)
Definition oid.c:60
Datum oidsmaller(PG_FUNCTION_ARGS)
Definition oid.c:364
Datum oideq(PG_FUNCTION_ARGS)
Definition oid.c:301
Datum oidvectorsend(PG_FUNCTION_ARGS)
Definition oid.c:254
#define OidVectorSize(n)
Definition oid.c:29
Datum oidvectorrecv(PG_FUNCTION_ARGS)
Definition oid.c:212
Datum oidvectorlt(PG_FUNCTION_ARGS)
Definition oid.c:389
Oid oidparse(Node *node)
Definition oid.c:264
oidvector * buildoidvector(const Oid *oids, int n)
Definition oid.c:87
int oid_cmp(const void *p1, const void *p2)
Definition oid.c:287
Datum oidin(PG_FUNCTION_ARGS)
Definition oid.c:37
Datum oidlt(PG_FUNCTION_ARGS)
Definition oid.c:319
Datum oidge(PG_FUNCTION_ARGS)
Definition oid.c:337
Datum oidout(PG_FUNCTION_ARGS)
Definition oid.c:47
Datum oidsend(PG_FUNCTION_ARGS)
Definition oid.c:71
void check_valid_oidvector(const oidvector *oidArray)
Definition oid.c:118
Datum oidle(PG_FUNCTION_ARGS)
Definition oid.c:328
Datum oidne(PG_FUNCTION_ARGS)
Definition oid.c:310
Datum oidvectorne(PG_FUNCTION_ARGS)
Definition oid.c:381
Datum oidgt(PG_FUNCTION_ARGS)
Definition oid.c:346
Datum oidlarger(PG_FUNCTION_ARGS)
Definition oid.c:355
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define sprintf
Definition port.h:262
#define snprintf
Definition port.h:260
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
#define InvalidOid
unsigned int Oid
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144
static int fb(int x)
static int cmp(const chr *x, const chr *y, size_t len)
struct StringInfoData * StringInfo
Definition string.h:15
Definition value.h:48
Definition nodes.h:135
Definition c.h:757
int dim1
Definition c.h:762
int32 dataoffset
Definition c.h:760
Oid elemtype
Definition c.h:761
int lbound1
Definition c.h:763
int ndim
Definition c.h:759
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition c.h:764
#define intVal(v)
Definition value.h:79
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432