Skip to content

Commit

Permalink
Follow-on cleanup for the transition table patch.
Browse files Browse the repository at this point in the history
Commit 5970271 added transition table support to PL/pgsql so that
SQL queries in trigger functions could access those transient
tables.  In order to provide the same level of support for PL/perl,
PL/python and PL/tcl, refactor the relevant code into a new
function SPI_register_trigger_data.  Call the new function in the
trigger handler of all four PLs, and document it as a public SPI
function so that authors of out-of-tree PLs can do the same.

Also get rid of a second QueryEnvironment object that was
maintained by PL/pgsql.  That was previously used to deal with
cursors, but the same approach wasn't appropriate for PLs that are
less tangled up with core code.  Instead, have SPI_cursor_open
install the connection's current QueryEnvironment, as already
happens for SPI_execute_plan.

While in the docs, remove the note that transition tables were only
supported in C and PL/pgSQL triggers, and correct some ommissions.

Thomas Munro with some work by Kevin Grittner (mostly docs)
  • Loading branch information
kgrittn committed Apr 4, 2017
1 parent 9a32150 commit 5ebeb57
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 63 deletions.
10 changes: 5 additions & 5 deletions doc/src/sgml/ref/create_trigger.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ PostgreSQL documentation
<primary>CREATE TRIGGER</primary>
</indexterm>

<indexterm>
<primary>transition tables</primary>
<seealso>ephemeral named relation</seealso>
</indexterm>

<refmeta>
<refentrytitle>CREATE TRIGGER</refentrytitle>
<manvolnum>7</manvolnum>
Expand Down Expand Up @@ -322,11 +327,6 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
<para>
The (unqualified) name to be used within the trigger for this relation.
</para>
<note>
<para>
So far only triggers written in C or PL/pgSQL support this.
</para>
</note>
</listitem>
</varlistentry>

Expand Down
125 changes: 125 additions & 0 deletions doc/src/sgml/spi.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -2644,6 +2644,11 @@ SPIPlanPtr SPI_saveplan(SPIPlanPtr <parameter>plan</parameter>)
<refentry id="spi-spi-register-relation">
<indexterm><primary>SPI_register_relation</primary></indexterm>

<indexterm>
<primary>ephemeral named relation</primary>
<secondary>registering with SPI</secondary>
</indexterm>

<refmeta>
<refentrytitle>SPI_register_relation</refentrytitle>
<manvolnum>3</manvolnum>
Expand Down Expand Up @@ -2746,6 +2751,11 @@ int SPI_register_relation(EphemeralNamedRelation <parameter>enr</parameter>)
<refentry id="spi-spi-unregister-relation">
<indexterm><primary>SPI_unregister_relation</primary></indexterm>

<indexterm>
<primary>ephemeral named relation</primary>
<secondary>unregistering from SPI</secondary>
</indexterm>

<refmeta>
<refentrytitle>SPI_unregister_relation</refentrytitle>
<manvolnum>3</manvolnum>
Expand Down Expand Up @@ -2843,6 +2853,121 @@ int SPI_unregister_relation(const char * <parameter>name</parameter>)

<!-- *********************************************** -->

<refentry id="spi-spi-register-trigger-data">
<indexterm><primary>SPI_register_trigger_data</primary></indexterm>

<indexterm>
<primary>ephemeral named relation</primary>
<secondary>registering with SPI</secondary>
</indexterm>

<indexterm>
<primary>transition tables</primary>
<secondary>implementation in PLs</secondary>
</indexterm>

<refmeta>
<refentrytitle>SPI_register_trigger_data</refentrytitle>
<manvolnum>3</manvolnum>
</refmeta>

<refnamediv>
<refname>SPI_register_trigger_data</refname>
<refpurpose>make ephemeral trigger data available in SPI queries</refpurpose>
</refnamediv>

<refsynopsisdiv>
<synopsis>
int SPI_register_trigger_data(TriggerData *<parameter>tdata</parameter>)
</synopsis>
</refsynopsisdiv>

<refsect1>
<title>Description</title>

<para>
<function>SPI_register_trigger_data</function> makes any ephemeral
relations captured by a trigger available to queries planned and executed
through the current SPI connection. Currently, this means the transition
tables captured by an <literal>AFTER</literal> trigger defined with a
<literal>REFERENCING OLD/NEW TABLE AS</literal> ... clause. This function
should be called by a PL trigger handler function after connecting.
</para>
</refsect1>

<refsect1>
<title>Arguments</title>

<variablelist>
<varlistentry>
<term><literal>TriggerData *<parameter>tdata</parameter></literal></term>
<listitem>
<para>
the <structname>TriggerData</structname> object passed to a trigger
handler function as <literal>fcinfo->context</literal>
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>

