summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/pseudotypes.c
blob: 3ba8cb192ca26f7256c9a3bd21db2612c0914688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*-------------------------------------------------------------------------
 *
 * pseudotypes.c
 *	  Functions for the system pseudo-types.
 *
 * A pseudo-type isn't really a type and never has any operations, but
 * we do need to supply input and output functions to satisfy the links
 * in the pseudo-type's entry in pg_type.  In most cases the functions
 * just throw an error if invoked.  (XXX the error messages here cover
 * the most common case, but might be confusing in some contexts.  Can
 * we do better?)
 *
 *
 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  src/backend/utils/adt/pseudotypes.c
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "libpq/pqformat.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rangetypes.h"
#include "utils/multirangetypes.h"


/*
 * These macros generate input and output functions for a pseudo-type that
 * will reject all input and output attempts.  (But for some types, only
 * the input function need be dummy.)
 */
#define PSEUDOTYPE_DUMMY_INPUT_FUNC(typname) \
Datum \
typname##_in(PG_FUNCTION_ARGS) \
{ \
	ereport(ERROR, \
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
			 errmsg("cannot accept a value of type %s", #typname))); \
\
	PG_RETURN_VOID();			/* keep compiler quiet */ \
} \
\
extern int no_such_variable

#define PSEUDOTYPE_DUMMY_IO_FUNCS(typname) \
PSEUDOTYPE_DUMMY_INPUT_FUNC(typname); \
\
Datum \
typname##_out(PG_FUNCTION_ARGS) \
{ \
	ereport(ERROR, \
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
			 errmsg("cannot display a value of type %s", #typname))); \
\
	PG_RETURN_VOID();			/* keep compiler quiet */ \
} \
\
extern int no_such_variable

/*
 * Likewise for binary send/receive functions.  We don't bother with these
 * at all for many pseudotypes, but some have them.  (By convention, if
 * a type has a send function it should have a receive function, even if
 * that's only dummy.)
 */
#define PSEUDOTYPE_DUMMY_RECEIVE_FUNC(typname) \
Datum \
typname##_recv(PG_FUNCTION_ARGS) \
{ \
	ereport(ERROR, \
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
			 errmsg("cannot accept a value of type %s", #typname))); \
\
	PG_RETURN_VOID();			/* keep compiler quiet */ \
} \
\
extern int no_such_variable

#define PSEUDOTYPE_DUMMY_BINARY_IO_FUNCS(typname) \
PSEUDOTYPE_DUMMY_RECEIVE_FUNC(typname); \
\
Datum \
typname##_send(PG_FUNCTION_ARGS) \
{ \
	ereport(ERROR, \
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
			 errmsg("cannot display a value of type %s", #typname))); \
\
	PG_RETURN_VOID();			/* keep compiler quiet */ \
} \
\
extern int no_such_variable


/*
 * cstring
 *
 * cstring is marked as a pseudo-type because we don't want people using it
 * in tables.  But it's really a perfectly functional type, so provide
 * a full set of working I/O functions for it.  Among other things, this
 * allows manual invocation of datatype I/O functions, along the lines of
 * "SELECT foo_in('blah')" or "SELECT foo_out(some-foo-value)".
 */
Datum
cstring_in(PG_FUNCTION_ARGS)
{
	char	   *str = PG_GETARG_CSTRING(0);

	PG_RETURN_CSTRING(pstrdup(str));
}

Datum
cstring_out(PG_FUNCTION_ARGS)
{
	char	   *str = PG_GETARG_CSTRING(0);

	PG_RETURN_CSTRING(pstrdup(str));
}

Datum
cstring_recv(PG_FUNCTION_ARGS)
{
	StringInfo	buf = (StringInfo) PG_GETARG_POINTER(0);
	char	   *str;
	int			nbytes;

	str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
	PG_RETURN_CSTRING(str);
}

Datum
cstring_send(PG_FUNCTION_ARGS)
{
	char	   *str = PG_GETARG_CSTRING(0);
	StringInfoData buf;

	pq_begintypsend(&buf);
	pq_sendtext(&buf, str, strlen(str));
	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

/*
 * anyarray
 *
 * We need to allow output of anyarray so that, e.g., pg_statistic columns
 * can be printed.  Input has to be disallowed, however.
 *
 * XXX anyarray_recv could actually be made to work, since the incoming
 * array data would contain the element type OID.  It seems unlikely that
 * it'd be sufficiently type-safe, though.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anyarray);
PSEUDOTYPE_DUMMY_RECEIVE_FUNC(anyarray);

Datum
anyarray_out(PG_FUNCTION_ARGS)
{
	return array_out(fcinfo);
}

Datum
anyarray_send(PG_FUNCTION_ARGS)
{
	return array_send(fcinfo);
}

/*
 * anycompatiblearray
 *
 * We may as well allow output, since we do for anyarray.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anycompatiblearray);
PSEUDOTYPE_DUMMY_RECEIVE_FUNC(anycompatiblearray);

Datum
anycompatiblearray_out(PG_FUNCTION_ARGS)
{
	return array_out(fcinfo);
}

Datum
anycompatiblearray_send(PG_FUNCTION_ARGS)
{
	return array_send(fcinfo);
}

/*
 * anyenum
 *
 * We may as well allow output, since enum_out will in fact work.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anyenum);

Datum
anyenum_out(PG_FUNCTION_ARGS)
{
	return enum_out(fcinfo);
}

/*
 * anyrange
 *
 * We may as well allow output, since range_out will in fact work.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anyrange);

Datum
anyrange_out(PG_FUNCTION_ARGS)
{
	return range_out(fcinfo);
}

/*
 * anycompatiblerange
 *
 * We may as well allow output, since range_out will in fact work.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anycompatiblerange);

Datum
anycompatiblerange_out(PG_FUNCTION_ARGS)
{
	return range_out(fcinfo);
}

/*
 * anymultirange
 *
 * We may as well allow output, since multirange_out will in fact work.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anymultirange);

Datum
anymultirange_out(PG_FUNCTION_ARGS)
{
	return multirange_out(fcinfo);
}

/*
 * anycompatiblemultirange
 *
 * We may as well allow output, since multirange_out will in fact work.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(anycompatiblemultirange);

Datum
anycompatiblemultirange_out(PG_FUNCTION_ARGS)
{
	return multirange_out(fcinfo);
}

/*
 * void
 *
 * We support void_in so that PL functions can return VOID without any
 * special hack in the PL handler.  Whatever value the PL thinks it's
 * returning will just be ignored.  Conversely, void_out and void_send
 * are needed so that "SELECT function_returning_void(...)" works.
 */
Datum
void_in(PG_FUNCTION_ARGS)
{
	PG_RETURN_VOID();			/* you were expecting something different? */
}

Datum
void_out(PG_FUNCTION_ARGS)
{
	PG_RETURN_CSTRING(pstrdup(""));
}

Datum
void_recv(PG_FUNCTION_ARGS)
{
	/*
	 * Note that since we consume no bytes, an attempt to send anything but an
	 * empty string will result in an "invalid message format" error.
	 */
	PG_RETURN_VOID();
}

Datum
void_send(PG_FUNCTION_ARGS)
{
	StringInfoData buf;

	/* send an empty string */
	pq_begintypsend(&buf);
	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

/*
 * shell
 *
 * shell_in and shell_out are entered in pg_type for "shell" types
 * (those not yet filled in).  They should be unreachable, but we
 * set them up just in case some code path tries to do I/O without
 * having checked pg_type.typisdefined anywhere along the way.
 */
Datum
shell_in(PG_FUNCTION_ARGS)
{
	ereport(ERROR,
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
			 errmsg("cannot accept a value of a shell type")));

	PG_RETURN_VOID();			/* keep compiler quiet */
}

Datum
shell_out(PG_FUNCTION_ARGS)
{
	ereport(ERROR,
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
			 errmsg("cannot display a value of a shell type")));

	PG_RETURN_VOID();			/* keep compiler quiet */
}


/*
 * pg_node_tree
 *
 * pg_node_tree isn't really a pseudotype --- it's real enough to be a table
 * column --- but it presently has no operations of its own, and disallows
 * input too, so its I/O functions seem to fit here as much as anywhere.
 *
 * We must disallow input of pg_node_tree values because the SQL functions
 * that operate on the type are not secure against malformed input.
 * We do want to allow output, though.
 */
PSEUDOTYPE_DUMMY_INPUT_FUNC(pg_node_tree);
PSEUDOTYPE_DUMMY_RECEIVE_FUNC(pg_node_tree);

Datum
pg_node_tree_out(PG_FUNCTION_ARGS)
{
	return textout(fcinfo);
}

Datum
pg_node_tree_send(PG_FUNCTION_ARGS)
{
	return textsend(fcinfo);
}

/*
 * pg_ddl_command
 *
 * Like pg_node_tree, pg_ddl_command isn't really a pseudotype; it's here
 * for the same reasons as that one.
 *
 * We don't have any good way to output this type directly, so punt
 * for output as well as input.
 */
PSEUDOTYPE_DUMMY_IO_FUNCS(pg_ddl_command);
PSEUDOTYPE_DUMMY_BINARY_IO_FUNCS(pg_ddl_command);


/*
 * Dummy I/O functions for various other pseudotypes.
 */
PSEUDOTYPE_DUMMY_IO_FUNCS(any);
PSEUDOTYPE_DUMMY_IO_FUNCS(trigger);
PSEUDOTYPE_DUMMY_IO_FUNCS(event_trigger);
PSEUDOTYPE_DUMMY_IO_FUNCS(language_handler);
PSEUDOTYPE_DUMMY_IO_FUNCS(fdw_handler);
PSEUDOTYPE_DUMMY_IO_FUNCS(table_am_handler);
PSEUDOTYPE_DUMMY_IO_FUNCS(index_am_handler);
PSEUDOTYPE_DUMMY_IO_FUNCS(tsm_handler);
PSEUDOTYPE_DUMMY_IO_FUNCS(internal);
PSEUDOTYPE_DUMMY_IO_FUNCS(anyelement);
PSEUDOTYPE_DUMMY_IO_FUNCS(anynonarray);
PSEUDOTYPE_DUMMY_IO_FUNCS(anycompatible);
PSEUDOTYPE_DUMMY_IO_FUNCS(anycompatiblenonarray);