mirror of
https://github.com/postgres/postgres.git
synced 2025-06-05 00:02:04 -04:00
Remove traces of version-0 calling convention in src/tutorial/
Support has been removed as of 5ded4bd, but code related to the tutorial still used it. Functions using version-1 are already present for some time in the tutorial, and the documentation mentions them, so just replace the old version with the new one. Reported-by: Pavel Stehule Analyzed-by: Euler Taveira Author: Michael Paquier Reviewed-by: Tom Lane, Pavel Stehule Discussion: https://postgr.es/m/CAFj8pRCgC2uDzrw-vvanXu6Z3ofyviEOQPEpH6_aL4OCe7JRag@mail.gmail.com
This commit is contained in:
parent
4a0aab14dc
commit
a9d5157ae8
@ -6,9 +6,6 @@
|
|||||||
|
|
||||||
The calling format for these functions is defined by the CREATE FUNCTION
|
The calling format for these functions is defined by the CREATE FUNCTION
|
||||||
SQL statement that binds them to the backend.
|
SQL statement that binds them to the backend.
|
||||||
|
|
||||||
NOTE: this file shows examples of "old style" function call conventions.
|
|
||||||
See funcs_new.c for examples of "new style".
|
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
|
|
||||||
#include "postgres.h" /* general Postgres declarations */
|
#include "postgres.h" /* general Postgres declarations */
|
||||||
@ -18,94 +15,112 @@
|
|||||||
|
|
||||||
PG_MODULE_MAGIC;
|
PG_MODULE_MAGIC;
|
||||||
|
|
||||||
/* These prototypes just prevent possible warnings from gcc. */
|
|
||||||
|
|
||||||
int add_one(int arg);
|
|
||||||
float8 *add_one_float8(float8 *arg);
|
|
||||||
Point *makepoint(Point *pointx, Point *pointy);
|
|
||||||
text *copytext(text *t);
|
|
||||||
text *concat_text(text *arg1, text *arg2);
|
|
||||||
bool c_overpaid(HeapTupleHeader t, /* the current instance of EMP */
|
|
||||||
int32 limit);
|
|
||||||
|
|
||||||
|
|
||||||
/* By Value */
|
/* By Value */
|
||||||
|
|
||||||
int
|
PG_FUNCTION_INFO_V1(add_one);
|
||||||
add_one(int arg)
|
|
||||||
|
Datum
|
||||||
|
add_one(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
return arg + 1;
|
int32 arg = PG_GETARG_INT32(0);
|
||||||
|
|
||||||
|
PG_RETURN_INT32(arg + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* By Reference, Fixed Length */
|
/* By Reference, Fixed Length */
|
||||||
|
|
||||||
float8 *
|
PG_FUNCTION_INFO_V1(add_one_float8);
|
||||||
add_one_float8(float8 *arg)
|
|
||||||
|
Datum
|
||||||
|
add_one_float8(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
float8 *result = (float8 *) palloc(sizeof(float8));
|
/* The macros for FLOAT8 hide its pass-by-reference nature */
|
||||||
|
float8 arg = PG_GETARG_FLOAT8(0);
|
||||||
|
|
||||||
*result = *arg + 1.0;
|
PG_RETURN_FLOAT8(arg + 1.0);
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Point *
|
PG_FUNCTION_INFO_V1(makepoint);
|
||||||
makepoint(Point *pointx, Point *pointy)
|
|
||||||
|
Datum
|
||||||
|
makepoint(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
|
Point *pointx = PG_GETARG_POINT_P(0);
|
||||||
|
Point *pointy = PG_GETARG_POINT_P(1);
|
||||||
Point *new_point = (Point *) palloc(sizeof(Point));
|
Point *new_point = (Point *) palloc(sizeof(Point));
|
||||||
|
|
||||||
new_point->x = pointx->x;
|
new_point->x = pointx->x;
|
||||||
new_point->y = pointy->y;
|
new_point->y = pointy->y;
|
||||||
|
|
||||||
return new_point;
|
PG_RETURN_POINT_P(new_point);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* By Reference, Variable Length */
|
/* By Reference, Variable Length */
|
||||||
|
|
||||||
text *
|
PG_FUNCTION_INFO_V1(copytext);
|
||||||
copytext(text *t)
|
|
||||||
|
Datum
|
||||||
|
copytext(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
|
text *t = PG_GETARG_TEXT_PP(0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* VARSIZE is the total size of the struct in bytes.
|
* VARSIZE_ANY_EXHDR is the size of the struct in bytes, minus the
|
||||||
|
* VARHDRSZ or VARHDRSZ_SHORT of its header. Construct the copy with a
|
||||||
|
* full-length header.
|
||||||
*/
|
*/
|
||||||
text *new_t = (text *) palloc(VARSIZE(t));
|
text *new_t = (text *) palloc(VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
|
||||||
|
|
||||||
SET_VARSIZE(new_t, VARSIZE(t));
|
SET_VARSIZE(new_t, VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* VARDATA is a pointer to the data region of the struct.
|
* VARDATA is a pointer to the data region of the new struct. The source
|
||||||
|
* could be a short datum, so retrieve its data through VARDATA_ANY.
|
||||||
*/
|
*/
|
||||||
memcpy((void *) VARDATA(new_t), /* destination */
|
memcpy((void *) VARDATA(new_t), /* destination */
|
||||||
(void *) VARDATA(t), /* source */
|
(void *) VARDATA_ANY(t), /* source */
|
||||||
VARSIZE(t) - VARHDRSZ); /* how many bytes */
|
VARSIZE_ANY_EXHDR(t)); /* how many bytes */
|
||||||
return new_t;
|
PG_RETURN_TEXT_P(new_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
text *
|
PG_FUNCTION_INFO_V1(concat_text);
|
||||||
concat_text(text *arg1, text *arg2)
|
|
||||||
|
Datum
|
||||||
|
concat_text(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
int32 arg1_size = VARSIZE(arg1) - VARHDRSZ;
|
text *arg1 = PG_GETARG_TEXT_PP(0);
|
||||||
int32 arg2_size = VARSIZE(arg2) - VARHDRSZ;
|
text *arg2 = PG_GETARG_TEXT_PP(1);
|
||||||
|
int32 arg1_size = VARSIZE_ANY_EXHDR(arg1);
|
||||||
|
int32 arg2_size = VARSIZE_ANY_EXHDR(arg2);
|
||||||
int32 new_text_size = arg1_size + arg2_size + VARHDRSZ;
|
int32 new_text_size = arg1_size + arg2_size + VARHDRSZ;
|
||||||
text *new_text = (text *) palloc(new_text_size);
|
text *new_text = (text *) palloc(new_text_size);
|
||||||
|
|
||||||
SET_VARSIZE(new_text, new_text_size);
|
SET_VARSIZE(new_text, new_text_size);
|
||||||
memcpy(VARDATA(new_text), VARDATA(arg1), arg1_size);
|
memcpy(VARDATA(new_text), VARDATA_ANY(arg1), arg1_size);
|
||||||
memcpy(VARDATA(new_text) + arg1_size, VARDATA(arg2), arg2_size);
|
memcpy(VARDATA(new_text) + arg1_size, VARDATA_ANY(arg2), arg2_size);
|
||||||
return new_text;
|
PG_RETURN_TEXT_P(new_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Composite types */
|
/* Composite types */
|
||||||
|
|
||||||
bool
|
PG_FUNCTION_INFO_V1(c_overpaid);
|
||||||
c_overpaid(HeapTupleHeader t, /* the current instance of EMP */
|
|
||||||
int32 limit)
|
Datum
|
||||||
|
c_overpaid(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
|
HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
|
||||||
|
int32 limit = PG_GETARG_INT32(1);
|
||||||
bool isnull;
|
bool isnull;
|
||||||
int32 salary;
|
int32 salary;
|
||||||
|
|
||||||
salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
|
salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
|
||||||
if (isnull)
|
if (isnull)
|
||||||
return false;
|
PG_RETURN_BOOL(false);
|
||||||
return salary > limit;
|
|
||||||
|
/*
|
||||||
|
* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
|
||||||
|
*/
|
||||||
|
|
||||||
|
PG_RETURN_BOOL(salary > limit);
|
||||||
}
|
}
|
||||||
|
@ -1,129 +0,0 @@
|
|||||||
/* src/tutorial/funcs_new.c */
|
|
||||||
|
|
||||||
/******************************************************************************
|
|
||||||
These are user-defined functions that can be bound to a Postgres backend
|
|
||||||
and called by Postgres to execute SQL functions of the same name.
|
|
||||||
|
|
||||||
The calling format for these functions is defined by the CREATE FUNCTION
|
|
||||||
SQL statement that binds them to the backend.
|
|
||||||
|
|
||||||
NOTE: this file shows examples of "new style" function call conventions.
|
|
||||||
See funcs.c for examples of "old style".
|
|
||||||
*****************************************************************************/
|
|
||||||
|
|
||||||
#include "postgres.h" /* general Postgres declarations */
|
|
||||||
|
|
||||||
#include "executor/executor.h" /* for GetAttributeByName() */
|
|
||||||
#include "utils/geo_decls.h" /* for point type */
|
|
||||||
|
|
||||||
PG_MODULE_MAGIC;
|
|
||||||
|
|
||||||
|
|
||||||
/* By Value */
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(add_one);
|
|
||||||
|
|
||||||
Datum
|
|
||||||
add_one(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
int32 arg = PG_GETARG_INT32(0);
|
|
||||||
|
|
||||||
PG_RETURN_INT32(arg + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* By Reference, Fixed Length */
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(add_one_float8);
|
|
||||||
|
|
||||||
Datum
|
|
||||||
add_one_float8(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
/* The macros for FLOAT8 hide its pass-by-reference nature */
|
|
||||||
float8 arg = PG_GETARG_FLOAT8(0);
|
|
||||||
|
|
||||||
PG_RETURN_FLOAT8(arg + 1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(makepoint);
|
|
||||||
|
|
||||||
Datum
|
|
||||||
makepoint(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
Point *pointx = PG_GETARG_POINT_P(0);
|
|
||||||
Point *pointy = PG_GETARG_POINT_P(1);
|
|
||||||
Point *new_point = (Point *) palloc(sizeof(Point));
|
|
||||||
|
|
||||||
new_point->x = pointx->x;
|
|
||||||
new_point->y = pointy->y;
|
|
||||||
|
|
||||||
PG_RETURN_POINT_P(new_point);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* By Reference, Variable Length */
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(copytext);
|
|
||||||
|
|
||||||
Datum
|
|
||||||
copytext(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
text *t = PG_GETARG_TEXT_PP(0);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* VARSIZE_ANY_EXHDR is the size of the struct in bytes, minus the
|
|
||||||
* VARHDRSZ or VARHDRSZ_SHORT of its header. Construct the copy with a
|
|
||||||
* full-length header.
|
|
||||||
*/
|
|
||||||
text *new_t = (text *) palloc(VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
|
|
||||||
|
|
||||||
SET_VARSIZE(new_t, VARSIZE_ANY_EXHDR(t) + VARHDRSZ);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* VARDATA is a pointer to the data region of the new struct. The source
|
|
||||||
* could be a short datum, so retrieve its data through VARDATA_ANY.
|
|
||||||
*/
|
|
||||||
memcpy((void *) VARDATA(new_t), /* destination */
|
|
||||||
(void *) VARDATA_ANY(t), /* source */
|
|
||||||
VARSIZE_ANY_EXHDR(t)); /* how many bytes */
|
|
||||||
PG_RETURN_TEXT_P(new_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(concat_text);
|
|
||||||
|
|
||||||
Datum
|
|
||||||
concat_text(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
text *arg1 = PG_GETARG_TEXT_PP(0);
|
|
||||||
text *arg2 = PG_GETARG_TEXT_PP(1);
|
|
||||||
int32 arg1_size = VARSIZE_ANY_EXHDR(arg1);
|
|
||||||
int32 arg2_size = VARSIZE_ANY_EXHDR(arg2);
|
|
||||||
int32 new_text_size = arg1_size + arg2_size + VARHDRSZ;
|
|
||||||
text *new_text = (text *) palloc(new_text_size);
|
|
||||||
|
|
||||||
SET_VARSIZE(new_text, new_text_size);
|
|
||||||
memcpy(VARDATA(new_text), VARDATA_ANY(arg1), arg1_size);
|
|
||||||
memcpy(VARDATA(new_text) + arg1_size, VARDATA_ANY(arg2), arg2_size);
|
|
||||||
PG_RETURN_TEXT_P(new_text);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Composite types */
|
|
||||||
|
|
||||||
PG_FUNCTION_INFO_V1(c_overpaid);
|
|
||||||
|
|
||||||
Datum
|
|
||||||
c_overpaid(PG_FUNCTION_ARGS)
|
|
||||||
{
|
|
||||||
HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
|
|
||||||
int32 limit = PG_GETARG_INT32(1);
|
|
||||||
bool isnull;
|
|
||||||
int32 salary;
|
|
||||||
|
|
||||||
salary = DatumGetInt32(GetAttributeByName(t, "salary", &isnull));
|
|
||||||
if (isnull)
|
|
||||||
PG_RETURN_BOOL(false);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Alternatively, we might prefer to do PG_RETURN_NULL() for null salary
|
|
||||||
*/
|
|
||||||
|
|
||||||
PG_RETURN_BOOL(salary > limit);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user