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