head	1.13;
access;
symbols
	REL9_0_0:1.13
	REL9_1_ALPHA1:1.13
	REL9_0_RC1:1.13
	REL9_0_BETA4:1.13
	REL9_0_STABLE:1.13.0.16
	REL9_0_BETA3:1.13
	REL9_0_BETA2:1.13
	REL7_4_29:1.4
	REL8_0_25:1.5
	REL8_1_21:1.6
	REL8_2_17:1.12
	REL8_3_11:1.13
	REL8_4_4:1.13
	REL9_0_BETA1:1.13
	REL9_0_ALPHA5_BRANCH:1.13.0.14
	REL9_0_ALPHA5:1.13
	REL7_4_28:1.4
	REL8_0_24:1.5
	REL8_1_20:1.6
	REL8_2_16:1.12
	REL8_3_10:1.13
	REL8_4_3:1.13
	REL9_0_ALPHA4:1.13
	REL9_0_ALPHA4_BRANCH:1.13.0.12
	REL8_5_ALPHA3:1.13
	REL8_5_ALPHA3_BRANCH:1.13.0.10
	REL7_4_27:1.4
	REL8_0_23:1.5
	REL8_1_19:1.6
	REL8_2_15:1.12
	REL8_3_9:1.13
	REL8_4_2:1.13
	REL8_5_ALPHA2:1.13
	REL8_5_ALPHA2_BRANCH:1.13.0.8
	REL7_4_26:1.4
	REL8_0_22:1.5
	REL8_1_18:1.6
	REL8_2_14:1.12
	REL8_3_8:1.13
	REL8_4_1:1.13
	REL8_5_ALPHA1:1.13
	REL8_5_ALPHA1_BRANCH:1.13.0.6
	REL8_4_STABLE:1.13.0.4
	REL8_4_0:1.13
	REL8_4_RC2:1.13
	REL8_4_RC1:1.13
	REL8_4_BETA2:1.13
	REL8_4_BETA1:1.13
	REL7_4_25:1.4
	REL8_0_21:1.5
	REL8_1_17:1.6
	REL8_2_13:1.12
	REL8_3_7:1.13
	REL7_4_24:1.4
	REL8_0_20:1.5
	REL8_1_16:1.6
	REL8_2_12:1.12
	REL8_3_6:1.13
	REL7_4_23:1.4
	REL8_0_19:1.5
	REL8_1_15:1.6
	REL8_2_11:1.12
	REL8_3_5:1.13
	REL7_4_22:1.4
	REL8_0_18:1.5
	REL8_1_14:1.6
	REL8_2_10:1.12
	REL8_3_4:1.13
	REL7_4_21:1.4
	REL8_0_17:1.5
	REL8_1_13:1.6
	REL8_2_9:1.12
	REL8_3_3:1.13
	REL7_4_20:1.4
	REL8_0_16:1.5
	REL8_1_12:1.6
	REL8_2_8:1.12
	REL8_3_2:1.13
	REL8_2_7:1.12
	REL8_3_1:1.13
	REL8_3_STABLE:1.13.0.2
	REL8_3_0:1.13
	REL8_3_RC2:1.13
	REL7_3_21:1.4
	REL7_4_19:1.4
	REL8_0_15:1.5
	REL8_1_11:1.6
	REL8_2_6:1.12
	REL8_3_RC1:1.13
	REL8_3_BETA4:1.13
	REL8_3_BETA3:1.13
	REL8_3_BETA2:1.13
	REL8_3_BETA1:1.13
	REL7_3_20:1.4
	REL7_4_18:1.4
	REL8_0_14:1.5
	REL8_1_10:1.6
	REL8_2_5:1.12
	REL7_3_19:1.4
	REL7_4_17:1.4
	REL8_0_13:1.5
	REL8_1_9:1.6
	REL8_2_4:1.12
	REL8_0_12:1.5
	REL8_1_8:1.6
	REL8_2_3:1.12
	REL7_3_18:1.4
	REL7_4_16:1.4
	REL8_0_11:1.5
	REL8_1_7:1.6
	REL8_2_2:1.12
	REL8_0_10:1.5
	REL8_1_6:1.6
	REL8_2_1:1.12
	REL7_4_15:1.4
	REL7_3_17:1.4
	REL8_2_STABLE:1.12.0.2
	REL8_2_0:1.12
	REL8_2_RC1:1.12
	REL8_2_BETA3:1.12
	REL8_2_BETA2:1.12
	REL8_1_5:1.6
	REL8_0_9:1.5
	REL7_4_14:1.4
	REL7_3_16:1.4
	REL8_2_BETA1:1.11
	REL7_3_15:1.4
	REL7_4_13:1.4
	REL8_0_8:1.5
	REL8_1_4:1.6
	REL7_3_14:1.4
	REL7_4_12:1.4
	REL8_0_7:1.5
	REL8_1_3:1.6
	REL7_3_13:1.4
	REL7_4_11:1.4
	REL8_0_6:1.5
	REL8_1_2:1.6
	REL7_3_12:1.4
	REL7_4_10:1.4
	REL8_0_5:1.5
	REL8_1_1:1.6
	REL8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1.4.0.2
	REL7_1_BETA:1.2
	REL7_1_BETA3:1.2
	REL7_1_BETA2:1.2
	REL7_1:1.4;
