PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oid.c
Go to the documentation of this file.
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
38{
39 char *s = PG_GETARG_CSTRING(0);
40 Oid result;
41
42 result = uint32in_subr(s, NULL, "oid", fcinfo->context);
43 PG_RETURN_OID(result);
44}
45
48{
49 Oid o = PG_GETARG_OID(0);
50 char *result = (char *) palloc(12);
51
52 snprintf(result, 12, "%u", o);
53 PG_RETURN_CSTRING(result);
54}
55
56/*
57 * oidrecv - converts external binary format to oid
58 */
66
67/*
68 * oidsend - converts oid to binary format
69 */
80
81/*
82 * construct oidvector given a raw array of Oids
83 *
84 * If oids is NULL then caller must fill values[] afterward
85 */
87buildoidvector(const Oid *oids, int n)
88{
89 oidvector *result;
90
91 result = (oidvector *) palloc0(OidVectorSize(n));
92
93 if (n > 0 && oids)
94 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 SET_VARSIZE(result, OidVectorSize(n));
101 result->ndim = 1;
102 result->dataoffset = 0; /* never any nulls */
103 result->elemtype = OIDOID;
104 result->dim1 = n;
105 result->lbound1 = 0;
106
107 return result;
108}
109
110/*
111 * oidvectorin - converts "num num ..." to internal form
112 */
113Datum
115{
116 char *oidString = PG_GETARG_CSTRING(0);
117 Node *escontext = fcinfo->context;
118 oidvector *result;
119 int nalloc;
120 int n;
121
122 nalloc = 32; /* arbitrary initial size guess */
123 result = (oidvector *) palloc0(OidVectorSize(nalloc));
124
125 for (n = 0;; n++)
126 {
127 while (*oidString && isspace((unsigned char) *oidString))
128 oidString++;
129 if (*oidString == '\0')
130 break;
131
132 if (n >= nalloc)
133 {
134 nalloc *= 2;
135 result = (oidvector *) repalloc(result, OidVectorSize(nalloc));
136 }
137
139 "oid", escontext);
140 if (SOFT_ERROR_OCCURRED(escontext))
142 }
143
144 SET_VARSIZE(result, OidVectorSize(n));
145 result->ndim = 1;
146 result->dataoffset = 0; /* never any nulls */
147 result->elemtype = OIDOID;
148 result->dim1 = n;
149 result->lbound1 = 0;
150
151 PG_RETURN_POINTER(result);
152}
153
154/*
155 * oidvectorout - converts internal form to "num num ..."
156 */
157Datum
159{
161 int num,
163 char *rp;
164 char *result;
165
166 /* assumes sign, 10 digits, ' ' */
167 rp = result = (char *) palloc(nnums * 12 + 1);
168 for (num = 0; num < nnums; num++)
169 {
170 if (num != 0)
171 *rp++ = ' ';
172 sprintf(rp, "%u", oidArray->values[num]);
173 while (*++rp != '\0')
174 ;
175 }
176 *rp = '\0';
177 PG_RETURN_CSTRING(result);
178}
179
180/*
181 * oidvectorrecv - converts external binary format to oidvector
182 */
183Datum
185{
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 */
196 InitFunctionCallInfoData(*locfcinfo, fcinfo->flinfo, 3,
198
199 locfcinfo->args[0].value = PointerGetDatum(buf);
200 locfcinfo->args[0].isnull = false;
201 locfcinfo->args[1].value = ObjectIdGetDatum(OIDOID);
202 locfcinfo->args[1].isnull = false;
203 locfcinfo->args[2].value = Int32GetDatum(-1);
204 locfcinfo->args[2].isnull = false;
205
207
208 Assert(!locfcinfo->isnull);
209
210 /* sanity checks: oidvector must be 1-D, 0-based, no nulls */
211 if (ARR_NDIM(result) != 1 ||
212 ARR_HASNULL(result) ||
213 ARR_ELEMTYPE(result) != OIDOID ||
214 ARR_LBOUND(result)[0] != 0)
217 errmsg("invalid oidvector data")));
218
219 PG_RETURN_POINTER(result);
220}
221
222/*
223 * oidvectorsend - converts oidvector to binary format
224 */
225Datum
227{
228 return array_send(fcinfo);
229}
230
231/*
232 * oidparse - get OID from ICONST/FCONST node
233 */
234Oid
236{
237 switch (nodeTag(node))
238 {
239 case T_Integer:
240 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 return uint32in_subr(castNode(Float, node)->fval, NULL,
249 "oid", NULL);
250 default:
251 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
252 }
253 return InvalidOid; /* keep compiler quiet */
254}
255
256/* qsort comparison function for Oids */
257int
258oid_cmp(const void *p1, const void *p2)
259{
260 Oid v1 = *((const Oid *) p1);
261 Oid v2 = *((const Oid *) p2);
262
263 return pg_cmp_u32(v1, v2);
264}
265
266
267/*****************************************************************************
268 * PUBLIC ROUTINES *
269 *****************************************************************************/
270
271Datum
279
280Datum
288
289Datum
297
298Datum
306
307Datum
315
316Datum
324
325Datum
333
334Datum
342
343Datum
350
351Datum
358
359Datum
366
367Datum
374
375Datum
382
383Datum
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_HASNULL(a)
Definition array.h:291
#define ARR_LBOUND(a)
Definition array.h:296
Datum array_recv(PG_FUNCTION_ARGS)
Datum array_send(PG_FUNCTION_ARGS)
#define Assert(condition)
Definition c.h:873
int32_t int32
Definition c.h:542
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Definition fmgr.h:150
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
#define LOCAL_FCINFO(name, nargs)
Definition fmgr.h:110
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_RETURN_OID(x)
Definition fmgr.h:361
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
static int pg_cmp_u32(uint32 a, uint32 b)
Definition int.h:719
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
void * palloc0(Size size)
Definition mcxt.c:1417
void * palloc(Size size)
Definition mcxt.c:1387
#define SOFT_ERROR_OCCURRED(escontext)
Definition miscnodes.h:53
Datum btoidvectorcmp(PG_FUNCTION_ARGS)
Definition nbtcompare.c:584
#define nodeTag(nodeptr)
Definition nodes.h:139
#define castNode(_type_, nodeptr)
Definition nodes.h:182
uint32 uint32in_subr(const char *s, char **endloc, const char *typname, Node *escontext)
Definition numutils.c:897
Datum oidvectorin(PG_FUNCTION_ARGS)
Definition oid.c:114
Datum oidvectoreq(PG_FUNCTION_ARGS)
Definition oid.c:344
Datum oidvectorle(PG_FUNCTION_ARGS)
Definition oid.c:368
Datum oidvectorout(PG_FUNCTION_ARGS)
Definition oid.c:158
Datum oidvectorgt(PG_FUNCTION_ARGS)
Definition oid.c:384
Datum oidvectorge(PG_FUNCTION_ARGS)
Definition oid.c:376
Datum oidrecv(PG_FUNCTION_ARGS)
Definition oid.c:60
Datum oidsmaller(PG_FUNCTION_ARGS)
Definition oid.c:335
Datum oideq(PG_FUNCTION_ARGS)
Definition oid.c:272
Datum oidvectorsend(PG_FUNCTION_ARGS)
Definition oid.c:226
#define OidVectorSize(n)
Definition oid.c:29
Datum oidvectorrecv(PG_FUNCTION_ARGS)
Definition oid.c:184
Datum oidvectorlt(PG_FUNCTION_ARGS)
Definition oid.c:360
Oid oidparse(Node *node)
Definition oid.c:235
oidvector * buildoidvector(const Oid *oids, int n)
Definition oid.c:87
int oid_cmp(const void *p1, const void *p2)
Definition oid.c:258
Datum oidin(PG_FUNCTION_ARGS)
Definition oid.c:37
Datum oidlt(PG_FUNCTION_ARGS)
Definition oid.c:290
Datum oidge(PG_FUNCTION_ARGS)
Definition oid.c:308
Datum oidout(PG_FUNCTION_ARGS)
Definition oid.c:47
Datum oidsend(PG_FUNCTION_ARGS)
Definition oid.c:71
Datum oidle(PG_FUNCTION_ARGS)
Definition oid.c:299
Datum oidne(PG_FUNCTION_ARGS)
Definition oid.c:281
Datum oidvectorne(PG_FUNCTION_ARGS)
Definition oid.c:352
Datum oidgt(PG_FUNCTION_ARGS)
Definition oid.c:317
Datum oidlarger(PG_FUNCTION_ARGS)
Definition oid.c:326
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define sprintf
Definition port.h:262
#define snprintf
Definition port.h:260
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
static Datum Int32GetDatum(int32 X)
Definition postgres.h:222
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
#define InvalidOid
unsigned int Oid
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144
static int fb(int x)
static int cmp(const chr *x, const chr *y, size_t len)
struct StringInfoData * StringInfo
Definition string.h:15
Definition value.h:48
Definition nodes.h:135
Definition c.h:745
int dim1
Definition c.h:750
int32 dataoffset
Definition c.h:748
Oid elemtype
Definition c.h:749
int lbound1
Definition c.h:751
int ndim
Definition c.h:747
Oid values[FLEXIBLE_ARRAY_MEMBER]
Definition c.h:752
#define intVal(v)
Definition value.h:79
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432