PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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#include "varatt.h" /* for VARDATA/VARSIZE macros */
17
19
20
21/* By Value */
22
24
32
33/* By Reference, Fixed Length */
34
36
39{
40 /* The macros for FLOAT8 hide its pass-by-reference nature */
42
43 PG_RETURN_FLOAT8(arg + 1.0);
44}
45
47
60
61/* By Reference, Variable Length */
62
64
67{
68 text *t = PG_GETARG_TEXT_PP(0);
69
70 /*
71 * VARSIZE_ANY_EXHDR is the size of the struct in bytes, minus the
72 * VARHDRSZ or VARHDRSZ_SHORT of its header. Construct the copy with a
73 * full-length header.
74 */
76
78
79 /*
80 * VARDATA is a pointer to the data region of the new struct. The source
81 * could be a short datum, so retrieve its data through VARDATA_ANY.
82 */
83 memcpy(VARDATA(new_t), /* destination */
84 VARDATA_ANY(t), /* source */
85 VARSIZE_ANY_EXHDR(t)); /* how many bytes */
87}
88
90
106
107/* A wrapper around starts_with(text, text) */
108
110
111Datum
125
126/* Composite types */
127
129
130Datum
132{
134 int32 limit = PG_GETARG_INT32(1);
135 bool isnull;
137
138 salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
139 if (isnull)
140 PG_RETURN_BOOL(false);
141
142 /*
143 * Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
144 */
145
146 PG_RETURN_BOOL(salary > limit);
147}
#define VARHDRSZ
Definition c.h:711
double float8
Definition c.h:644
int32_t int32
Definition c.h:542
Oid collid
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition execUtils.c:1061
#define palloc_object(type)
Definition fe_memutils.h:74
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition fmgr.c:813
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_GETARG_FLOAT8(n)
Definition fmgr.h:283
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition fmgr.h:313
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#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:360
Datum add_one(PG_FUNCTION_ARGS)
Definition funcs.c:26
PG_MODULE_MAGIC
Definition funcs.c:18
Datum makepoint(PG_FUNCTION_ARGS)
Definition funcs.c:49
Datum copytext(PG_FUNCTION_ARGS)
Definition funcs.c:66
Datum t_starts_with(PG_FUNCTION_ARGS)
Definition funcs.c:112
Datum add_one_float8(PG_FUNCTION_ARGS)
Definition funcs.c:38
Datum concat_text(PG_FUNCTION_ARGS)
Definition funcs.c:92
Datum c_overpaid(PG_FUNCTION_ARGS)
Definition funcs.c:131
#define PG_GETARG_POINT_P(n)
Definition geo_decls.h:184
#define PG_RETURN_POINT_P(x)
Definition geo_decls.h:185
void * palloc(Size size)
Definition mcxt.c:1387
void * arg
static bool DatumGetBool(Datum X)
Definition postgres.h:100
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
unsigned int Oid
static int fb(int x)
Definition c.h:706
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432
Datum text_starts_with(PG_FUNCTION_ARGS)
Definition varlena.c:1545