<refsect1>
<title>Return Value</title>

<para>
If the execution of the command was successful then the following
(nonnegative) value will be returned:

<variablelist>
<varlistentry>
<term><symbol>SPI_OK_TD_REGISTER</symbol></term>
<listitem>
<para>
if the captured trigger data (if any) has been successfully registered
</para>
</listitem>
</varlistentry>
</variablelist>
</para>

<para>
On error, one of the following negative values is returned:

<variablelist>
<varlistentry>
<term><symbol>SPI_ERROR_ARGUMENT</symbol></term>
<listitem>
<para>
if <parameter>tdata</parameter> is <symbol>NULL</symbol>
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><symbol>SPI_ERROR_UNCONNECTED</symbol></term>
<listitem>
<para>
if called from an unconnected procedure
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><symbol>SPI_ERROR_REL_DUPLICATE</symbol></term>
<listitem>
<para>
if the name of any trigger data transient relation is already
registered for this connection
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
</refentry>

<!-- *********************************************** -->

</sect1>

<sect1 id="spi-interface-support">
Expand Down
54 changes: 46 additions & 8 deletions doc/src/sgml/trigger.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@
<secondary>in C</secondary>
</indexterm>

<indexterm>
<primary>transition tables</primary>
<secondary>referencing from C trigger</secondary>
</indexterm>

<para>
This section describes the low-level details of the interface to a
trigger function. This information is only needed when writing
Expand Down Expand Up @@ -438,14 +443,16 @@ CALLED_AS_TRIGGER(fcinfo)
<programlisting>
typedef struct TriggerData
{
NodeTag type;
TriggerEvent tg_event;
Relation tg_relation;
HeapTuple tg_trigtuple;
HeapTuple tg_newtuple;
Trigger *tg_trigger;
Buffer tg_trigtuplebuf;
Buffer tg_newtuplebuf;
NodeTag type;
TriggerEvent tg_event;
Relation tg_relation;
HeapTuple tg_trigtuple;
HeapTuple tg_newtuple;
Trigger *tg_trigger;
Buffer tg_trigtuplebuf;
Buffer tg_newtuplebuf;
Tuplestorestate *tg_oldtable;
Tuplestorestate *tg_newtable;
} TriggerData;
</programlisting>

Expand Down Expand Up @@ -629,6 +636,8 @@ typedef struct Trigger
int16 *tgattr;
char **tgargs;
char *tgqual;
char *tgoldtable;
char *tgnewtable;
} Trigger;
</programlisting>

Expand Down Expand Up @@ -662,9 +671,38 @@ typedef struct Trigger
</listitem>
</varlistentry>

<varlistentry>
<term><structfield>tg_oldtable</></term>
<listitem>
<para>
A pointer to a structure of type <structname>Tuplestorestate</structname>
containing zero or more rows in the format specified by
<structfield>tg_relation</structfield>, or a <symbol>NULL</> pointer
if there is no <literal>OLD TABLE</literal> transition relation.
</para>
</listitem>
</varlistentry>

<varlistentry>
<term><structfield>tg_newtable</></term>
<listitem>
<para>
A pointer to a structure of type <structname>Tuplestorestate</structname>
containing zero or more rows in the format specified by
<structfield>tg_relation</structfield>, or a <symbol>NULL</> pointer
if there is no <literal>NEW TABLE</literal> transition relation.
</para>
</listitem>
</varlistentry>

</variablelist>
</para>

<para>
To allow queries issued through SPI to reference transition tables, see
<xref linkend="spi-spi-register-trigger-data">.
</para>

<para>
A trigger function must return either a
<structname>HeapTuple</> pointer or a <symbol>NULL</> pointer
Expand Down
52 changes: 52 additions & 0 deletions src/backend/executor/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,9 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
errdetail("Scrollable cursors must be READ ONLY.")));
}

/* Make current query environment available to portal at execution time. */
portal->queryEnv = _SPI_current->queryEnv;

/*
* If told to be read-only, or in parallel mode, verify that this query is
* in fact read-only. This can't be done earlier because we need to look
Expand Down Expand Up @@ -2716,3 +2719,52 @@ SPI_unregister_relation(const char *name)

return res;
}

/*
* Register the transient relations from 'tdata' using this SPI connection.
* This should be called by PL implementations' trigger handlers after
* connecting, in order to make transition tables visible to any queries run
* in this connection.
*/
int
SPI_register_trigger_data(TriggerData *tdata)
{
if (tdata == NULL)
return SPI_ERROR_ARGUMENT;

if (tdata->tg_newtable)
{
EphemeralNamedRelation enr =
palloc(sizeof(EphemeralNamedRelationData));
int rc;

enr->md.name = tdata->tg_trigger->tgnewtable;
enr->md.reliddesc = tdata->tg_relation->rd_id;
enr->md.tupdesc = NULL;
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_newtable);
enr->reldata = tdata->tg_newtable;
rc = SPI_register_relation(enr);
if (rc != SPI_OK_REL_REGISTER)
return rc;
}

