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