|
|
| · · · · · · · | |
pgAdmin 1.4 online documentationChapter 46. Writing A Procedural Language HandlerAll calls to functions that are written in a language other than the current “version 1” interface for compiled languages (this includes functions in user-defined procedural languages, functions written in SQL, and functions using the version 0 compiled language interface), go through a call handler function for the specific language. It is the responsibility of the call handler to execute the function in a meaningful way, such as by interpreting the supplied source text. This chapter outlines how a new procedural language's call handler can be written. The call handler for a procedural language is a
“normal” function that must be written in a compiled
language such as C, using the version-1 interface, and registered
with PostgreSQL as taking no arguments
and returning the type The call handler is called in the same way as any other function:
It receives a pointer to a
It's up to the call handler to fetch the entry of the function from the
system table
Often, the same function is called many times per SQL statement.
A call handler can avoid repeated lookups of information about the
called function by using the
When a procedural-language function is invoked as a trigger, no arguments
are passed in the usual way, but the
This is a template for a procedural-language handler written in C: #include "postgres.h"
#include "executor/spi.h"
#include "commands/trigger.h"
#include "fmgr.h"
#include "access/heapam.h"
#include "utils/syscache.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
PG_FUNCTION_INFO_V1(plsample_call_handler);
Datum
plsample_call_handler(PG_FUNCTION_ARGS)
{
Datum retval;
if (CALLED_AS_TRIGGER(fcinfo))
{
/*
* Called as a trigger procedure
*/
TriggerData *trigdata = (TriggerData *) fcinfo->context;
retval = ...
}
else
{
/*
* Called as a function
*/
retval = ...
}
return retval;
}
Only a few thousand lines of code have to be added instead of the dots to complete the call handler. After having compiled the handler function into a loadable module (see Section 32.9.6, “Compiling and Linking Dynamically-Loaded Functions”), the following commands then register the sample procedural language: CREATE FUNCTION plsample_call_handler() RETURNS language_handler
AS '
The procedural languages included in the standard distribution
are good references when trying to write your own call handler.
Look into the |