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