Branch data 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-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 : :
36 : : Datum
37 : 71219 : oidin(PG_FUNCTION_ARGS)
38 : : {
39 : 71219 : char *s = PG_GETARG_CSTRING(0);
40 : 71219 : Oid result;
41 : :
42 : 71219 : result = uint32in_subr(s, NULL, "oid", fcinfo->context);
43 : 142438 : PG_RETURN_OID(result);
44 : 71219 : }
45 : :
46 : : Datum
47 : 12480 : oidout(PG_FUNCTION_ARGS)
48 : : {
49 : 12480 : Oid o = PG_GETARG_OID(0);
50 : 12480 : char *result = (char *) palloc(12);
51 : :
52 : 12480 : snprintf(result, 12, "%u", o);
53 : 24960 : PG_RETURN_CSTRING(result);
54 : 12480 : }
55 : :
56 : : /*
57 : : * oidrecv - converts external binary format to oid
58 : : */
59 : : Datum
60 : 7 : oidrecv(PG_FUNCTION_ARGS)
61 : : {
62 : 7 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
63 : :
64 : 14 : PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
65 : 7 : }
66 : :
67 : : /*
68 : : * oidsend - converts oid to binary format
69 : : */
70 : : Datum
71 : 2 : oidsend(PG_FUNCTION_ARGS)
72 : : {
73 : 2 : Oid arg1 = PG_GETARG_OID(0);
74 : 2 : StringInfoData buf;
75 : :
76 : 2 : pq_begintypsend(&buf);
77 : 2 : pq_sendint32(&buf, arg1);
78 : 4 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
79 : 2 : }
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 : 10058 : buildoidvector(const Oid *oids, int n)
88 : : {
89 : 10058 : oidvector *result;
90 : :
91 : 10058 : result = (oidvector *) palloc0(OidVectorSize(n));
92 : :
93 [ + + - + ]: 10058 : if (n > 0 && oids)
94 : 9622 : 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 : 10058 : SET_VARSIZE(result, OidVectorSize(n));
101 : 10058 : result->ndim = 1;
102 : 10058 : result->dataoffset = 0; /* never any nulls */
103 : 10058 : result->elemtype = OIDOID;
104 : 10058 : result->dim1 = n;
105 : 10058 : result->lbound1 = 0;
106 : :
107 : 20116 : return result;
108 : 10058 : }
109 : :
110 : : /*
111 : : * oidvectorin - converts "num num ..." to internal form
112 : : */
113 : : Datum
114 : 3464 : oidvectorin(PG_FUNCTION_ARGS)
115 : : {
116 : 3464 : char *oidString = PG_GETARG_CSTRING(0);
117 : 3464 : Node *escontext = fcinfo->context;
118 : 3464 : oidvector *result;
119 : 3464 : int nalloc;
120 : 3464 : int n;
121 : :
122 : 3464 : nalloc = 32; /* arbitrary initial size guess */
123 : 3464 : result = (oidvector *) palloc0(OidVectorSize(nalloc));
124 : :
125 : 9790 : for (n = 0;; n++)
126 : : {
127 [ + + + + ]: 12830 : while (*oidString && isspace((unsigned char) *oidString))
128 : 3040 : oidString++;
129 [ + + ]: 9790 : if (*oidString == '\0')
130 : 3460 : break;
131 : :
132 [ + - ]: 6330 : if (n >= nalloc)
133 : : {
134 : 0 : nalloc *= 2;
135 : 0 : result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
136 : 0 : }
137 : :
138 : 12660 : result->values[n] = uint32in_subr(oidString, &oidString,
139 : 6330 : "oid", escontext);
140 [ + + + - : 6330 : if (SOFT_ERROR_OCCURRED(escontext))
+ + ]
141 : 4 : PG_RETURN_NULL();
142 : 6326 : }
143 : :
144 : 3460 : SET_VARSIZE(result, OidVectorSize(n));
145 : 3460 : result->ndim = 1;
146 : 3460 : result->dataoffset = 0; /* never any nulls */
147 : 3460 : result->elemtype = OIDOID;
148 : 3460 : result->dim1 = n;
149 : 3460 : result->lbound1 = 0;
150 : :
151 : 3460 : PG_RETURN_POINTER(result);
152 : 3464 : }
153 : :
154 : : /*
155 : : * oidvectorout - converts internal form to "num num ..."
156 : : */
157 : : Datum
158 : 23 : oidvectorout(PG_FUNCTION_ARGS)
159 : : {
160 : 23 : oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
161 : 23 : int num,
162 : 23 : nnums = oidArray->dim1;
163 : 23 : char *rp;
164 : 23 : char *result;
165 : :
166 : : /* assumes sign, 10 digits, ' ' */
167 : 23 : rp = result = (char *) palloc(nnums * 12 + 1);
168 [ + + ]: 89 : for (num = 0; num < nnums; num++)
169 : : {
170 [ + + ]: 66 : if (num != 0)
171 : 43 : *rp++ = ' ';
172 : 66 : sprintf(rp, "%u", oidArray->values[num]);
173 [ + + ]: 138 : while (*++rp != '\0')
174 : : ;
175 : 66 : }
176 : 23 : *rp = '\0';
177 : 46 : PG_RETURN_CSTRING(result);
178 : 23 : }
179 : :
180 : : /*
181 : : * oidvectorrecv - converts external binary format to oidvector
182 : : */
183 : : Datum
184 : 0 : oidvectorrecv(PG_FUNCTION_ARGS)
185 : : {
186 : 0 : LOCAL_FCINFO(locfcinfo, 3);
187 : 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
188 : 0 : 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 : 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 */
211 [ # # ]: 0 : if (ARR_NDIM(result) != 1 ||
212 : 0 : ARR_HASNULL(result) ||
213 : 0 : ARR_ELEMTYPE(result) != OIDOID ||
214 : 0 : ARR_LBOUND(result)[0] != 0)
215 [ # # # # ]: 0 : ereport(ERROR,
216 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
217 : : errmsg("invalid oidvector data")));
218 : :
219 : 0 : PG_RETURN_POINTER(result);
220 : 0 : }
221 : :
222 : : /*
223 : : * oidvectorsend - converts oidvector to binary format
224 : : */
225 : : Datum
226 : 0 : oidvectorsend(PG_FUNCTION_ARGS)
227 : : {
228 : 0 : return array_send(fcinfo);
229 : : }
230 : :
231 : : /*
232 : : * oidparse - get OID from ICONST/FCONST node
233 : : */
234 : : Oid
235 : 20 : oidparse(Node *node)
236 : : {
237 [ + + - ]: 20 : switch (nodeTag(node))
238 : : {
239 : : case T_Integer:
240 : 18 : 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 : 2 : return uint32in_subr(castNode(Float, node)->fval, NULL,
249 : : "oid", NULL);
250 : : default:
251 [ # # # # ]: 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
252 : 0 : }
253 : 0 : return InvalidOid; /* keep compiler quiet */
254 : 20 : }
255 : :
256 : : /* qsort comparison function for Oids */
257 : : int
258 : 10121 : oid_cmp(const void *p1, const void *p2)
259 : : {
260 : 10121 : Oid v1 = *((const Oid *) p1);
261 : 10121 : Oid v2 = *((const Oid *) p2);
262 : :
263 : 20242 : return pg_cmp_u32(v1, v2);
264 : 10121 : }
265 : :
266 : :
267 : : /*****************************************************************************
268 : : * PUBLIC ROUTINES *
269 : : *****************************************************************************/
270 : :
271 : : Datum
272 : 4791875 : oideq(PG_FUNCTION_ARGS)
273 : : {
274 : 4791875 : Oid arg1 = PG_GETARG_OID(0);
275 : 4791875 : Oid arg2 = PG_GETARG_OID(1);
276 : :
277 : 9583750 : PG_RETURN_BOOL(arg1 == arg2);
278 : 4791875 : }
279 : :
280 : : Datum
281 : 213785 : oidne(PG_FUNCTION_ARGS)
282 : : {
283 : 213785 : Oid arg1 = PG_GETARG_OID(0);
284 : 213785 : Oid arg2 = PG_GETARG_OID(1);
285 : :
286 : 427570 : PG_RETURN_BOOL(arg1 != arg2);
287 : 213785 : }
288 : :
289 : : Datum
290 : 249814 : oidlt(PG_FUNCTION_ARGS)
291 : : {
292 : 249814 : Oid arg1 = PG_GETARG_OID(0);
293 : 249814 : Oid arg2 = PG_GETARG_OID(1);
294 : :
295 : 499628 : PG_RETURN_BOOL(arg1 < arg2);
296 : 249814 : }
297 : :
298 : : Datum
299 : 80873 : oidle(PG_FUNCTION_ARGS)
300 : : {
301 : 80873 : Oid arg1 = PG_GETARG_OID(0);
302 : 80873 : Oid arg2 = PG_GETARG_OID(1);
303 : :
304 : 161746 : PG_RETURN_BOOL(arg1 <= arg2);
305 : 80873 : }
306 : :
307 : : Datum
308 : 642 : oidge(PG_FUNCTION_ARGS)
309 : : {
310 : 642 : Oid arg1 = PG_GETARG_OID(0);
311 : 642 : Oid arg2 = PG_GETARG_OID(1);
312 : :
313 : 1284 : PG_RETURN_BOOL(arg1 >= arg2);
314 : 642 : }
315 : :
316 : : Datum
317 : 772 : oidgt(PG_FUNCTION_ARGS)
318 : : {
319 : 772 : Oid arg1 = PG_GETARG_OID(0);
320 : 772 : Oid arg2 = PG_GETARG_OID(1);
321 : :
322 : 1544 : PG_RETURN_BOOL(arg1 > arg2);
323 : 772 : }
324 : :
325 : : Datum
326 : 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 : 0 : }
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 : 0 : }
342 : :
343 : : Datum
344 : 4340 : oidvectoreq(PG_FUNCTION_ARGS)
345 : : {
346 : 4340 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
347 : :
348 : 8680 : PG_RETURN_BOOL(cmp == 0);
349 : 4340 : }
350 : :
351 : : Datum
352 : 3749 : oidvectorne(PG_FUNCTION_ARGS)
353 : : {
354 : 3749 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
355 : :
356 : 7498 : PG_RETURN_BOOL(cmp != 0);
357 : 3749 : }
358 : :
359 : : Datum
360 : 614 : oidvectorlt(PG_FUNCTION_ARGS)
361 : : {
362 : 614 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
363 : :
364 : 1228 : PG_RETURN_BOOL(cmp < 0);
365 : 614 : }
366 : :
367 : : Datum
368 : 212 : oidvectorle(PG_FUNCTION_ARGS)
369 : : {
370 : 212 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
371 : :
372 : 424 : PG_RETURN_BOOL(cmp <= 0);
373 : 212 : }
374 : :
375 : : Datum
376 : 0 : oidvectorge(PG_FUNCTION_ARGS)
377 : : {
378 : 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
379 : :
380 : 0 : PG_RETURN_BOOL(cmp >= 0);
381 : 0 : }
382 : :
383 : : Datum
384 : 0 : oidvectorgt(PG_FUNCTION_ARGS)
385 : : {
386 : 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
387 : :
388 : 0 : PG_RETURN_BOOL(cmp > 0);
389 : 0 : }
|