if (tdata->tg_oldtable)
{
EphemeralNamedRelation enr =
palloc(sizeof(EphemeralNamedRelationData));
int rc;

enr->md.name = tdata->tg_trigger->tgoldtable;
enr->md.reliddesc = tdata->tg_relation->rd_id;
enr->md.tupdesc = NULL;
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
enr->md.enrtuples = tuplestore_tuple_count(tdata->tg_oldtable);
enr->reldata = tdata->tg_oldtable;
rc = SPI_register_relation(enr);
if (rc != SPI_OK_REL_REGISTER)
return rc;
}

return SPI_OK_TD_REGISTER;
}
3 changes: 3 additions & 0 deletions src/include/executor/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SPI_H
#define SPI_H

#include "commands/trigger.h"
#include "lib/ilist.h"
#include "nodes/parsenodes.h"
#include "utils/portal.h"
Expand Down Expand Up @@ -62,6 +63,7 @@ typedef struct _SPI_plan *SPIPlanPtr;
#define SPI_OK_REWRITTEN 14
#define SPI_OK_REL_REGISTER 15
#define SPI_OK_REL_UNREGISTER 16
#define SPI_OK_TD_REGISTER 17

/* These used to be functions, now just no-ops for backwards compatibility */
#define SPI_push() ((void) 0)
Expand Down Expand Up @@ -152,6 +154,7 @@ extern void SPI_cursor_close(Portal portal);

extern int SPI_register_relation(EphemeralNamedRelation enr);
extern int SPI_unregister_relation(const char *name);
extern int SPI_register_trigger_data(TriggerData *tdata);

extern void AtEOXact_SPI(bool isCommit);
extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid);
Expand Down
29 changes: 29 additions & 0 deletions src/pl/plperl/expected/plperl_trigger.out
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,35 @@ $$ LANGUAGE plperl;
SELECT direct_trigger();
ERROR: trigger functions can only be called as triggers
CONTEXT: compilation of PL/Perl function "direct_trigger"
-- check that SQL run in trigger code can see transition tables
CREATE TABLE transition_table_test (id int, name text);
INSERT INTO transition_table_test VALUES (1, 'a');
CREATE FUNCTION transition_table_test_f() RETURNS trigger LANGUAGE plperl AS
$$
my $cursor = spi_query("SELECT * FROM old_table");
my $row = spi_fetchrow($cursor);
defined($row) || die "expected a row";
elog(INFO, "old: " . $row->{id} . " -> " . $row->{name});
my $row = spi_fetchrow($cursor);
!defined($row) || die "expected no more rows";

my $cursor = spi_query("SELECT * FROM new_table");
my $row = spi_fetchrow($cursor);
defined($row) || die "expected a row";
elog(INFO, "new: " . $row->{id} . " -> " . $row->{name});
my $row = spi_fetchrow($cursor);
!defined($row) || die "expected no more rows";

return undef;
$$;
CREATE TRIGGER a_t AFTER UPDATE ON transition_table_test
REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
FOR EACH STATEMENT EXECUTE PROCEDURE transition_table_test_f();
UPDATE transition_table_test SET name = 'b';
INFO: old: 1 -> a
INFO: new: 1 -> b
DROP TABLE transition_table_test;
DROP FUNCTION transition_table_test_f();
-- test plperl command triggers
create or replace function perlsnitch() returns event_trigger language plperl as $$
elog(NOTICE, "perlsnitch: " . $_TD->{event} . " " . $_TD->{tag} . " ");
Expand Down
7 changes: 7 additions & 0 deletions src/pl/plperl/plperl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2442,11 +2442,18 @@ plperl_trigger_handler(PG_FUNCTION_ARGS)
SV *svTD;
HV *hvTD;
ErrorContextCallback pl_error_context;
TriggerData *tdata;
int rc PG_USED_FOR_ASSERTS_ONLY;

/* Connect to SPI manager */
if (SPI_connect() != SPI_OK_CONNECT)
elog(ERROR, "could not connect to SPI manager");

/* Make transition tables visible to this SPI connection */
tdata = (TriggerData *) fcinfo->context;
rc = SPI_register_trigger_data(tdata);
Assert(rc >= 0);

/* Find or compile the function */
prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, true, false);
current_call_data->prodesc = prodesc;
Expand Down
Loading

0 comments on commit 5ebeb57

Please sign in to comment.