Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5970271

Browse files
committed
Add transition table support to plpgsql.
Kevin Grittner and Thomas MunroReviewed by Heikki Linnakangas, David Fetter, and Thomas Munrowith valuable comments and suggestions from many others
1 parent18ce3a4 commit5970271

File tree

8 files changed

+685
-11
lines changed

8 files changed

+685
-11
lines changed

‎doc/src/sgml/ref/create_trigger.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ UPDATE OF <replaceable>column_name1</replaceable> [, <replaceable>column_name2</
322322
<para>
323323
The (unqualified) name to be used within the trigger for this relation.
324324
</para>
325+
<note>
326+
<para>
327+
So far only triggers written in C or PL/pgSQL support this.
328+
</para>
329+
</note>
325330
</listitem>
326331
</varlistentry>
327332

‎src/pl/plpgsql/src/pl_comp.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,11 @@ do_compile(FunctionCallInfo fcinfo,
589589
errmsg("trigger functions cannot have declared arguments"),
590590
errhint("The arguments of the trigger can be accessed through TG_NARGS and TG_ARGV instead.")));
591591

592-
/* Add the record for referencing NEW */
592+
/* Add the record for referencing NEWROW*/
593593
rec=plpgsql_build_record("new",0, true);
594594
function->new_varno=rec->dno;
595595

596-
/* Add the record for referencing OLD */
596+
/* Add the record for referencing OLDROW*/
597597
rec=plpgsql_build_record("old",0, true);
598598
function->old_varno=rec->dno;
599599

@@ -2453,15 +2453,16 @@ compute_function_hashkey(FunctionCallInfo fcinfo,
24532453
hashkey->isTrigger=CALLED_AS_TRIGGER(fcinfo);
24542454

24552455
/*
2456-
* if trigger, get relation OID. In validation mode we do not know what
2457-
* relation is intended to be used, so we leave trigrelOid zero; the hash
2458-
* entry built in this case will never really be used.
2456+
* if trigger, get its OID. In validation mode we do not know what
2457+
* relation or transition table names are intended to be used, so we leave
2458+
* trigOid zero; the hash entry built in this case will never really be
2459+
* used.
24592460
*/
24602461
if (hashkey->isTrigger&& !forValidator)
24612462
{
24622463
TriggerData*trigdata= (TriggerData*)fcinfo->context;
24632464

2464-
hashkey->trigrelOid=RelationGetRelid(trigdata->tg_relation);
2465+
hashkey->trigOid=trigdata->tg_trigger->tgoid;
24652466
}
24662467

24672468
/* get input collation, if known */

‎src/pl/plpgsql/src/pl_exec.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,47 @@ plpgsql_exec_trigger(PLpgSQL_function *func,
689689
else
690690
elog(ERROR,"unrecognized trigger action: not INSERT, DELETE, or UPDATE");
691691

692+
/*
693+
* Capture the NEW and OLD transition TABLE tuplestores (if specified for
694+
* this trigger).
695+
*/
696+
if (trigdata->tg_newtable||trigdata->tg_oldtable)
697+
{
698+
estate.queryEnv=create_queryEnv();
699+
if (trigdata->tg_newtable)
700+
{
701+
EphemeralNamedRelationenr=
702+
palloc(sizeof(EphemeralNamedRelationData));
703+
intrcPG_USED_FOR_ASSERTS_ONLY;
704+
705+
enr->md.name=trigdata->tg_trigger->tgnewtable;
706+
enr->md.reliddesc=RelationGetRelid(trigdata->tg_relation);
707+
enr->md.tupdesc=NULL;
708+
enr->md.enrtype=ENR_NAMED_TUPLESTORE;
709+
enr->md.enrtuples=tuplestore_tuple_count(trigdata->tg_newtable);
710+
enr->reldata=trigdata->tg_newtable;
711+
register_ENR(estate.queryEnv,enr);
712+
rc=SPI_register_relation(enr);
713+
Assert(rc >=0);
714+
}
715+
if (trigdata->tg_oldtable)
716+
{
717+
EphemeralNamedRelationenr=
718+
palloc(sizeof(EphemeralNamedRelationData));
719+
intrcPG_USED_FOR_ASSERTS_ONLY;
720+
721+
enr->md.name=trigdata->tg_trigger->tgoldtable;
722+
enr->md.reliddesc=RelationGetRelid(trigdata->tg_relation);
723+
enr->md.tupdesc=NULL;
724+
enr->md.enrtype=ENR_NAMED_TUPLESTORE;
725+
enr->md.enrtuples=tuplestore_tuple_count(trigdata->tg_oldtable);
726+
enr->reldata=trigdata->tg_oldtable;
727+
register_ENR(estate.queryEnv,enr);
728+
rc=SPI_register_relation(enr);
729+
Assert(rc >=0);
730+
}
731+
}
732+
692733
/*
693734
* Assign the special tg_ variables
694735
*/
@@ -3442,6 +3483,9 @@ plpgsql_estate_setup(PLpgSQL_execstate *estate,
34423483
estate->paramLI->paramMask=NULL;
34433484
estate->params_dirty= false;
34443485

3486+
/* default tuplestore cache to "none" */
3487+
estate->queryEnv=NULL;
3488+
34453489
/* set up for use of appropriate simple-expression EState and cast hash */
34463490
if (simple_eval_estate)
34473491
{
@@ -7329,6 +7373,9 @@ exec_dynquery_with_params(PLpgSQL_execstate *estate,
73297373
/* Release transient data */
73307374
MemoryContextReset(stmt_mcontext);
73317375

7376+
/* Make sure the portal knows about any named tuplestores. */
7377+
portal->queryEnv=estate->queryEnv;
7378+
73327379
returnportal;
73337380
}
73347381

‎src/pl/plpgsql/src/plpgsql.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include"commands/event_trigger.h"
2121
#include"commands/trigger.h"
2222
#include"executor/spi.h"
23+
#include"utils/queryenvironment.h"
2324

2425
/**********************************************************************
2526
* Definitions
@@ -780,12 +781,12 @@ typedef struct PLpgSQL_func_hashkey
780781
/* be careful that pad bytes in this struct get zeroed! */
781782

782783
/*
783-
* For a trigger function, the OID of therelation triggered onis part of
784-
*the hash key--- we want to compile the trigger separately for each
785-
*relationit is used with, in case the rowtypeis different. Zero if
786-
* not called as a trigger.
784+
* For a trigger function, the OID of thetriggeris part of the hash key
785+
* --- we want to compile the triggerfunctionseparately for each trigger
786+
* it is used with, in case the rowtypeor transition table names are
787+
*different. Zero ifnot called as a trigger.
787788
*/
788-
OidtrigrelOid;
789+
OidtrigOid;
789790

790791
/*
791792
* We must include the input collation as part of the hash key too,
@@ -910,6 +911,9 @@ typedef struct PLpgSQL_execstate
910911
ParamListInfoparamLI;
911912
boolparams_dirty;/* T if any resettable datum has been passed */
912913

914+
/* custom environment for parsing/execution of query for this context */
915+
QueryEnvironment*queryEnv;
916+
913917
/* EState to use for "simple" expression evaluation */
914918
EState*simple_eval_estate;
915919

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp