*** a/src/pl/plpgsql/src/pl_comp.c
--- b/src/pl/plpgsql/src/pl_comp.c
***************
*** 594,604 **** do_compile(FunctionCallInfo fcinfo,
  				  errmsg("trigger functions cannot have declared arguments"),
  						 errhint("The arguments of the trigger can be accessed through TG_NARGS and TG_ARGV instead.")));
  
! 			/* Add the record for referencing NEW */
  			rec = plpgsql_build_record("new", 0, true);
  			function->new_varno = rec->dno;
  
! 			/* Add the record for referencing OLD */
  			rec = plpgsql_build_record("old", 0, true);
  			function->old_varno = rec->dno;
  
--- 594,604 ----
  				  errmsg("trigger functions cannot have declared arguments"),
  						 errhint("The arguments of the trigger can be accessed through TG_NARGS and TG_ARGV instead.")));
  
! 			/* Add the record for referencing NEW ROW */
  			rec = plpgsql_build_record("new", 0, true);
  			function->new_varno = rec->dno;
  
! 			/* Add the record for referencing OLD ROW */
  			rec = plpgsql_build_record("old", 0, true);
  			function->old_varno = rec->dno;
  
***************
*** 2402,2416 **** compute_function_hashkey(FunctionCallInfo fcinfo,
  	hashkey->isTrigger = CALLED_AS_TRIGGER(fcinfo);
  
  	/*
! 	 * if trigger, get relation OID.  In validation mode we do not know what
! 	 * relation is intended to be used, so we leave trigrelOid zero; the hash
! 	 * entry built in this case will never really be used.
  	 */
  	if (hashkey->isTrigger && !forValidator)
  	{
  		TriggerData *trigdata = (TriggerData *) fcinfo->context;
  
! 		hashkey->trigrelOid = RelationGetRelid(trigdata->tg_relation);
  	}
  
  	/* get input collation, if known */
--- 2402,2417 ----
  	hashkey->isTrigger = CALLED_AS_TRIGGER(fcinfo);
  
  	/*
! 	 * if trigger, get its OID.  In validation mode we do not know what
! 	 * relation or transition table names are intended to be used, so we leave
! 	 * trigOid zero; the hash entry built in this case will never really be
! 	 * used.
  	 */
  	if (hashkey->isTrigger && !forValidator)
  	{
  		TriggerData *trigdata = (TriggerData *) fcinfo->context;
  
! 		hashkey->trigOid = trigdata->tg_trigger->tgoid;
  	}
  
  	/* get input collation, if known */
*** a/src/pl/plpgsql/src/pl_exec.c
--- b/src/pl/plpgsql/src/pl_exec.c
***************
*** 583,588 **** plpgsql_exec_trigger(PLpgSQL_function *func,
--- 583,613 ----
  		elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
  
  	/*
+ 	 * Capture the NEW and OLD transition TABLE tuplestores (if specified for
+ 	 * this trigger).
+ 	 */
+ 	if (trigdata->tg_newtable)
+ 	{
+ 		Tsr tsr = palloc(sizeof(TsrData));
+ 
+ 		tsr->name = trigdata->tg_trigger->tgnewtable;
+ 		tsr->tstate = trigdata->tg_newtable;
+ 		tsr->tupdesc = trigdata->tg_relation->rd_att;
+ 		tsr->relid = trigdata->tg_relation->rd_id;
+ 		SPI_register_tuplestore(tsr);
+ 	}
+ 	if (trigdata->tg_oldtable)
+ 	{
+ 		Tsr tsr = palloc(sizeof(TsrData));
+ 
+ 		tsr->name = trigdata->tg_trigger->tgoldtable;
+ 		tsr->tstate = trigdata->tg_oldtable;
+ 		tsr->tupdesc = trigdata->tg_relation->rd_att;
+ 		tsr->relid = trigdata->tg_relation->rd_id;
+ 		SPI_register_tuplestore(tsr);
+ 	}
+ 
+ 	/*
  	 * Assign the special tg_ variables
  	 */
  
*** a/src/pl/plpgsql/src/plpgsql.h
--- b/src/pl/plpgsql/src/plpgsql.h
***************
*** 666,677 **** typedef struct PLpgSQL_func_hashkey
  	/* be careful that pad bytes in this struct get zeroed! */
  
  	/*
! 	 * For a trigger function, the OID of the relation triggered on is part of
! 	 * the hash key --- we want to compile the trigger separately for each
! 	 * relation it is used with, in case the rowtype is different.  Zero if
! 	 * not called as a trigger.
  	 */
! 	Oid			trigrelOid;
  
  	/*
  	 * We must include the input collation as part of the hash key too,
--- 666,677 ----
  	/* be careful that pad bytes in this struct get zeroed! */
  
  	/*
! 	 * For a trigger function, the OID of the trigger is part of the hash key
! 	 * --- we want to compile the trigger function separately for each trigger
! 	 * it is used with, in case the rowtype or transition table names are
! 	 * different.  Zero if not called as a trigger.
  	 */
! 	Oid			trigOid;
  
  	/*
  	 * We must include the input collation as part of the hash key too,
