PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
funcs_new.c
Go to the documentation of this file.
1 /* src/tutorial/funcs_new.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  NOTE: this file shows examples of "new style" function call conventions.
11  See funcs.c for examples of "old style".
12 *****************************************************************************/
13 
14 #include "postgres.h" /* general Postgres declarations */
15 
16 #include "executor/executor.h" /* for GetAttributeByName() */
17 #include "utils/geo_decls.h" /* for point type */
18 
20 
21 
22 /* By Value */
23 
25 
26 Datum
28 {
30 
31  PG_RETURN_INT32(arg + 1);
32 }
33 
34 /* By Reference, Fixed Length */
35 
37 
38 Datum
40 {
41  /* The macros for FLOAT8 hide its pass-by-reference nature */
43 
44  PG_RETURN_FLOAT8(arg + 1.0);
45 }
46 
48 
49 Datum
51 {
52  Point *pointx = PG_GETARG_POINT_P(0);
53  Point *pointy = PG_GETARG_POINT_P(1);
54  Point *new_point = (Point *) palloc(sizeof(Point));
55 
56  new_point->x = pointx->x;
57  new_point->y = pointy->y;
58 
59  PG_RETURN_POINT_P(new_point);
60 }
61 
62 /* By Reference, Variable Length */
63 
65 
66 Datum
68 {
69  text *t = PG_GETARG_TEXT_P(0);
70 
71  /*
72  * VARSIZE is the total size of the struct in bytes.
73  */
74  text *new_t = (text *) palloc(VARSIZE(t));
75 
76  SET_VARSIZE(new_t, VARSIZE(t));
77 
78  /*
79  * VARDATA is a pointer to the data region of the struct.
80  */
81  memcpy((void *) VARDATA(new_t), /* destination */
82  (void *) VARDATA(t), /* source */
83  VARSIZE(t) - VARHDRSZ); /* how many bytes */
84  PG_RETURN_TEXT_P(new_t);
85 }
86 
88 
89 Datum
91 {
92  text *arg1 = PG_GETARG_TEXT_P(0);
93  text *arg2 = PG_GETARG_TEXT_P(1);
94  int32 arg1_size = VARSIZE(arg1) - VARHDRSZ;
95  int32 arg2_size = VARSIZE(arg2) - VARHDRSZ;
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(arg1), arg1_size);
101  memcpy(VARDATA(new_text) + arg1_size, VARDATA(arg2), arg2_size);
102  PG_RETURN_TEXT_P(new_text);
103 }
104 
105 /* Composite types */
106 
108 
109 Datum
111 {
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 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:237
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define VARDATA(PTR)
Definition: postgres.h:305
#define DatumGetInt32(X)
Definition: postgres.h:480
#define VARSIZE(PTR)
Definition: postgres.h:306
#define VARHDRSZ
Definition: c.h:441
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:310
double y
Definition: geo_decls.h:60
#define PG_RETURN_INT32(x)
Definition: fmgr.h:298
Datum add_one_float8(PG_FUNCTION_ARGS)
Definition: funcs_new.c:39
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:275
Datum makepoint(PG_FUNCTION_ARGS)
Definition: funcs_new.c:50
signed int int32
Definition: c.h:253
double x
Definition: geo_decls.h:60
double float8
Definition: c.h:378
#define PG_GETARG_POINT_P(n)
Definition: geo_decls.h:139
Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname, bool *isNull)
Definition: execQual.c:1209
#define PG_RETURN_POINT_P(x)
Definition: geo_decls.h:140
Datum c_overpaid(PG_FUNCTION_ARGS)
Definition: funcs_new.c:110
PG_FUNCTION_INFO_V1(add_one)
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:303
uintptr_t Datum
Definition: postgres.h:374
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:314
Datum concat_text(PG_FUNCTION_ARGS)
Definition: funcs_new.c:90
Datum add_one(PG_FUNCTION_ARGS)
Definition: funcs_new.c:27
void * palloc(Size size)
Definition: mcxt.c:891
#define PG_GETARG_TEXT_P(n)
Definition: fmgr.h:269
void * arg
Definition: c.h:435
#define PG_FUNCTION_ARGS
Definition: fmgr.h:150
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:330
Datum copytext(PG_FUNCTION_ARGS)
Definition: funcs_new.c:67
PG_MODULE_MAGIC
Definition: funcs_new.c:19