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/geo_decls.h
"
/* for point type */
15
16
PG_MODULE_MAGIC
;
17
18
19
/* By Value */
20
21
PG_FUNCTION_INFO_V1
(
add_one
);
22
23
Datum
24
add_one
(
PG_FUNCTION_ARGS
)
25
{
26
int32
arg
=
PG_GETARG_INT32
(0);
27
28
PG_RETURN_INT32
(
arg
+ 1);
29
}
30
31
/* By Reference, Fixed Length */
32
33
PG_FUNCTION_INFO_V1
(
add_one_float8
);
34
35
Datum
36
add_one_float8
(
PG_FUNCTION_ARGS
)
37
{
38
/* The macros for FLOAT8 hide its pass-by-reference nature */
39
float8
arg
=
PG_GETARG_FLOAT8
(0);
40
41
PG_RETURN_FLOAT8
(
arg
+ 1.0);
42
}
43
44
PG_FUNCTION_INFO_V1
(
makepoint
);
45
46
Datum
47
makepoint
(
PG_FUNCTION_ARGS
)
48
{
49
Point
*pointx =
PG_GETARG_POINT_P
(0);
50
Point
*pointy =
PG_GETARG_POINT_P
(1);
51
Point
*new_point = (
Point
*)
palloc
(
sizeof
(
Point
));
52
53
new_point->
x
= pointx->
x
;
54
new_point->
y
= pointy->
y
;
55
56
PG_RETURN_POINT_P
(new_point);
57
}
58
59
/* By Reference, Variable Length */
60
61
PG_FUNCTION_INFO_V1
(
copytext
);
62
63
Datum
64
copytext
(
PG_FUNCTION_ARGS
)
65
{
66
text
*t =
PG_GETARG_TEXT_PP
(0);
67
68
/*
69
* VARSIZE_ANY_EXHDR is the size of the struct in bytes, minus the
70
* VARHDRSZ or VARHDRSZ_SHORT of its header. Construct the copy with a
71
* full-length header.
72
*/
73
text
*new_t = (
text
*)
palloc
(
VARSIZE_ANY_EXHDR
(t) +
VARHDRSZ
);
74
75
SET_VARSIZE
(new_t,
VARSIZE_ANY_EXHDR
(t) +
VARHDRSZ
);
76
77
/*
78
* VARDATA is a pointer to the data region of the new struct. The source
79
* could be a short datum, so retrieve its data through VARDATA_ANY.
80
*/
81
memcpy(
VARDATA
(new_t),
/* destination */
82
VARDATA_ANY
(t),
/* source */
83
VARSIZE_ANY_EXHDR
(t));
/* how many bytes */
84
PG_RETURN_TEXT_P
(new_t);
85
}
86
87
PG_FUNCTION_INFO_V1
(
concat_text
);
88
89
Datum
90
concat_text
(
PG_FUNCTION_ARGS
)
91
{
92
text
*arg1 =
PG_GETARG_TEXT_PP
(0);
93
text
*arg2 =
PG_GETARG_TEXT_PP
(1);
94
int32
arg1_size =
VARSIZE_ANY_EXHDR
(arg1);
95
int32
arg2_size =
VARSIZE_ANY_EXHDR
(arg2);
96
int32
new_text_size = arg1_size + arg2_size +
VARHDRSZ
;
97
text
*new_text = (
text
*)
palloc
(new_text_size);
98
99
SET_VARSIZE
(new_text, new_text_size);
100
memcpy(
VARDATA
(new_text),
VARDATA_ANY
(arg1), arg1_size);
101
memcpy(
VARDATA
(new_text) + arg1_size,
VARDATA_ANY
(arg2), arg2_size);
102
PG_RETURN_TEXT_P
(new_text);
103
}
104
105
/* Composite types */
106
107
PG_FUNCTION_INFO_V1
(
c_overpaid
);
108
109
Datum
110
c_overpaid
(
PG_FUNCTION_ARGS
)
111
{
112
HeapTupleHeader
t =
PG_GETARG_HEAPTUPLEHEADER
(0);
113
int32
limit =
PG_GETARG_INT32
(1);
114
bool
isnull;
115
int32
salary;
116
117
salary =
DatumGetInt32
(
GetAttributeByName
(t,
"salary"
, &isnull));
118
if
(isnull)
119
PG_RETURN_BOOL
(
false
);
120
121
/*
122
* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
123
*/
124
125
PG_RETURN_BOOL
(salary > limit);
126
}
int32
signed int int32
Definition:
c.h:478
VARHDRSZ
#define VARHDRSZ
Definition:
c.h:676
float8
double float8
Definition:
c.h:614
GetAttributeByName
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition:
execUtils.c:1026
executor.h
PG_GETARG_TEXT_PP
#define PG_GETARG_TEXT_PP(n)
Definition:
fmgr.h:309
PG_GETARG_FLOAT8
#define PG_GETARG_FLOAT8(n)
Definition:
fmgr.h:282
PG_RETURN_FLOAT8
#define PG_RETURN_FLOAT8(x)
Definition:
fmgr.h:367
PG_GETARG_HEAPTUPLEHEADER
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition:
fmgr.h:312
PG_RETURN_TEXT_P
#define PG_RETURN_TEXT_P(x)
Definition:
fmgr.h:372
PG_RETURN_INT32
#define PG_RETURN_INT32(x)
Definition:
fmgr.h:354
PG_GETARG_INT32
#define PG_GETARG_INT32(n)
Definition:
fmgr.h:269
PG_FUNCTION_ARGS
#define PG_FUNCTION_ARGS
Definition:
fmgr.h:193
PG_RETURN_BOOL
#define PG_RETURN_BOOL(x)
Definition:
fmgr.h:359
add_one
Datum add_one(PG_FUNCTION_ARGS)
Definition:
funcs.c:24
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(add_one)
PG_MODULE_MAGIC
PG_MODULE_MAGIC
Definition:
funcs.c:16
makepoint
Datum makepoint(PG_FUNCTION_ARGS)
Definition:
funcs.c:47
copytext
Datum copytext(PG_FUNCTION_ARGS)
Definition:
funcs.c:64
add_one_float8
Datum add_one_float8(PG_FUNCTION_ARGS)
Definition:
funcs.c:36
concat_text
Datum concat_text(PG_FUNCTION_ARGS)
Definition:
funcs.c:90
c_overpaid
Datum c_overpaid(PG_FUNCTION_ARGS)
Definition:
funcs.c:110
geo_decls.h
PG_GETARG_POINT_P
#define PG_GETARG_POINT_P(n)
Definition:
geo_decls.h:185
PG_RETURN_POINT_P
#define PG_RETURN_POINT_P(x)
Definition:
geo_decls.h:186
palloc
void * palloc(Size size)
Definition:
mcxt.c:1226
arg
void * arg
Definition:
pg_backup_utils.c:27
postgres.h
Datum
uintptr_t Datum
Definition:
postgres.h:64
DatumGetInt32
static int32 DatumGetInt32(Datum X)
Definition:
postgres.h:202
HeapTupleHeaderData
Definition:
htup_details.h:154
Point
Definition:
geo_decls.h:97
Point::y
float8 y
Definition:
geo_decls.h:99
Point::x
float8 x
Definition:
geo_decls.h:98
varlena
Definition:
c.h:671
VARDATA
#define VARDATA(PTR)
Definition:
varatt.h:278
VARDATA_ANY
#define VARDATA_ANY(PTR)
Definition:
varatt.h:324
SET_VARSIZE
#define SET_VARSIZE(PTR, len)
Definition:
varatt.h:305
VARSIZE_ANY_EXHDR
#define VARSIZE_ANY_EXHDR(PTR)
Definition:
varatt.h:317
src
tutorial
funcs.c
Generated on Sun May 28 2023 12:13:25 for PostgreSQL Source Code by
1.9.1