PostgreSQL Source Code git master
funcs.c
Go to the documentation of this file.
1/* src/tutorial/funcs.c */
2
3/******************************************************************************
4 These are user-defined functions that can be bound to a Postgres backend
5 and called by Postgres to execute SQL functions of the same name.
6
7 The calling format for these functions is defined by the CREATE FUNCTION
8 SQL statement that binds them to the backend.
9*****************************************************************************/
10
11#include "postgres.h" /* general Postgres declarations */
12
13#include "executor/executor.h" /* for GetAttributeByName() */
14#include "utils/fmgrprotos.h" /* for text_starts_with() */
15#include "utils/geo_decls.h" /* for point type */
16
18
19
20/* By Value */
21
23
26{
28
30}
31
32/* By Reference, Fixed Length */
33
35
38{
39 /* The macros for FLOAT8 hide its pass-by-reference nature */
41
42 PG_RETURN_FLOAT8(arg + 1.0);
43}
44
46
49{
50 Point *pointx = PG_GETARG_POINT_P(0);
51 Point *pointy = PG_GETARG_POINT_P(1);
52 Point *new_point = (Point *) palloc(sizeof(Point));
53
54 new_point->x = pointx->x;
55 new_point->y = pointy->y;
56
57 PG_RETURN_POINT_P(new_point);
58}
59
60/* By Reference, Variable Length */
61
63
66{
67 text *t = PG_GETARG_TEXT_PP(0);
68
69 /*
70 * VARSIZE_ANY_EXHDR is the size of the struct in bytes, minus the
71 * VARHDRSZ or VARHDRSZ_SHORT of its header. Construct the copy with a
72 * full-length header.
73 */
74 text *new_t = (text *) palloc(VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
75
77
78 /*
79 * VARDATA is a pointer to the data region of the new struct. The source
80 * could be a short datum, so retrieve its data through VARDATA_ANY.
81 */
82 memcpy(VARDATA(new_t), /* destination */
83 VARDATA_ANY(t), /* source */
84 VARSIZE_ANY_EXHDR(t)); /* how many bytes */
85 PG_RETURN_TEXT_P(new_t);
86}
87
89
92{
93 text *arg1 = PG_GETARG_TEXT_PP(0);
94 text *arg2 = PG_GETARG_TEXT_PP(1);
95 int32 arg1_size = VARSIZE_ANY_EXHDR(arg1);
96 int32 arg2_size = VARSIZE_ANY_EXHDR(arg2);
97 int32 new_text_size = arg1_size + arg2_size + VARHDRSZ;
98 text *new_text = (text *) palloc(new_text_size);
99
100 SET_VARSIZE(new_text, new_text_size);
101 memcpy(VARDATA(new_text), VARDATA_ANY(arg1), arg1_size);
102 memcpy(VARDATA(new_text) + arg1_size, VARDATA_ANY(arg2), arg2_size);
103 PG_RETURN_TEXT_P(new_text);
104}
105
106/* A wrapper around starts_with(text, text) */
107
109
110Datum
112{
113 text *t1 = PG_GETARG_TEXT_PP(0);
114 text *t2 = PG_GETARG_TEXT_PP(1);
116 bool result;
117
119 collid,
120 PointerGetDatum(t1),
121 PointerGetDatum(t2)));
122 PG_RETURN_BOOL(result);
123}
124
125/* Composite types */
126
128
129Datum
131{
133 int32 limit = PG_GETARG_INT32(1);
134 bool isnull;
135 int32 salary;
136
137 salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
138 if (isnull)
139 PG_RETURN_BOOL(false);
140
141 /*
142 * Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
143 */
144
145 PG_RETURN_BOOL(salary > limit);
146}
#define VARHDRSZ
Definition: c.h:663
double float8
Definition: c.h:601
int32_t int32
Definition: c.h:498
Oid collid
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition: execUtils.c:1062
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:812
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:312
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
Datum add_one(PG_FUNCTION_ARGS)
Definition: funcs.c:25
PG_FUNCTION_INFO_V1(add_one)
PG_MODULE_MAGIC
Definition: funcs.c:17
Datum makepoint(PG_FUNCTION_ARGS)
Definition: funcs.c:48
Datum copytext(PG_FUNCTION_ARGS)
Definition: funcs.c:65
Datum t_starts_with(PG_FUNCTION_ARGS)
Definition: funcs.c:111
Datum add_one_float8(PG_FUNCTION_ARGS)
Definition: funcs.c:37
Datum concat_text(PG_FUNCTION_ARGS)
Definition: funcs.c:91
Datum c_overpaid(PG_FUNCTION_ARGS)
Definition: funcs.c:130
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:185
#define PG_RETURN_POINT_P(x)
Definition: geo_decls.h:186
void * palloc(Size size)
Definition: mcxt.c:1317
void * arg
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:207
unsigned int Oid
Definition: postgres_ext.h:30
float8 y
Definition: geo_decls.h:99
float8 x
Definition: geo_decls.h:98
Definition: c.h:658
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
Datum text_starts_with(PG_FUNCTION_ARGS)
Definition: varlena.c:1847