locks; strict;
comment	@ * @;


1.13
date	2007.02.27.23.48.10;	author tgl;	state Exp;
branches;
next	1.12;

1.12
date	2006.09.27.16.19.42;	author tgl;	state Exp;
branches;
next	1.11;

1.11
date	2006.07.14.14.52.27;	author momjian;	state Exp;
branches;
next	1.10;

1.10
date	2006.07.14.04.59.30;	author momjian;	state Exp;
branches;
next	1.9;

1.9
date	2006.07.14.04.44.46;	author momjian;	state Exp;
branches;
next	1.8;

1.8
date	2006.05.30.22.12.16;	author tgl;	state Exp;
branches;
next	1.7;

1.7
date	2006.03.11.04.38.42;	author momjian;	state Exp;
branches;
next	1.6;

1.6
date	2005.10.15.02.49.52;	author momjian;	state Exp;
branches;
next	1.5;

1.5
date	2004.04.01.21.28.47;	author tgl;	state Exp;
branches;
next	1.4;

1.4
date	2001.03.22.04.01.46;	author momjian;	state Exp;
branches;
next	1.3;

1.3
date	2001.02.10.02.31.31;	author tgl;	state Exp;
branches;
next	1.2;

1.2
date	2000.11.20.20.36.52;	author tgl;	state Exp;
branches;
next	1.1;

1.1
date	2000.08.24.23.32.43;	author tgl;	state Exp;
branches;
next	;


desc
@@


1.13
log
@Replace direct assignments to VARATT_SIZEP(x) with SET_VARSIZE(x, len).
Get rid of VARATT_SIZE and VARATT_DATA, which were simply redundant with
VARSIZE and VARDATA, and as a consequence almost no code was using the
longer names.  Rename the length fields of struct varlena and various
derived structures to catch anyplace that was accessing them directly;
and clean up various places so caught.  In itself this patch doesn't
change any behavior at all, but it is necessary infrastructure if we hope
to play any games with the representation of varlena headers.
Greg Stark and Tom Lane
@
text
@/* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.12 2006/09/27 16:19:42 tgl Exp $ */

/******************************************************************************
  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;

/* These prototypes just prevent possible warnings from gcc. */

Datum		add_one(PG_FUNCTION_ARGS);
Datum		add_one_float8(PG_FUNCTION_ARGS);
Datum		makepoint(PG_FUNCTION_ARGS);
Datum		copytext(PG_FUNCTION_ARGS);
Datum		concat_text(PG_FUNCTION_ARGS);
Datum		c_overpaid(PG_FUNCTION_ARGS);


/* 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_P(0);

	/*
	 * VARSIZE is the total size of the struct in bytes.
	 */
	text	   *new_t = (text *) palloc(VARSIZE(t));

	SET_VARSIZE(new_t, VARSIZE(t));

	/*
	 * VARDATA is a pointer to the data region of the struct.
	 */
	memcpy((void *) VARDATA(new_t),		/* destination */
		   (void *) VARDATA(t), /* source */
		   VARSIZE(t) - VARHDRSZ);		/* 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_P(0);
	text	   *arg2 = PG_GETARG_TEXT_P(1);
	int32		arg1_size = VARSIZE(arg1) - VARHDRSZ;
	int32		arg2_size = VARSIZE(arg2) - VARHDRSZ;
	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(arg1), arg1_size);
	memcpy(VARDATA(new_text) + arg1_size, VARDATA(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);
}
@


1.12
log
@Fix some misuses of strncat().
@
text
@d1 1
a1 1
/* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.11 2006/07/14 14:52:27 momjian Exp $ */
d86 1
a86 1
	VARATT_SIZEP(new_t) = VARSIZE(t);
d109 1
a109 1
	VARATT_SIZEP(new_text) = new_text_size;
@


1.11
log
@Remove 576 references of include files that were not needed.
@
text
@d1 1
a1 1
/* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.10 2006/07/14 04:59:30 momjian Exp $ */
d104 3
a106 1
	int32		new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
a108 1
	memset((void *) new_text, 0, new_text_size);
d110 2
a111 2
	strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1) - VARHDRSZ);
	strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2) - VARHDRSZ);
@


1.10
log
@Revert part of recent include patch not ready for application.
@
text
@d1 1
a1 1
/* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.8 2006/05/30 22:12:16 tgl Exp $ */
a15 1
#include "fmgr.h"				/* for argument/result macros */
@


