Skip to content

Commit 8c79853

Browse files
committed
BEFORE/AFTER ROW INSERT triggers startup from CopyFrom()
RelationBuildTriggers() & FreeTriggerDesc() in trigger.c
1 parent c2efeaf commit 8c79853

File tree

2 files changed

+314
-27
lines changed

2 files changed

+314
-27
lines changed

src/backend/commands/copy.c

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.27 1997/08/22 14:22:09 vadim Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.28 1997/09/01 07:59:04 vadim Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -34,6 +34,7 @@
3434
#include <catalog/catname.h>
3535
#include <catalog/pg_user.h>
3636
#include <commands/copy.h>
37+
#include "commands/trigger.h"
3738
#include <storage/fd.h>
3839

3940
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
@@ -334,6 +335,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
334335
InsertIndexResult indexRes;
335336
TupleDesc tupDesc;
336337
Oid loaded_oid;
338+
bool skip_tuple = false;
337339

338340
tupDesc = RelationGetTupleDescriptor(rel);
339341
attr = tupDesc->attrs;
@@ -602,54 +604,83 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
602604
tuple = heap_formtuple(tupDesc, values, nulls);
603605
if (oids)
604606
tuple->t_oid = loaded_oid;
605-
606-
/* ----------------
607-
* Check the constraints of a tuple
608-
* ----------------
609-
*/
610-
611-
if ( rel->rd_att->constr )
607+
608+
skip_tuple = false;
609+
/* BEFORE ROW INSERT Triggers */
610+
if ( rel->trigdesc &&
611+
rel->trigdesc->n_before_row[TRIGGER_ACTION_INSERT] > 0 )
612612
{
613613
HeapTuple newtuple;
614614

615-
newtuple = ExecConstraints ("CopyFrom", rel, tuple);
616-
617-
if ( newtuple != tuple )
615+
newtuple = ExecBRInsertTriggers (rel, tuple);
616+
617+
if ( newtuple == NULL ) /* "do nothing" */
618+
skip_tuple = true;
619+
else if ( newtuple != tuple ) /* modified by Trigger(s) */
618620
{
619621
pfree (tuple);
620622
tuple = newtuple;
621623
}
622624
}
625+
626+
if ( !skip_tuple )
627+
{
628+
/* ----------------
629+
* Check the constraints of a tuple
630+
* ----------------
631+
*/
623632

624-
heap_insert(rel, tuple);
633+
if ( rel->rd_att->constr )
634+
{
635+
HeapTuple newtuple;
636+
637+
newtuple = ExecConstraints ("CopyFrom", rel, tuple);
638+
639+
if ( newtuple != tuple )
640+
{
641+
pfree (tuple);
642+
tuple = newtuple;
643+
}
644+
}
645+
646+
heap_insert(rel, tuple);
625647

626-
if (has_index) {
627-
for (i = 0; i < n_indices; i++) {
628-
if (indexPred[i] != NULL) {
648+
if (has_index)
649+
{
650+
for (i = 0; i < n_indices; i++)
651+
{
652+
if (indexPred[i] != NULL)
653+
{
629654
#ifndef OMIT_PARTIAL_INDEX
630-
/* if tuple doesn't satisfy predicate,
631-
* don't update index
632-
*/
633-
slot->val = tuple;
634-
/*SetSlotContents(slot, tuple); */
635-
if (ExecQual((List*)indexPred[i], econtext) == false)
636-
continue;
655+
/*
656+
* if tuple doesn't satisfy predicate,
657+
* don't update index
658+
*/
659+
slot->val = tuple;
660+
/*SetSlotContents(slot, tuple); */
661+
if (ExecQual((List*)indexPred[i], econtext) == false)
662+
continue;
637663
#endif /* OMIT_PARTIAL_INDEX */
638-
}
639-
FormIndexDatum(indexNatts[i],
664+
}
665+
FormIndexDatum(indexNatts[i],
640666
(AttrNumber *)&(pgIndexP[i]->indkey[0]),
641667
tuple,
642668
tupDesc,
643669
InvalidBuffer,
644670
idatum,
645671
index_nulls,
646672
finfoP[i]);
647-
indexRes = index_insert(index_rels[i], idatum, index_nulls,
673+
indexRes = index_insert(index_rels[i], idatum, index_nulls,
648674
&(tuple->t_ctid), rel);
649-
if (indexRes) pfree(indexRes);
675+
if (indexRes) pfree(indexRes);
676+
}
650677
}
678+
/* AFTER ROW INSERT Triggers */
679+
if ( rel->trigdesc &&
680+
rel->trigdesc->n_after_row[TRIGGER_ACTION_INSERT] > 0 )
681+
ExecARInsertTriggers (rel, tuple);
651682
}
652-
683+
653684
if (binary) pfree(string);
654685

655686
for (i = 0; i < attr_count; i++) {

0 commit comments

Comments
 (0)