PostgreSQL Source Code  git master
pseudotypes.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pseudotypes.c
4  * Functions for the system pseudo-types.
5  *
6  * A pseudo-type isn't really a type and never has any operations, but
7  * we do need to supply input and output functions to satisfy the links
8  * in the pseudo-type's entry in pg_type. In most cases the functions
9  * just throw an error if invoked. (XXX the error messages here cover
10  * the most common case, but might be confusing in some contexts. Can
11  * we do better?)
12  *
13  *
14  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  *
18  * IDENTIFICATION
19  * src/backend/utils/adt/pseudotypes.c
20  *
21  *-------------------------------------------------------------------------
22  */
23 #include "postgres.h"
24 
25 #include "libpq/pqformat.h"
26 #include "utils/fmgrprotos.h"
27 
28 
29 /*
30  * These macros generate input and output functions for a pseudo-type that
31  * will reject all input and output attempts. (But for some types, only
32  * the input function need be dummy.)
33  */
34 #define PSEUDOTYPE_DUMMY_INPUT_FUNC(typname) \
35 Datum \
36 typname##_in(PG_FUNCTION_ARGS) \
37 { \
38  ereport(ERROR, \
39  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
40  errmsg("cannot accept a value of type %s", #typname))); \
41 \
42  PG_RETURN_VOID(); /* keep compiler quiet */ \
43 } \
44 \
45 extern int no_such_variable
46 
47 #define PSEUDOTYPE_DUMMY_IO_FUNCS(typname) \
48 PSEUDOTYPE_DUMMY_INPUT_FUNC(typname); \
49 \
50 Datum \
51 typname##_out(PG_FUNCTION_ARGS) \
52 { \
53  ereport(ERROR, \
54  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
55  errmsg("cannot display a value of type %s", #typname))); \
56 \
57  PG_RETURN_VOID(); /* keep compiler quiet */ \
58 } \
59 \
60 extern int no_such_variable
61 
62 /*
63  * Likewise for binary send/receive functions. We don't bother with these
64  * at all for many pseudotypes, but some have them. (By convention, if
65  * a type has a send function it should have a receive function, even if
66  * that's only dummy.)
67  */
68 #define PSEUDOTYPE_DUMMY_RECEIVE_FUNC(typname) \
69 Datum \
70 typname##_recv(PG_FUNCTION_ARGS) \
71 { \
72  ereport(ERROR, \
73  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
74  errmsg("cannot accept a value of type %s", #typname))); \
75 \
76  PG_RETURN_VOID(); /* keep compiler quiet */ \
77 } \
78 \
79 extern int no_such_variable
80 
81 #define PSEUDOTYPE_DUMMY_BINARY_IO_FUNCS(typname) \
82 PSEUDOTYPE_DUMMY_RECEIVE_FUNC(typname); \
83 \
84 Datum \
85 typname##_send(PG_FUNCTION_ARGS) \
86 { \
87  ereport(ERROR, \
88  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
89  errmsg("cannot display a value of type %s", #typname))); \
90 \
91  PG_RETURN_VOID(); /* keep compiler quiet */ \
92 } \
93 \
94 extern int no_such_variable
95 
96 
97 /*
98  * cstring
99  *
100  * cstring is marked as a pseudo-type because we don't want people using it
101  * in tables. But it's really a perfectly functional type, so provide
102  * a full set of working I/O functions for it. Among other things, this
103  * allows manual invocation of datatype I/O functions, along the lines of
104  * "SELECT foo_in('blah')" or "SELECT foo_out(some-foo-value)".
105  */
106 Datum
108 {
109  char *str = PG_GETARG_CSTRING(0);
110 
112 }
113 
114 Datum
116 {
117  char *str = PG_GETARG_CSTRING(0);
118 
120 }
121 
122 Datum
124 {
126  char *str;
127  int nbytes;
128 
129  str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
131 }
132 
133 Datum
135 {
136  char *str = PG_GETARG_CSTRING(0);
138 
140  pq_sendtext(&buf, str, strlen(str));
142 }
143 
144 /*
145  * anyarray
146  *
147  * We need to allow output of anyarray so that, e.g., pg_statistic columns
148  * can be printed. Input has to be disallowed, however.
149  *
150  * XXX anyarray_recv could actually be made to work, since the incoming
151  * array data would contain the element type OID. It seems unlikely that
152  * it'd be sufficiently type-safe, though.
153  */
156 
157 Datum
159 {
160  return array_out(fcinfo);
161 }
162 
163 Datum
165 {
166  return array_send(fcinfo);
167 }
168 
169 /*
170  * anycompatiblearray
171  *
172  * We may as well allow output, since we do for anyarray.
173  */
174 PSEUDOTYPE_DUMMY_INPUT_FUNC(anycompatiblearray);
175 PSEUDOTYPE_DUMMY_RECEIVE_FUNC(anycompatiblearray);
176 
177 Datum
179 {
180  return array_out(fcinfo);
181 }
182 
183 Datum
185 {
186  return array_send(fcinfo);
187 }
188 
189 /*
190  * anyenum
191  *
192  * We may as well allow output, since enum_out will in fact work.
193  */
195 
196 Datum
198 {
199  return enum_out(fcinfo);
200 }
201 
202 /*
203  * anyrange
204  *
205  * We may as well allow output, since range_out will in fact work.
206  */
208 
209 Datum
211 {
212  return range_out(fcinfo);
213 }
214 
215 /*
216  * anycompatiblerange
217  *
218  * We may as well allow output, since range_out will in fact work.
219  */
220 PSEUDOTYPE_DUMMY_INPUT_FUNC(anycompatiblerange);
221 
222 Datum
224 {
225  return range_out(fcinfo);
226 }
227 
228 /*
229  * anymultirange
230  *
231  * We may as well allow output, since multirange_out will in fact work.
232  */
234 
235 Datum
237 {
238  return multirange_out(fcinfo);
239 }
240 
241 /*
242  * anycompatiblemultirange
243  *
244  * We may as well allow output, since multirange_out will in fact work.
245  */
246 PSEUDOTYPE_DUMMY_INPUT_FUNC(anycompatiblemultirange);
247 
248 Datum
250 {
251  return multirange_out(fcinfo);
252 }
253 
254 /*
255  * void
256  *
257  * We support void_in so that PL functions can return VOID without any
258  * special hack in the PL handler. Whatever value the PL thinks it's
259  * returning will just be ignored. Conversely, void_out and void_send
260  * are needed so that "SELECT function_returning_void(...)" works.
261  */
262 Datum
264 {
265  PG_RETURN_VOID(); /* you were expecting something different? */
266 }
267 
268 Datum
270 {
272 }
273 
274 Datum
276 {
277  /*
278  * Note that since we consume no bytes, an attempt to send anything but an
279  * empty string will result in an "invalid message format" error.
280  */
281  PG_RETURN_VOID();
282 }
283 
284 Datum
286 {
288 
289  /* send an empty string */
292 }
293 
294 /*
295  * shell
296  *
297  * shell_in and shell_out are entered in pg_type for "shell" types
298  * (those not yet filled in). They should be unreachable, but we
299  * set them up just in case some code path tries to do I/O without
300  * having checked pg_type.typisdefined anywhere along the way.
301  */
302 Datum
304 {
305  ereport(ERROR,
306  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
307  errmsg("cannot accept a value of a shell type")));
308 
309  PG_RETURN_VOID(); /* keep compiler quiet */
310 }
311 
312 Datum
314 {
315  ereport(ERROR,
316  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
317  errmsg("cannot display a value of a shell type")));
318 
319  PG_RETURN_VOID(); /* keep compiler quiet */
320 }
321 
322 
323 /*
324  * pg_node_tree
325  *
326  * pg_node_tree isn't really a pseudotype --- it's real enough to be a table
327  * column --- but it presently has no operations of its own, and disallows
328  * input too, so its I/O functions seem to fit here as much as anywhere.
329  *
330  * We must disallow input of pg_node_tree values because the SQL functions
331  * that operate on the type are not secure against malformed input.
332  * We do want to allow output, though.
333  */
336 
337 Datum
339 {
340  return textout(fcinfo);
341 }
342 
343 Datum
345 {
346  return textsend(fcinfo);
347 }
348 
349 /*
350  * pg_ddl_command
351  *
352  * Like pg_node_tree, pg_ddl_command isn't really a pseudotype; it's here
353  * for the same reasons as that one.
354  *
355  * We don't have any good way to output this type directly, so punt
356  * for output as well as input.
357  */
360 
361 
362 /*
363  * Dummy I/O functions for various other pseudotypes.
364  */
368 PSEUDOTYPE_DUMMY_IO_FUNCS(language_handler);
370 PSEUDOTYPE_DUMMY_IO_FUNCS(table_am_handler);
371 PSEUDOTYPE_DUMMY_IO_FUNCS(index_am_handler);
377 PSEUDOTYPE_DUMMY_IO_FUNCS(anycompatiblenonarray);
Datum array_send(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1548
Datum array_out(PG_FUNCTION_ARGS)
Definition: arrayfuncs.c:1016
int errcode(int sqlerrcode)
Definition: elog.c:855
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Datum enum_out(PG_FUNCTION_ARGS)
Definition: enum.c:155
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:371
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
const char * str
char * pstrdup(const char *in)
Definition: mcxt.c:1696
Datum multirange_out(PG_FUNCTION_ARGS)
static char * buf
Definition: pg_test_fsync.c:73
uintptr_t Datum
Definition: postgres.h:64
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:172
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:546
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346
Datum anyrange_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:210
Datum pg_node_tree_send(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:344
Datum anymultirange_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:236
#define PSEUDOTYPE_DUMMY_RECEIVE_FUNC(typname)
Definition: pseudotypes.c:68
Datum cstring_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:115
Datum anycompatiblearray_send(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:184
Datum shell_in(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:303
Datum void_send(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:285
Datum void_in(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:263
Datum void_recv(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:275
Datum anycompatiblemultirange_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:249
#define PSEUDOTYPE_DUMMY_INPUT_FUNC(typname)
Definition: pseudotypes.c:34
Datum cstring_recv(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:123
Datum anyarray_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:158
Datum pg_node_tree_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:338
#define PSEUDOTYPE_DUMMY_BINARY_IO_FUNCS(typname)
Definition: pseudotypes.c:81
#define PSEUDOTYPE_DUMMY_IO_FUNCS(typname)
Definition: pseudotypes.c:47
Datum void_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:269
Datum anyarray_send(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:164
Datum cstring_in(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:107
Datum anycompatiblerange_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:223
Datum anyenum_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:197
Datum shell_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:313
Datum anycompatiblearray_out(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:178
Datum cstring_send(PG_FUNCTION_ARGS)
Definition: pseudotypes.c:134
Datum range_out(PG_FUNCTION_ARGS)
Definition: rangetypes.c:137
StringInfoData * StringInfo
Definition: stringinfo.h:54
Datum textsend(PG_FUNCTION_ARGS)
Definition: varlena.c:619
Datum textout(PG_FUNCTION_ARGS)
Definition: varlena.c:590