Age Owner 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-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 :
8143 tgl 35 ECB : Datum
8143 tgl 36 GIC 17351216 : oidin(PG_FUNCTION_ARGS)
37 : {
38 17351216 : char *s = PG_GETARG_CSTRING(0);
39 : Oid result;
8143 tgl 40 ECB :
103 tgl 41 GNC 17351216 : result = uint32in_subr(s, NULL, "oid", fcinfo->context);
8143 tgl 42 GIC 17351186 : PG_RETURN_OID(result);
8143 tgl 43 ECB : }
44 :
45 : Datum
8143 tgl 46 CBC 4795923 : oidout(PG_FUNCTION_ARGS)
8143 tgl 47 ECB : {
8143 tgl 48 CBC 4795923 : Oid o = PG_GETARG_OID(0);
8143 tgl 49 GIC 4795923 : char *result = (char *) palloc(12);
8143 tgl 50 ECB :
8143 tgl 51 GIC 4795923 : snprintf(result, 12, "%u", o);
8143 tgl 52 GBC 4795923 : PG_RETURN_CSTRING(result);
8143 tgl 53 EUB : }
54 :
55 : /*
7275 tgl 56 ECB : * oidrecv - converts external binary format to oid
57 : */
58 : Datum
7275 tgl 59 CBC 102 : oidrecv(PG_FUNCTION_ARGS)
60 : {
7275 tgl 61 GIC 102 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
7275 tgl 62 ECB :
7275 tgl 63 CBC 102 : PG_RETURN_OID((Oid) pq_getmsgint(buf, sizeof(Oid)));
7275 tgl 64 ECB : }
65 :
66 : /*
67 : * oidsend - converts oid to binary format
68 : */
69 : Datum
7275 tgl 70 GIC 7 : oidsend(PG_FUNCTION_ARGS)
71 : {
72 7 : Oid arg1 = PG_GETARG_OID(0);
73 : StringInfoData buf;
74 :
75 7 : pq_begintypsend(&buf);
2006 andres 76 CBC 7 : pq_sendint32(&buf, arg1);
7275 tgl 77 GIC 7 : PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
7275 tgl 78 ECB : }
79 :
6585 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 *
6585 tgl 86 CBC 167306 : buildoidvector(const Oid *oids, int n)
87 : {
6585 tgl 88 ECB : oidvector *result;
89 :
6585 tgl 90 CBC 167306 : result = (oidvector *) palloc0(OidVectorSize(n));
6585 tgl 91 ECB :
6585 tgl 92 GIC 167306 : if (n > 0 && oids)
93 164947 : memcpy(result->values, oids, n * sizeof(Oid));
6585 tgl 94 ECB :
95 : /*
96 : * Attach standard array header. For historical reasons, we set the index
97 : * lower bound to 0 not 1.
98 : */
5885 tgl 99 GIC 167306 : SET_VARSIZE(result, OidVectorSize(n));
6585 100 167306 : result->ndim = 1;
6352 101 167306 : result->dataoffset = 0; /* never any nulls */
6585 tgl 102 GBC 167306 : result->elemtype = OIDOID;
6585 tgl 103 GIC 167306 : result->dim1 = n;
6585 tgl 104 GBC 167306 : result->lbound1 = 0;
6585 tgl 105 EUB :
6585 tgl 106 GIC 167306 : return result;
107 : }
108 :
109 : /*
110 : * oidvectorin - converts "num num ..." to internal form
111 : */
112 : Datum
8343 113 1003190 : oidvectorin(PG_FUNCTION_ARGS)
9770 scrappy 114 EUB : {
8343 tgl 115 GIC 1003190 : char *oidString = PG_GETARG_CSTRING(0);
116 tgl 116 GNC 1003190 : Node *escontext = fcinfo->context;
117 : oidvector *result;
84 tgl 118 EUB : int nalloc;
6585 119 : int n;
9345 bruce 120 :
84 tgl 121 GBC 1003190 : nalloc = 32; /* arbitrary initial size guess */
122 1003190 : result = (oidvector *) palloc0(OidVectorSize(nalloc));
6585 tgl 123 EUB :
84 tgl 124 GIC 1003190 : for (n = 0;; n++)
9345 bruce 125 EUB : {
8162 tgl 126 GIC 3740530 : while (*oidString && isspace((unsigned char) *oidString))
8490 bruce 127 GBC 890723 : oidString++;
8143 tgl 128 GIC 2849807 : if (*oidString == '\0')
129 1003178 : break;
84 tgl 130 EUB :
84 tgl 131 GBC 1846629 : if (n >= nalloc)
84 tgl 132 EUB : {
84 tgl 133 UBC 0 : nalloc *= 2;
134 0 : result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
135 : }
136 :
103 tgl 137 GNC 1846629 : result->values[n] = uint32in_subr(oidString, &oidString,
138 : "oid", escontext);
116 139 1846629 : if (SOFT_ERROR_OCCURRED(escontext))
140 12 : PG_RETURN_NULL();
9345 bruce 141 EUB : }
142 :
5885 tgl 143 GIC 1003178 : SET_VARSIZE(result, OidVectorSize(n));
6585 144 1003178 : result->ndim = 1;
6352 145 1003178 : result->dataoffset = 0; /* never any nulls */
6585 146 1003178 : result->elemtype = OIDOID;
147 1003178 : result->dim1 = n;
6585 tgl 148 GBC 1003178 : result->lbound1 = 0;
149 :
8343 150 1003178 : PG_RETURN_POINTER(result);
151 : }
152 :
153 : /*
154 : * oidvectorout - converts internal form to "num num ..."
155 : */
156 : Datum
8343 tgl 157 CBC 3574 : oidvectorout(PG_FUNCTION_ARGS)
158 : {
6585 159 3574 : oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0);
160 : int num,
161 3574 : nnums = oidArray->dim1;
9173 bruce 162 ECB : char *rp;
9344 163 : char *result;
164 :
165 : /* assumes sign, 10 digits, ' ' */
6585 tgl 166 GIC 3574 : rp = result = (char *) palloc(nnums * 12 + 1);
167 8025 : for (num = 0; num < nnums; num++)
168 : {
8490 bruce 169 4451 : if (num != 0)
8490 bruce 170 CBC 1654 : *rp++ = ' ';
6585 tgl 171 GIC 4451 : sprintf(rp, "%u", oidArray->values[num]);
9345 bruce 172 GBC 11292 : while (*++rp != '\0')
9345 bruce 173 EUB : ;
174 : }
8490 bruce 175 GIC 3574 : *rp = '\0';
8343 tgl 176 3574 : PG_RETURN_CSTRING(result);
177 : }
178 :
179 : /*
7275 tgl 180 ECB : * oidvectorrecv - converts external binary format to oidvector
181 : */
182 : Datum
7275 tgl 183 LBC 0 : oidvectorrecv(PG_FUNCTION_ARGS)
184 : {
1534 andres 185 0 : LOCAL_FCINFO(locfcinfo, 3);
7275 tgl 186 0 : StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
6585 tgl 187 ECB : oidvector *result;
188 :
6247 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 : */
1534 andres 195 UIC 0 : InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
196 : InvalidOid, NULL, NULL);
197 :
1534 andres 198 LBC 0 : locfcinfo->args[0].value = PointerGetDatum(buf);
1534 andres 199 UIC 0 : locfcinfo->args[0].isnull = false;
1534 andres 200 LBC 0 : locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
201 0 : locfcinfo->args[1].isnull = false;
1534 andres 202 UIC 0 : locfcinfo->args[2].value = Int32GetDatum(-1);
1534 andres 203 LBC 0 : locfcinfo->args[2].isnull = false;
204 :
1534 andres 205 UIC 0 : result = (oidvector *) DatumGetPointer(array_recv(locfcinfo));
206 :
1534 andres 207 LBC 0 : Assert(!locfcinfo->isnull);
208 :
4965 heikki.linnakangas 209 ECB : /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
6352 tgl 210 LBC 0 : if (ARR_NDIM(result) != 1 ||
6352 tgl 211 UIC 0 : ARR_HASNULL(result) ||
4965 heikki.linnakangas 212 LBC 0 : ARR_ELEMTYPE(result) != OIDOID ||
4965 heikki.linnakangas 213 UIC 0 : ARR_LBOUND(result)[0] != 0)
6585 tgl 214 0 : ereport(ERROR,
215 : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
6585 tgl 216 ECB : errmsg("invalid oidvector data")));
217 :
7275 tgl 218 LBC 0 : PG_RETURN_POINTER(result);
7275 tgl 219 ECB : }
220 :
221 : /*
222 : * oidvectorsend - converts oidvector to binary format
223 : */
224 : Datum
7275 tgl 225 LBC 0 : oidvectorsend(PG_FUNCTION_ARGS)
226 : {
6585 227 0 : return array_send(fcinfo);
7275 tgl 228 ECB : }
229 :
4683 rhaas 230 : /*
231 : * oidparse - get OID from ICONST/FCONST node
232 : */
233 : Oid
4683 rhaas 234 CBC 68 : oidparse(Node *node)
235 : {
236 68 : switch (nodeTag(node))
4683 rhaas 237 ECB : {
4683 rhaas 238 GIC 62 : case T_Integer:
4683 rhaas 239 CBC 62 : return intVal(node);
4683 rhaas 240 GIC 6 : case T_Float:
241 :
242 : /*
4660 bruce 243 ECB : * Values too large for int4 will be represented as Float
244 : * constants by the lexer. Accept these if they are valid OID
245 : * strings.
4683 rhaas 246 : */
103 tgl 247 GNC 6 : return uint32in_subr(castNode(Float, node)->fval, NULL,
248 : "oid", NULL);
4683 rhaas 249 LBC 0 : default:
4683 rhaas 250 UIC 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
251 : }
252 : return InvalidOid; /* keep compiler quiet */
4683 rhaas 253 EUB : }
254 :
2230 peter_e 255 : /* qsort comparison function for Oids */
256 : int
2230 peter_e 257 GIC 318327 : oid_cmp(const void *p1, const void *p2)
2230 peter_e 258 EUB : {
2230 peter_e 259 GIC 318327 : Oid v1 = *((const Oid *) p1);
260 318327 : Oid v2 = *((const Oid *) p2);
261 :
2230 peter_e 262 GBC 318327 : if (v1 < v2)
2230 peter_e 263 GIC 143529 : return -1;
2230 peter_e 264 GBC 174798 : if (v1 > v2)
265 13117 : return 1;
2230 peter_e 266 GIC 161681 : return 0;
2230 peter_e 267 EUB : }
268 :
269 :
270 : /*****************************************************************************
9345 bruce 271 ECB : * PUBLIC ROUTINES *
272 : *****************************************************************************/
9770 scrappy 273 :
274 : Datum
8343 tgl 275 CBC 144088601 : oideq(PG_FUNCTION_ARGS)
276 : {
8343 tgl 277 GIC 144088601 : Oid arg1 = PG_GETARG_OID(0);
278 144088601 : Oid arg2 = PG_GETARG_OID(1);
8343 tgl 279 ECB :
8343 tgl 280 GIC 144088601 : PG_RETURN_BOOL(arg1 == arg2);
9770 scrappy 281 ECB : }
282 :
8343 tgl 283 : Datum
8343 tgl 284 GIC 1069758 : oidne(PG_FUNCTION_ARGS)
285 : {
286 1069758 : Oid arg1 = PG_GETARG_OID(0);
8343 tgl 287 CBC 1069758 : Oid arg2 = PG_GETARG_OID(1);
288 :
289 1069758 : PG_RETURN_BOOL(arg1 != arg2);
290 : }
9770 scrappy 291 ECB :
292 : Datum
8174 tgl 293 GIC 1806382 : oidlt(PG_FUNCTION_ARGS)
294 : {
8174 tgl 295 CBC 1806382 : Oid arg1 = PG_GETARG_OID(0);
8174 tgl 296 GIC 1806382 : Oid arg2 = PG_GETARG_OID(1);
8174 tgl 297 ECB :
8174 tgl 298 GIC 1806382 : PG_RETURN_BOOL(arg1 < arg2);
8174 tgl 299 ECB : }
300 :
301 : Datum
8174 tgl 302 GIC 650180 : oidle(PG_FUNCTION_ARGS)
8174 tgl 303 EUB : {
8174 tgl 304 GIC 650180 : Oid arg1 = PG_GETARG_OID(0);
8174 tgl 305 GBC 650180 : Oid arg2 = PG_GETARG_OID(1);
306 :
307 650180 : PG_RETURN_BOOL(arg1 <= arg2);
308 : }
309 :
310 : Datum
311 25930 : oidge(PG_FUNCTION_ARGS)
312 : {
313 25930 : Oid arg1 = PG_GETARG_OID(0);
8174 tgl 314 GIC 25930 : Oid arg2 = PG_GETARG_OID(1);
8174 tgl 315 EUB :
8174 tgl 316 GIC 25930 : PG_RETURN_BOOL(arg1 >= arg2);
317 : }
318 :
319 : Datum
320 173272 : oidgt(PG_FUNCTION_ARGS)
321 : {
322 173272 : Oid arg1 = PG_GETARG_OID(0);
323 173272 : Oid arg2 = PG_GETARG_OID(1);
324 :
325 173272 : PG_RETURN_BOOL(arg1 > arg2);
326 : }
327 :
328 : Datum
7908 tgl 329 UIC 0 : oidlarger(PG_FUNCTION_ARGS)
330 : {
331 0 : Oid arg1 = PG_GETARG_OID(0);
332 0 : Oid arg2 = PG_GETARG_OID(1);
333 :
334 0 : PG_RETURN_OID((arg1 > arg2) ? arg1 : arg2);
335 : }
336 :
337 : Datum
338 0 : oidsmaller(PG_FUNCTION_ARGS)
339 : {
340 0 : Oid arg1 = PG_GETARG_OID(0);
341 0 : Oid arg2 = PG_GETARG_OID(1);
342 :
343 0 : PG_RETURN_OID((arg1 < arg2) ? arg1 : arg2);
344 : }
345 :
346 : Datum
8343 tgl 347 GIC 37548 : oidvectoreq(PG_FUNCTION_ARGS)
348 : {
6585 349 37548 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
350 :
351 37548 : PG_RETURN_BOOL(cmp == 0);
352 : }
353 :
354 : Datum
8343 355 10731 : oidvectorne(PG_FUNCTION_ARGS)
356 : {
6585 357 10731 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
358 :
359 10731 : PG_RETURN_BOOL(cmp != 0);
360 : }
361 :
362 : Datum
8343 363 1842 : oidvectorlt(PG_FUNCTION_ARGS)
364 : {
6585 365 1842 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
366 :
367 1842 : PG_RETURN_BOOL(cmp < 0);
368 : }
369 :
370 : Datum
8343 371 636 : oidvectorle(PG_FUNCTION_ARGS)
372 : {
6585 373 636 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
374 :
375 636 : PG_RETURN_BOOL(cmp <= 0);
376 : }
377 :
378 : Datum
8343 tgl 379 UIC 0 : oidvectorge(PG_FUNCTION_ARGS)
380 : {
6585 381 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
382 :
383 0 : PG_RETURN_BOOL(cmp >= 0);
384 : }
385 :
386 : Datum
8343 387 0 : oidvectorgt(PG_FUNCTION_ARGS)
388 : {
6585 389 0 : int32 cmp = DatumGetInt32(btoidvectorcmp(fcinfo));
390 :
391 0 : PG_RETURN_BOOL(cmp > 0);
392 : }
|