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