Disabling triggers / constraints - Mailing list pgsql-patches

From Jorge Pereira
Subject Disabling triggers / constraints
Date
Msg-id 40ABA0DC.4030107@users.sf.net
Whole thread Raw
List pgsql-patches
After trying to do some custom dumping/restoring, and having to resort
to the awful trick of changing the trigger counts on the catalog (the
stuff pg_dump adds), decided to add a couple little variables to control
disabling constraints and triggers. Added them to guc.c so that they
show up in the SET/SHOW list, and added checks accordingly (always at
top level, to avoid unnecessary function calls and loops). Variables are
'disable_constraints' and 'disable_triggers' and they are "false" by
default.

I am unsure wether it is right to send such an uncalled-for patch, but
seeing as this is such a simple thing that can be quite useful (large
volumes of data), I thought it could be useful.

(apply with patch -p0 from the root distro directory)

Cheers
- Jorge Pereira
*** src/backend/utils/misc/guc.c.orig    2004-05-19 18:52:08.718580424 +0100
--- src/backend/utils/misc/guc.c    2004-05-19 18:50:46.162130912 +0100
***************
*** 131,136 ****
--- 131,140 ----
  int            log_min_duration_statement = -1;


+ /*  Control disabling of triggers (BS,AS,BR and AR) and constraints (useful on mass insert from dumps)*/
+ bool            disable_constraints;
+ bool            disable_triggers;
+

  /*
   * These variables are all dummies that don't do anything, except in some
*** src/backend/executor/execMain.c.orig    2004-05-19 18:53:14.251617888 +0100
--- src/backend/executor/execMain.c    2004-05-19 18:53:19.501819736 +0100
***************
*** 90,95 ****
--- 90,99 ----
                    evalPlanQual *priorepq);
  static void EvalPlanQualStop(evalPlanQual *epq);

+
+ extern bool disable_constraints;
+ extern bool disable_triggers;
+
  /* end of local decls */


***************
*** 1063,1068 ****
--- 1067,1073 ----
      /*
       * Process BEFORE EACH STATEMENT triggers
       */
+     if ( !disable_triggers )
      switch (operation)
      {
          case CMD_UPDATE:
***************
*** 1281,1286 ****
--- 1286,1292 ----
      /*
       * Process AFTER EACH STATEMENT triggers
       */
+     if ( !disable_triggers )
      switch (operation)
      {
          case CMD_UPDATE:
***************
*** 1379,1385 ****
      resultRelationDesc = resultRelInfo->ri_RelationDesc;

      /* BEFORE ROW INSERT Triggers */
!     if (resultRelInfo->ri_TrigDesc &&
        resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
      {
          HeapTuple    newtuple;
--- 1385,1391 ----
      resultRelationDesc = resultRelInfo->ri_RelationDesc;

      /* BEFORE ROW INSERT Triggers */
!     if ( !disable_triggers && resultRelInfo->ri_TrigDesc &&
        resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
      {
          HeapTuple    newtuple;
***************
*** 1405,1411 ****
      /*
       * Check the constraints of the tuple
       */
!     if (resultRelationDesc->rd_att->constr)
          ExecConstraints(resultRelInfo, slot, estate);

      /*
--- 1411,1417 ----
      /*
       * Check the constraints of the tuple
       */
!     if ( !disable_constraints && resultRelationDesc->rd_att->constr )
          ExecConstraints(resultRelInfo, slot, estate);

      /*
***************
*** 1431,1437 ****
          ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);

      /* AFTER ROW INSERT Triggers */
!     ExecARInsertTriggers(estate, resultRelInfo, tuple);
  }

  /* ----------------------------------------------------------------
--- 1437,1444 ----
          ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);

      /* AFTER ROW INSERT Triggers */
!     if ( !disable_triggers )
!         ExecARInsertTriggers(estate, resultRelInfo, tuple);
  }

  /* ----------------------------------------------------------------
***************
*** 1458,1464 ****
      resultRelationDesc = resultRelInfo->ri_RelationDesc;

      /* BEFORE ROW DELETE Triggers */
!     if (resultRelInfo->ri_TrigDesc &&
        resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
      {
          bool        dodelete;
--- 1465,1471 ----
      resultRelationDesc = resultRelInfo->ri_RelationDesc;

      /* BEFORE ROW DELETE Triggers */
!     if ( !disable_triggers && resultRelInfo->ri_TrigDesc &&
        resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
      {
          bool        dodelete;
***************
*** 1525,1531 ****
       */

      /* AFTER ROW DELETE Triggers */
!     ExecARDeleteTriggers(estate, resultRelInfo, tupleid);
  }

  /* ----------------------------------------------------------------
--- 1532,1539 ----
       */

      /* AFTER ROW DELETE Triggers */
!     if ( !disable_triggers )
!         ExecARDeleteTriggers(estate, resultRelInfo, tupleid);
  }

  /* ----------------------------------------------------------------
***************
*** 1569,1575 ****
      resultRelationDesc = resultRelInfo->ri_RelationDesc;

      /* BEFORE ROW UPDATE Triggers */
!     if (resultRelInfo->ri_TrigDesc &&
        resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
      {
          HeapTuple    newtuple;
--- 1577,1583 ----
      resultRelationDesc = resultRelInfo->ri_RelationDesc;

      /* BEFORE ROW UPDATE Triggers */
!     if ( !disable_triggers && resultRelInfo->ri_TrigDesc &&
        resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
      {
          HeapTuple    newtuple;
***************
*** 1604,1610 ****
       * there's no need to do them again.)
       */
  lreplace:;
!     if (resultRelationDesc->rd_att->constr)
          ExecConstraints(resultRelInfo, slot, estate);

      /*
--- 1612,1618 ----
       * there's no need to do them again.)
       */
  lreplace:;
!     if ( !disable_constraints && resultRelationDesc->rd_att->constr )
          ExecConstraints(resultRelInfo, slot, estate);

      /*
***************
*** 1678,1684 ****
          ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);

      /* AFTER ROW UPDATE Triggers */
!     ExecARUpdateTriggers(estate, resultRelInfo, tupleid, tuple);
  }

  static const char *
--- 1686,1693 ----
          ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);

      /* AFTER ROW UPDATE Triggers */
!     if ( !disable_triggers )
!         ExecARUpdateTriggers(estate, resultRelInfo, tupleid, tuple);
  }

  static const char *

pgsql-patches by date:

Previous
From: "Valentin Petkov"
Date:
Subject: About PostgreSQL
Next
From: Tom Lane
Date:
Subject: Re: Disabling triggers / constraints