PostgreSQL Source Code  git master
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-2024, 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 
36 Datum
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 
46 Datum
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  */
59 Datum
61 {
63 
64  PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
65 }
66 
67 /*
68  * oidsend - converts oid to binary format
69  */
70 Datum
72 {
73  Oid arg1 = PG_GETARG_OID(0);
75 
77  pq_sendint32(&buf, arg1);
79 }
80 
81 /*
82  * construct oidvector given a raw array of Oids
83  *
84  * If oids is NULL then caller must fill values[] afterward
85  */
86 oidvector *
87 buildoidvector(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  * oidvectorin - converts "num num ..." to internal form
112  */
113 Datum
115 {
116  char *oidString = PG_GETARG_CSTRING(0);
117  Node *escontext = fcinfo->context;
118  oidvector *result;
119  int nalloc;
120  int n;
121 
122  nalloc = 32; /* arbitrary initial size guess */
123  result = (oidvector *) palloc0(OidVectorSize(nalloc));
124 
125  for (n = 0;; n++)
126  {
127  while (*oidString && isspace((unsigned char) *oidString))
128  oidString++;
129  if (*oidString == '\0')
130  break;
131 
132  if (n >= nalloc)
133  {
134  nalloc *= 2;
135  result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
136  }
137 
138  result->values[n] = uint32in_subr(oidString, &oidString,
139  "oid", escontext);
140  if (SOFT_ERROR_OCCURRED(escontext))
141  PG_RETURN_NULL();
142  }
143 
144  SET_VARSIZE(result, OidVectorSize(n));
145  result->ndim = 1;
146  result->dataoffset = 0; /* never any nulls */
147  result->elemtype = OIDOID;
148  result->dim1 = n;
149  result->lbound1 = 0;
150 
151  PG_RETURN_POINTER(result);
152 }
153 
154 /*
155  * oidvectorout - converts internal form to "num num ..."
156  */
157 Datum
159 {
160  oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
161  int num,
162  nnums = oidArray->dim1;
163  char *rp;
164  char *result;
165 
166  /* assumes sign, 10 digits, ' ' */
167  rp = result = (char *) palloc(nnums * 12 + 1);
168  for (num = 0; num < nnums; num++)
169  {
170  if (num != 0)
171  *rp++ = ' ';
172  sprintf(rp, "%u", oidArray->values[num]);
173  while (*++rp != '\0')
174  ;
175  }
176  *rp = '\0';
177  PG_RETURN_CSTRING(result);
178 }
179 
180 /*
181  * oidvectorrecv - converts external binary format to oidvector
182  */
183 Datum
185 {
186  LOCAL_FCINFO(locfcinfo, 3);
188  oidvector *result;
189 
190  /*
191  * Normally one would call array_recv() using DirectFunctionCall3, but
192  * that does not work since array_recv wants to cache some data using
193  * fcinfo->flinfo->fn_extra. So we need to pass it our own flinfo
194  * parameter.
195  */
196  InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
197  InvalidOid, NULL, NULL);
198 
199  locfcinfo->args[0].value = PointerGetDatum(buf);
200  locfcinfo->args[0].isnull = false;
201  locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
202  locfcinfo->args[1].isnull = false;
203  locfcinfo->args[2].value = Int32GetDatum(-1);
204  locfcinfo->args[2].isnull = false;
205 
206  result = (oidvector *) DatumGetPointer(array_recv(locfcinfo));
207 
208  Assert(!locfcinfo->isnull);
209 
210  /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
211  if (ARR_NDIM(result) != 1 ||
212  ARR_HASNULL(result) ||
213  ARR_ELEMTYPE(result) != OIDOID ||
214  ARR_LBOUND(result)[0] != 0)
215  ereport(ERROR,
216  (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
217  errmsg("invalid oidvector data")));
218 
219  PG_RETURN_POINTER(result);
220 }
221 
222 /*
223  * oidvectorsend - converts oidvector to binary format
224  */
225 Datum
227 {
228  return array_send(fcinfo);
229 }
230 
231 /*
232  * oidparse - get OID from ICONST/FCONST node
233  */
234 Oid
236 {
237  switch (nodeTag(node))
238  {
239  case T_Integer:
240  return intVal(node);
241  case T_Float:
242 
243  /*
244  * Values too large for int4 will be represented as Float
245  * constants by the lexer. Accept these if they are valid OID
246  * strings.
247  */
248  return uint32in_subr(castNode(Float, node)->fval, NULL,
249  "oid", NULL);
250  default:
251  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
252  }
253  return InvalidOid; /* keep compiler quiet */
254 }
255 
256 /* qsort comparison function for Oids */
257 int
258 oid_cmp(const void *p1, const void *p2)
259 {
260  Oid v1 = *((const Oid *) p1);
261  Oid v2 = *((const Oid *) p2);
262 
263  return pg_cmp_u32(v1, v2);
264 }
265 
266 
267 /*****************************************************************************
268  * PUBLIC ROUTINES *
269  *****************************************************************************/
270 
271 Datum
273 {
274  Oid arg1 = PG_GETARG_OID(0);
275  Oid arg2 = PG_GETARG_OID(1);
276 
277  PG_RETURN_BOOL(arg1 == arg2);
278 }
279 
280 Datum
282 {
283  Oid arg1 = PG_GETARG_OID(0);
284  Oid arg2 = PG_GETARG_OID(1);
285 
286  PG_RETURN_BOOL(arg1 != arg2);
287 }
288 
289 Datum
291 {
292  Oid arg1 = PG_GETARG_OID(0);
293  Oid arg2 = PG_GETARG_OID(1);
294 
295  PG_RETURN_BOOL(arg1 < arg2);
296 }
297 
298 Datum
300 {
301  Oid arg1 = PG_GETARG_OID(0);
302  Oid arg2 = PG_GETARG_OID(1);
303 
304  PG_RETURN_BOOL(arg1 <= arg2);
305 }
306 
307 Datum
309 {
310  Oid arg1 = PG_GETARG_OID(0);
311  Oid arg2 = PG_GETARG_OID(1);
312 
313  PG_RETURN_BOOL(arg1 >= arg2);
314 }
315 
316 Datum
318 {
319  Oid arg1 = PG_GETARG_OID(0);
320  Oid arg2 = PG_GETARG_OID(1);
321 
322  PG_RETURN_BOOL(arg1 > arg2);
323 }
324 
325 Datum
327 {
328  Oid arg1 = PG_GETARG_OID(0);
329  Oid arg2 = PG_GETARG_OID(1);
330 
331  PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
332 }
333 
334 Datum
336 {
337  Oid arg1 = PG_GETARG_OID(0);
338  Oid arg2 = PG_GETARG_OID(1);
339 
340  PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
341 }
342 
343 Datum
345 {
347 
348  PG_RETURN_BOOL(cmp == 0);
349 }
350 
351 Datum
353 {
355 
356  PG_RETURN_BOOL(cmp != 0);
357 }
358 
359 Datum
361 {
363 
364  PG_RETURN_BOOL(cmp < 0);
365 }
366 
367 Datum
369 {
371 
372  PG_RETURN_BOOL(cmp <= 0);
373 }
374 
375 Datum
377 {
379 
380  PG_RETURN_BOOL(cmp >= 0);
381 }
382 
383 Datum
385 {
387 
388  PG_RETURN_BOOL(cmp > 0);
389 }
#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)
Definition: arrayfuncs.c:1271
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1548
signed int int32
Definition: c.h:481
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition: fmgr.h:150
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int pg_cmp_u32(uint32 a, uint32 b)
Definition: int.h:489
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1334
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1528
void * palloc(Size size)
Definition: mcxt.c:1304
#define SOFT_ERROR_OCCURRED(escontext)
Definition: miscnodes.h:52
Datum btoidvectorcmp(PG_FUNCTION_ARGS)
Definition: nbtcompare.c:296
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
uint32 uint32in_subr(const char *s, char **endloc, const char *typname, Node *escontext)
Definition: numutils.c:900
Datum oidvectorin(PG_FUNCTION_ARGS)
Definition: oid.c:114
Datum oidvectoreq(PG_FUNCTION_ARGS)
Definition: oid.c:344
Datum oidvectorle(PG_FUNCTION_ARGS)
Definition: oid.c:368
Datum oidvectorout(PG_FUNCTION_ARGS)
Definition: oid.c:158
Datum oidvectorgt(PG_FUNCTION_ARGS)
Definition: oid.c:384
Datum oidvectorge(PG_FUNCTION_ARGS)
Definition: oid.c:376
Datum oidrecv(PG_FUNCTION_ARGS)
Definition: oid.c:60
Datum oidsmaller(PG_FUNCTION_ARGS)
Definition: oid.c:335
Datum oideq(PG_FUNCTION_ARGS)
Definition: oid.c:272
Datum oidvectorsend(PG_FUNCTION_ARGS)
Definition: oid.c:226
#define OidVectorSize(n)
Definition: oid.c:29
Datum oidvectorrecv(PG_FUNCTION_ARGS)
Definition: oid.c:184
Datum oidvectorlt(PG_FUNCTION_ARGS)
Definition: oid.c:360
Oid oidparse(Node *node)
Definition: oid.c:235
int oid_cmp(const void *p1, const void *p2)
Definition: oid.c:258
Datum oidin(PG_FUNCTION_ARGS)
Definition: oid.c:37
Datum oidlt(PG_FUNCTION_ARGS)
Definition: oid.c:290
Datum oidge(PG_FUNCTION_ARGS)
Definition: oid.c:308
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:47
Datum oidsend(PG_FUNCTION_ARGS)
Definition: oid.c:71
Datum oidle(PG_FUNCTION_ARGS)
Definition: oid.c:299
oidvector * buildoidvector(const Oid *oids, int n)
Definition: oid.c:87
Datum oidne(PG_FUNCTION_ARGS)
Definition: oid.c:281
Datum oidvectorne(PG_FUNCTION_ARGS)
Definition: oid.c:352
Datum oidgt(PG_FUNCTION_ARGS)
Definition: oid.c:317
Datum oidlarger(PG_FUNCTION_ARGS)
Definition: oid.c:326
static char * buf
Definition: pg_test_fsync.c:73
#define sprintf
Definition: port.h:240
#define snprintf
Definition: port.h:238
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:415
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
static void pq_sendint32(StringInfo buf, uint32 i)
Definition: pqformat.h:144
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
StringInfoData * StringInfo
Definition: stringinfo.h:54
Definition: value.h:48
Definition: nodes.h:129
Definition: c.h:713
int dim1
Definition: c.h:718
int32 dataoffset
Definition: c.h:716
Oid elemtype
Definition: c.h:717
int lbound1
Definition: c.h:719
int ndim
Definition: c.h:715
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition: c.h:720
#define intVal(v)
Definition: value.h:79
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305