1.9
log
@Add additional includes needed on some platforms.
@
text
@d16 1
@


1.8
log
@Magic blocks don't do us any good unless we use 'em ... so install one
in every shared library.
@
text
@d1 1
a1 1
/* $PostgreSQL: pgsql/src/tutorial/funcs_new.c,v 1.7 2006/03/11 04:38:42 momjian Exp $ */
a15 1
#include "fmgr.h"				/* for argument/result macros */
@


1.7
log
@Add CVS tag lines to files that were lacking them.
@
text
@d1 1
a1 1
/* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.314 2006/03/07 02:54:23 momjian Exp $ */
d21 2
@


1.6
log
@Standard pgindent run for 8.1.
@
text
@d1 2
@


1.5
log
@Replace TupleTableSlot convention for whole-row variables and function
results with tuples as ordinary varlena Datums.  This commit does not
in itself do much for us, except eliminate the horrid memory leak
associated with evaluation of whole-row variables.  However, it lays the
groundwork for allowing composite types as table columns, and perhaps
some other useful features as well.  Per my proposal of a few days ago.
@
text
@d128 1
a128 2
	 * Alternatively, we might prefer to do PG_RETURN_NULL() for null
	 * salary
@


1.4
log
@pgindent run.  Make it all clean.
@
text
@d118 1
a118 1
	TupleTableSlot *t = (TupleTableSlot *) PG_GETARG_POINTER(0);
@


1.3
log
@Restructure the key include files per recent pghackers discussion: there
are now separate files "postgres.h" and "postgres_fe.h", which are meant
to be the primary include files for backend .c files and frontend .c files
respectively.  By default, only include files meant for frontend use are
installed into the installation include directory.  There is a new make
target 'make install-all-headers' that adds the whole content of the
src/include tree to the installed fileset, for use by people who want to
develop server-side code without keeping the complete source tree on hand.
Cleaned up a whole lot of crufty and inconsistent header inclusions.
@
text
@d14 1
a14 1
#include "fmgr.h"               /* for argument/result macros */
d21 6
a26 6
Datum	add_one(PG_FUNCTION_ARGS);
Datum	add_one_float8(PG_FUNCTION_ARGS);
Datum	makepoint(PG_FUNCTION_ARGS);
Datum	copytext(PG_FUNCTION_ARGS);
Datum	concat_text(PG_FUNCTION_ARGS);
Datum   c_overpaid(PG_FUNCTION_ARGS);
d30 1
a30 1
         
d36 1
a36 1
    int32   arg = PG_GETARG_INT32(0);
d38 1
a38 1
    PG_RETURN_INT32(arg + 1);
d48 2
a49 2
    /* The macros for FLOAT8 hide its pass-by-reference nature */
    float8   arg = PG_GETARG_FLOAT8(0);
d51 1
a51 1
    PG_RETURN_FLOAT8(arg + 1.0);
d59 8
a66 8
    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);
d76 16
a91 13
    text     *t = PG_GETARG_TEXT_P(0);
    /*
     * VARSIZE is the total size of the struct in bytes.
     */
    text     *new_t = (text *) palloc(VARSIZE(t));
    VARATT_SIZEP(new_t) = VARSIZE(t);
    /*
     * VARDATA is a pointer to the data region of the struct.
     */
    memcpy((void *) VARDATA(new_t), /* destination */
           (void *) VARDATA(t),     /* source */
           VARSIZE(t)-VARHDRSZ);        /* how many bytes */
    PG_RETURN_TEXT_P(new_t);
d99 10
a108 10
    text  *arg1 = PG_GETARG_TEXT_P(0);
    text  *arg2 = PG_GETARG_TEXT_P(1);
    int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
    text *new_text = (text *) palloc(new_text_size);

    memset((void *) new_text, 0, new_text_size);
    VARATT_SIZEP(new_text) = new_text_size;
    strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
    strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
    PG_RETURN_TEXT_P(new_text);
d118 13
a130 9
    TupleTableSlot  *t = (TupleTableSlot *) PG_GETARG_POINTER(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 */
d132 1
a132 1
    PG_RETURN_BOOL(salary > limit);
@


1.2
log
@Revise handling of oldstyle/newstyle functions per recent discussions
in pghackers list.  Support for oldstyle internal functions is gone
(no longer needed, since conversion is complete) and pg_language entry
'internal' now implies newstyle call convention.  pg_language entry
'newC' is gone; both old and newstyle dynamically loaded C functions
are now called language 'C'.  A newstyle function must be identified
by an associated info routine.  See src/backend/utils/fmgr/README.
@
text
@a11 2
#include <string.h>

@


1.1
log
@Update funcs tutorial for new function manager.
@
text
@d33 2
d45 2
d56 2
d73 2
d93 2
d111 2
@
