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
9024 tgl@sss.pgh.pa.us 37 :CBC 2991055 : oidin(PG_FUNCTION_ARGS)
38 : : {
39 : 2991055 : char *s = PG_GETARG_CSTRING(0);
40 : : Oid result;
41 : :
984 42 : 2991055 : result = uint32in_subr(s, NULL, "oid", fcinfo->context);
9024 43 : 2991025 : PG_RETURN_OID(result);
44 : : }
45 : :
46 : : Datum
47 : 8676228 : oidout(PG_FUNCTION_ARGS)
48 : : {
49 : 8676228 : Oid o = PG_GETARG_OID(0);
50 : 8676228 : char *result = (char *) palloc(12);
51 : :
52 : 8676228 : snprintf(result, 12, "%u", o);
53 : 8676228 : PG_RETURN_CSTRING(result);
54 : : }
55 : :
56 : : /*
57 : : * oidrecv - converts external binary format to oid
58 : : */
59 : : Datum
8156 60 : 108 : oidrecv(PG_FUNCTION_ARGS)
61 : : {
62 : 108 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
63 : :
64 : 108 : 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);
2887 andres@anarazel.de 77 : 7 : pq_sendint32(&buf, arg1);
8156 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 *
7466 87 : 65651 : buildoidvector(const Oid *oids, int n)
88 : : {
89 : : oidvector *result;
90 : :
91 : 65651 : result = (oidvector *) palloc0(OidVectorSize(n));
92 : :
93 [ + + + - ]: 65651 : if (n > 0 && oids)
94 : 63259 : 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 : : */
6766 100 : 65651 : SET_VARSIZE(result, OidVectorSize(n));
7466 101 : 65651 : result->ndim = 1;
7233 102 : 65651 : result->dataoffset = 0; /* never any nulls */
7466 103 : 65651 : result->elemtype = OIDOID;
104 : 65651 : result->dim1 = n;
105 : 65651 : result->lbound1 = 0;
106 : :
107 : 65651 : return result;
108 : : }
109 : :
110 : : /*
111 : : * oidvectorin - converts "num num ..." to internal form
112 : : */
113 : : Datum
9224 114 : 170284 : oidvectorin(PG_FUNCTION_ARGS)
115 : : {
116 : 170284 : char *oidString = PG_GETARG_CSTRING(0);
997 117 : 170284 : Node *escontext = fcinfo->context;
118 : : oidvector *result;
119 : : int nalloc;
120 : : int n;
121 : :
965 122 : 170284 : nalloc = 32; /* arbitrary initial size guess */
123 : 170284 : result = (oidvector *) palloc0(OidVectorSize(nalloc));
124 : :
125 : 170284 : for (n = 0;; n++)
126 : : {
9043 127 [ + + + + ]: 628313 : while (*oidString && isspace((unsigned char) *oidString))
9371 bruce@momjian.us 128 : 148133 : oidString++;
9024 tgl@sss.pgh.pa.us 129 [ + + ]: 480180 : if (*oidString == '\0')
130 : 170272 : break;
131 : :
965 132 [ - + ]: 309908 : if (n >= nalloc)
133 : : {
965 tgl@sss.pgh.pa.us 134 :UBC 0 : nalloc *= 2;
135 : 0 : result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
136 : : }
137 : :
984 tgl@sss.pgh.pa.us 138 :CBC 309908 : result->values[n] = uint32in_subr(oidString, &oidString,
139 : : "oid", escontext);
997 140 [ + + + - : 309908 : if (SOFT_ERROR_OCCURRED(escontext))
+ + ]
141 : 12 : PG_RETURN_NULL();
142 : : }
143 : :
6766 144 : 170272 : SET_VARSIZE(result, OidVectorSize(n));
7466 145 : 170272 : result->ndim = 1;
7233 146 : 170272 : result->dataoffset = 0; /* never any nulls */
7466 147 : 170272 : result->elemtype = OIDOID;
148 : 170272 : result->dim1 = n;
149 : 170272 : result->lbound1 = 0;
150 : :
9224 151 : 170272 : PG_RETURN_POINTER(result);
152 : : }
153 : :
154 : : /*
155 : : * oidvectorout - converts internal form to "num num ..."
156 : : */
157 : : Datum
158 : 20692 : oidvectorout(PG_FUNCTION_ARGS)
159 : : {
7466 160 : 20692 : oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
161 : : int num,
162 : 20692 : nnums = oidArray->dim1;
163 : : char *rp;
164 : : char *result;
165 : :
166 : : /* assumes sign, 10 digits, ' ' */
167 : 20692 : rp = result = (char *) palloc(nnums * 12 + 1);
168 [ + + ]: 54683 : for (num = 0; num < nnums; num++)
169 : : {
9371 bruce@momjian.us 170 [ + + ]: 33991 : if (num != 0)
171 : 15200 : *rp++ = ' ';
7466 tgl@sss.pgh.pa.us 172 : 33991 : sprintf(rp, "%u", oidArray->values[num]);
10226 bruce@momjian.us 173 [ + + ]: 96999 : while (*++rp != '\0')
174 : : ;
175 : : }
9371 176 : 20692 : *rp = '\0';
9224 tgl@sss.pgh.pa.us 177 : 20692 : PG_RETURN_CSTRING(result);
178 : : }
179 : :
180 : : /*
181 : : * oidvectorrecv - converts external binary format to oidvector
182 : : */
183 : : Datum
8156 tgl@sss.pgh.pa.us 184 :UBC 0 : oidvectorrecv(PG_FUNCTION_ARGS)
185 : : {
2415 andres@anarazel.de 186 : 0 : LOCAL_FCINFO(locfcinfo, 3);
8156 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 : : */
2415 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 */
7233 tgl@sss.pgh.pa.us 211 [ # # ]: 0 : if (ARR_NDIM(result) != 1 ||
212 [ # # ]: 0 : ARR_HASNULL(result) ||
5846 heikki.linnakangas@i 213 [ # # ]: 0 : ARR_ELEMTYPE(result) != OIDOID ||
214 [ # # ]: 0 : ARR_LBOUND(result)[0] != 0)
7466 tgl@sss.pgh.pa.us 215 [ # # ]: 0 : ereport(ERROR,
216 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
217 : : errmsg("invalid oidvector data")));
218 : :
8156 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 : : {
7466 228 : 0 : return array_send(fcinfo);
229 : : }
230 : :
231 : : /*
232 : : * oidparse - get OID from ICONST/FCONST node
233 : : */
234 : : Oid
5564 rhaas@postgresql.org 235 :CBC 67 : oidparse(Node *node)
236 : : {
237 [ + + - ]: 67 : switch (nodeTag(node))
238 : : {
239 : 61 : case T_Integer:
240 : 61 : 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 : : */
984 tgl@sss.pgh.pa.us 248 : 6 : return uint32in_subr(castNode(Float, node)->fval, NULL,
249 : : "oid", NULL);
5564 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
3111 peter_e@gmx.net 258 :CBC 221854 : oid_cmp(const void *p1, const void *p2)
259 : : {
260 : 221854 : Oid v1 = *((const Oid *) p1);
261 : 221854 : Oid v2 = *((const Oid *) p2);
262 : :
568 nathan@postgresql.or 263 : 221854 : return pg_cmp_u32(v1, v2);
264 : : }
265 : :
266 : :
267 : : /*****************************************************************************
268 : : * PUBLIC ROUTINES *
269 : : *****************************************************************************/
270 : :
271 : : Datum
9224 tgl@sss.pgh.pa.us 272 : 71196486 : oideq(PG_FUNCTION_ARGS)
273 : : {
274 : 71196486 : Oid arg1 = PG_GETARG_OID(0);
275 : 71196486 : Oid arg2 = PG_GETARG_OID(1);
276 : :
277 : 71196486 : PG_RETURN_BOOL(arg1 == arg2);
278 : : }
279 : :
280 : : Datum
281 : 1391451 : oidne(PG_FUNCTION_ARGS)
282 : : {
283 : 1391451 : Oid arg1 = PG_GETARG_OID(0);
284 : 1391451 : Oid arg2 = PG_GETARG_OID(1);
285 : :
286 : 1391451 : PG_RETURN_BOOL(arg1 != arg2);
287 : : }
288 : :
289 : : Datum
9055 290 : 2925953 : oidlt(PG_FUNCTION_ARGS)
291 : : {
292 : 2925953 : Oid arg1 = PG_GETARG_OID(0);
293 : 2925953 : Oid arg2 = PG_GETARG_OID(1);
294 : :
295 : 2925953 : PG_RETURN_BOOL(arg1 < arg2);
296 : : }
297 : :
298 : : Datum
299 : 1021768 : oidle(PG_FUNCTION_ARGS)
300 : : {
301 : 1021768 : Oid arg1 = PG_GETARG_OID(0);
302 : 1021768 : Oid arg2 = PG_GETARG_OID(1);
303 : :
304 : 1021768 : PG_RETURN_BOOL(arg1 <= arg2);
305 : : }
306 : :
307 : : Datum
308 : 45773 : oidge(PG_FUNCTION_ARGS)
309 : : {
310 : 45773 : Oid arg1 = PG_GETARG_OID(0);
311 : 45773 : Oid arg2 = PG_GETARG_OID(1);
312 : :
313 : 45773 : PG_RETURN_BOOL(arg1 >= arg2);
314 : : }
315 : :
316 : : Datum
317 : 208246 : oidgt(PG_FUNCTION_ARGS)
318 : : {
319 : 208246 : Oid arg1 = PG_GETARG_OID(0);
320 : 208246 : Oid arg2 = PG_GETARG_OID(1);
321 : :
322 : 208246 : PG_RETURN_BOOL(arg1 > arg2);
323 : : }
324 : :
325 : : Datum
8789 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
9224 tgl@sss.pgh.pa.us 344 :CBC 18521 : oidvectoreq(PG_FUNCTION_ARGS)
345 : : {
7466 346 : 18521 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
347 : :
348 : 18521 : PG_RETURN_BOOL(cmp == 0);
349 : : }
350 : :
351 : : Datum
9224 352 : 11116 : oidvectorne(PG_FUNCTION_ARGS)
353 : : {
7466 354 : 11116 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
355 : :
356 : 11116 : PG_RETURN_BOOL(cmp != 0);
357 : : }
358 : :
359 : : Datum
9224 360 : 1842 : oidvectorlt(PG_FUNCTION_ARGS)
361 : : {
7466 362 : 1842 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
363 : :
364 : 1842 : PG_RETURN_BOOL(cmp < 0);
365 : : }
366 : :
367 : : Datum
9224 368 : 636 : oidvectorle(PG_FUNCTION_ARGS)
369 : : {
7466 370 : 636 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
371 : :
372 : 636 : PG_RETURN_BOOL(cmp <= 0);
373 : : }
374 : :
375 : : Datum
9224 tgl@sss.pgh.pa.us 376 :UBC 0 : oidvectorge(PG_FUNCTION_ARGS)
377 : : {
7466 378 : 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
379 : :
380 : 0 : PG_RETURN_BOOL(cmp >= 0);
381 : : }
382 : :
383 : : Datum
9224 384 : 0 : oidvectorgt(PG_FUNCTION_ARGS)
385 : : {
7466 386 : 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
387 : :
388 : 0 : PG_RETURN_BOOL(cmp > 0);
389 : : }
|