Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * oid.c
4 : : * Functions for the built-in type Oid ... also oidvector.
5 : : *
6 : : * Portions Copyright (c) 1996-2025, 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
9126 tgl@sss.pgh.pa.us 37 :CBC 3052853 : oidin(PG_FUNCTION_ARGS)
38 : : {
39 : 3052853 : char *s = PG_GETARG_CSTRING(0);
40 : : Oid result;
41 : :
1086 42 : 3052853 : result = uint32in_subr(s, NULL, "oid", fcinfo->context);
9126 43 : 3052823 : PG_RETURN_OID(result);
44 : : }
45 : :
46 : : Datum
47 : 8907303 : oidout(PG_FUNCTION_ARGS)
48 : : {
49 : 8907303 : Oid o = PG_GETARG_OID(0);
50 : 8907303 : char *result = (char *) palloc(12);
51 : :
52 : 8907303 : snprintf(result, 12, "%u", o);
53 : 8907303 : PG_RETURN_CSTRING(result);
54 : : }
55 : :
56 : : /*
57 : : * oidrecv - converts external binary format to oid
58 : : */
59 : : Datum
8258 60 : 106 : oidrecv(PG_FUNCTION_ARGS)
61 : : {
62 : 106 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
63 : :
64 : 106 : PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
65 : : }
66 : :
67 : : /*
68 : : * oidsend - converts oid to binary format
69 : : */
70 : : Datum
71 : 7 : oidsend(PG_FUNCTION_ARGS)
72 : : {
73 : 7 : Oid arg1 = PG_GETARG_OID(0);
74 : : StringInfoData buf;
75 : :
76 : 7 : pq_begintypsend(&buf);
2989 andres@anarazel.de 77 : 7 : pq_sendint32(&buf, arg1);
8258 tgl@sss.pgh.pa.us 78 : 7 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
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 *
7568 87 : 68276 : buildoidvector(const Oid *oids, int n)
88 : : {
89 : : oidvector *result;
90 : :
91 : 68276 : result = (oidvector *) palloc0(OidVectorSize(n));
92 : :
93 [ + + + - ]: 68276 : if (n > 0 && oids)
94 : 65835 : 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 : : */
6868 100 : 68276 : SET_VARSIZE(result, OidVectorSize(n));
7568 101 : 68276 : result->ndim = 1;
7335 102 : 68276 : result->dataoffset = 0; /* never any nulls */
7568 103 : 68276 : result->elemtype = OIDOID;
104 : 68276 : result->dim1 = n;
105 : 68276 : result->lbound1 = 0;
106 : :
107 : 68276 : return result;
108 : : }
109 : :
110 : : /*
111 : : * oidvectorin - converts "num num ..." to internal form
112 : : */
113 : : Datum
9326 114 : 174147 : oidvectorin(PG_FUNCTION_ARGS)
115 : : {
116 : 174147 : char *oidString = PG_GETARG_CSTRING(0);
1099 117 : 174147 : Node *escontext = fcinfo->context;
118 : : oidvector *result;
119 : : int nalloc;
120 : : int n;
121 : :
1067 122 : 174147 : nalloc = 32; /* arbitrary initial size guess */
123 : 174147 : result = (oidvector *) palloc0(OidVectorSize(nalloc));
124 : :
125 : 174147 : for (n = 0;; n++)
126 : : {
9145 127 [ + + + + ]: 642399 : while (*oidString && isspace((unsigned char) *oidString))
9473 bruce@momjian.us 128 : 151398 : oidString++;
9126 tgl@sss.pgh.pa.us 129 [ + + ]: 491001 : if (*oidString == '\0')
130 : 174135 : break;
131 : :
1067 132 [ - + ]: 316866 : if (n >= nalloc)
133 : : {
1067 tgl@sss.pgh.pa.us 134 :UBC 0 : nalloc *= 2;
135 : 0 : result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
136 : : }
137 : :
1086 tgl@sss.pgh.pa.us 138 :CBC 316866 : result->values[n] = uint32in_subr(oidString, &oidString,
139 : : "oid", escontext);
1099 140 [ + + + - : 316866 : if (SOFT_ERROR_OCCURRED(escontext))
+ + ]
141 : 12 : PG_RETURN_NULL();
142 : : }
143 : :
6868 144 : 174135 : SET_VARSIZE(result, OidVectorSize(n));
7568 145 : 174135 : result->ndim = 1;
7335 146 : 174135 : result->dataoffset = 0; /* never any nulls */
7568 147 : 174135 : result->elemtype = OIDOID;
148 : 174135 : result->dim1 = n;
149 : 174135 : result->lbound1 = 0;
150 : :
9326 151 : 174135 : PG_RETURN_POINTER(result);
152 : : }
153 : :
154 : : /*
155 : : * oidvectorout - converts internal form to "num num ..."
156 : : */
157 : : Datum
158 : 20760 : oidvectorout(PG_FUNCTION_ARGS)
159 : : {
7568 160 : 20760 : oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
161 : : int num,
162 : 20760 : nnums = oidArray->dim1;
163 : : char *rp;
164 : : char *result;
165 : :
166 : : /* assumes sign, 10 digits, ' ' */
167 : 20760 : rp = result = (char *) palloc(nnums * 12 + 1);
168 [ + + ]: 54909 : for (num = 0; num < nnums; num++)
169 : : {
9473 bruce@momjian.us 170 [ + + ]: 34149 : if (num != 0)
171 : 15270 : *rp++ = ' ';
7568 tgl@sss.pgh.pa.us 172 : 34149 : sprintf(rp, "%u", oidArray->values[num]);
10328 bruce@momjian.us 173 [ + + ]: 97364 : while (*++rp != '\0')
174 : : ;
175 : : }
9473 176 : 20760 : *rp = '\0';
9326 tgl@sss.pgh.pa.us 177 : 20760 : PG_RETURN_CSTRING(result);
178 : : }
179 : :
180 : : /*
181 : : * oidvectorrecv - converts external binary format to oidvector
182 : : */
183 : : Datum
8258 tgl@sss.pgh.pa.us 184 :UBC 0 : oidvectorrecv(PG_FUNCTION_ARGS)
185 : : {
2517 andres@anarazel.de 186 : 0 : LOCAL_FCINFO(locfcinfo, 3);
8258 tgl@sss.pgh.pa.us 187 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
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 : : */
2517 andres@anarazel.de 196 : 0 : InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
197 : : InvalidOid, NULL, NULL);
198 : :
199 : 0 : locfcinfo->args[0].value = PointerGetDatum(buf);
200 : 0 : locfcinfo->args[0].isnull = false;
201 : 0 : locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
202 : 0 : locfcinfo->args[1].isnull = false;
203 : 0 : locfcinfo->args[2].value = Int32GetDatum(-1);
204 : 0 : locfcinfo->args[2].isnull = false;
205 : :
206 : 0 : result = (oidvector *) DatumGetPointer(array_recv(locfcinfo));
207 : :
208 [ # # ]: 0 : Assert(!locfcinfo->isnull);
209 : :
210 : : /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
7335 tgl@sss.pgh.pa.us 211 [ # # ]: 0 : if (ARR_NDIM(result) != 1 ||
212 [ # # ]: 0 : ARR_HASNULL(result) ||
5948 heikki.linnakangas@i 213 [ # # ]: 0 : ARR_ELEMTYPE(result) != OIDOID ||
214 [ # # ]: 0 : ARR_LBOUND(result)[0] != 0)
7568 tgl@sss.pgh.pa.us 215 [ # # ]: 0 : ereport(ERROR,
216 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
217 : : errmsg("invalid oidvector data")));
218 : :
8258 219 : 0 : PG_RETURN_POINTER(result);
220 : : }
221 : :
222 : : /*
223 : : * oidvectorsend - converts oidvector to binary format
224 : : */
225 : : Datum
226 : 0 : oidvectorsend(PG_FUNCTION_ARGS)
227 : : {
7568 228 : 0 : return array_send(fcinfo);
229 : : }
230 : :
231 : : /*
232 : : * oidparse - get OID from ICONST/FCONST node
233 : : */
234 : : Oid
5666 rhaas@postgresql.org 235 :CBC 93 : oidparse(Node *node)
236 : : {
237 [ + + - ]: 93 : switch (nodeTag(node))
238 : : {
239 : 87 : case T_Integer:
240 : 87 : return intVal(node);
241 : 6 : 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 : : */
1086 tgl@sss.pgh.pa.us 248 : 6 : return uint32in_subr(castNode(Float, node)->fval, NULL,
249 : : "oid", NULL);
5666 rhaas@postgresql.org 250 :UBC 0 : default:
251 [ # # ]: 0 : 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
3213 peter_e@gmx.net 258 :CBC 222808 : oid_cmp(const void *p1, const void *p2)
259 : : {
260 : 222808 : Oid v1 = *((const Oid *) p1);
261 : 222808 : Oid v2 = *((const Oid *) p2);
262 : :
670 nathan@postgresql.or 263 : 222808 : return pg_cmp_u32(v1, v2);
264 : : }
265 : :
266 : :
267 : : /*****************************************************************************
268 : : * PUBLIC ROUTINES *
269 : : *****************************************************************************/
270 : :
271 : : Datum
9326 tgl@sss.pgh.pa.us 272 : 66286794 : oideq(PG_FUNCTION_ARGS)
273 : : {
274 : 66286794 : Oid arg1 = PG_GETARG_OID(0);
275 : 66286794 : Oid arg2 = PG_GETARG_OID(1);
276 : :
277 : 66286794 : PG_RETURN_BOOL(arg1 == arg2);
278 : : }
279 : :
280 : : Datum
281 : 1411998 : oidne(PG_FUNCTION_ARGS)
282 : : {
283 : 1411998 : Oid arg1 = PG_GETARG_OID(0);
284 : 1411998 : Oid arg2 = PG_GETARG_OID(1);
285 : :
286 : 1411998 : PG_RETURN_BOOL(arg1 != arg2);
287 : : }
288 : :
289 : : Datum
9157 290 : 3015998 : oidlt(PG_FUNCTION_ARGS)
291 : : {
292 : 3015998 : Oid arg1 = PG_GETARG_OID(0);
293 : 3015998 : Oid arg2 = PG_GETARG_OID(1);
294 : :
295 : 3015998 : PG_RETURN_BOOL(arg1 < arg2);
296 : : }
297 : :
298 : : Datum
299 : 1053333 : oidle(PG_FUNCTION_ARGS)
300 : : {
301 : 1053333 : Oid arg1 = PG_GETARG_OID(0);
302 : 1053333 : Oid arg2 = PG_GETARG_OID(1);
303 : :
304 : 1053333 : PG_RETURN_BOOL(arg1 <= arg2);
305 : : }
306 : :
307 : : Datum
308 : 45230 : oidge(PG_FUNCTION_ARGS)
309 : : {
310 : 45230 : Oid arg1 = PG_GETARG_OID(0);
311 : 45230 : Oid arg2 = PG_GETARG_OID(1);
312 : :
313 : 45230 : PG_RETURN_BOOL(arg1 >= arg2);
314 : : }
315 : :
316 : : Datum
317 : 187822 : oidgt(PG_FUNCTION_ARGS)
318 : : {
319 : 187822 : Oid arg1 = PG_GETARG_OID(0);
320 : 187822 : Oid arg2 = PG_GETARG_OID(1);
321 : :
322 : 187822 : PG_RETURN_BOOL(arg1 > arg2);
323 : : }
324 : :
325 : : Datum
8891 tgl@sss.pgh.pa.us 326 :UBC 0 : oidlarger(PG_FUNCTION_ARGS)
327 : : {
328 : 0 : Oid arg1 = PG_GETARG_OID(0);
329 : 0 : Oid arg2 = PG_GETARG_OID(1);
330 : :
331 : 0 : PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
332 : : }
333 : :
334 : : Datum
335 : 0 : oidsmaller(PG_FUNCTION_ARGS)
336 : : {
337 : 0 : Oid arg1 = PG_GETARG_OID(0);
338 : 0 : Oid arg2 = PG_GETARG_OID(1);
339 : :
340 : 0 : PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
341 : : }
342 : :
343 : : Datum
9326 tgl@sss.pgh.pa.us 344 :CBC 18731 : oidvectoreq(PG_FUNCTION_ARGS)
345 : : {
7568 346 : 18731 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
347 : :
348 : 18731 : PG_RETURN_BOOL(cmp == 0);
349 : : }
350 : :
351 : : Datum
9326 352 : 11149 : oidvectorne(PG_FUNCTION_ARGS)
353 : : {
7568 354 : 11149 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
355 : :
356 : 11149 : PG_RETURN_BOOL(cmp != 0);
357 : : }
358 : :
359 : : Datum
9326 360 : 1842 : oidvectorlt(PG_FUNCTION_ARGS)
361 : : {
7568 362 : 1842 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
363 : :
364 : 1842 : PG_RETURN_BOOL(cmp < 0);
365 : : }
366 : :
367 : : Datum
9326 368 : 636 : oidvectorle(PG_FUNCTION_ARGS)
369 : : {
7568 370 : 636 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
371 : :
372 : 636 : PG_RETURN_BOOL(cmp <= 0);
373 : : }
374 : :
375 : : Datum
9326 tgl@sss.pgh.pa.us 376 :UBC 0 : oidvectorge(PG_FUNCTION_ARGS)
377 : : {
7568 378 : 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
379 : :
380 : 0 : PG_RETURN_BOOL(cmp >= 0);
381 : : }
382 : :
383 : : Datum
9326 384 : 0 : oidvectorgt(PG_FUNCTION_ARGS)
385 : : {
7568 386 : 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
387 : :
388 : 0 : PG_RETURN_BOOL(cmp > 0);
389 : : }
|