Database Reference
In-Depth Information
try:
plan = SD['is_super_qplan']
except:
SD['is_super_qplan'] = plpy.prepare("....
plan = SD['is_super_qplan']
<the rest of the function>
The values in
SD[]
and
GD[]
only live inside a single database session, so it only
makes sense to do the caching in case you have long-lived connections.
Writing trigger functions in PL/Python
As with other PLs, PL/Pythonu can be used to write trigger functions. The declaration
of a trigger function is different from an ordinary function by the return type
RETURNS
TRIGGER
. So, a simple trigger function that just notifies the caller that it is indeed
called looks like this:
CREATE OR REPLACE FUNCTION notify_on_call()
RETURNS TRIGGER
AS $$
plpy.notice('I was called!')
$$ LANGUAGE plpythonu;
After creating this function, the trigger can be tested on a table using a trigger func-
tion:
hannu=# CREATE TABLE ttable(id int);
CREATE TABLE
hannu=# CREATE TRIGGER ttable_notify BEFORE
INSERT ON ttable EXECUTE PROCEDURE
notify_on_call();
CREATE TRIGGER
hannu=# INSERT INTO ttable VALUES(1);
NOTICE: I was called!
CONTEXT: PL/Python function "notify_on_call"
INSERT 0 1