Re: [HACKERS] statement level trigger causes pltcl, plpython SIGSEGV - Mailing list pgsql-patches

From Joe Conway
Subject Re: [HACKERS] statement level trigger causes pltcl, plpython SIGSEGV
Date
Msg-id 3F2DF2E2.3030107@joeconway.com
Whole thread Raw
Responses Re: [HACKERS] statement level trigger causes pltcl, plpython SIGSEGV
List pgsql-patches
Joe Conway wrote:
> I'll try to submit a patch later tonight or tomorrow morning if no one
> beats me to it.
>

Here's a patch for pltcl. It works for my test case:

CREATE OR REPLACE FUNCTION footrigfunc() RETURNS trigger AS 'return OK'
LANGUAGE pltcl;
CREATE TRIGGER footrig BEFORE INSERT OR UPDATE OR DELETE ON foo FOR EACH
STATEMENT EXECUTE PROCEDURE footrigfunc();
insert into foo values(11,'cat99',1.89);
delete from foo where f0 = 11;


I also ran the "runtest" script in pl/tcl/test -- it said:
**** Running test queries ****
     Tests passed O.K.


Note that I also changed behavior in that when trigdata->tg_event
doesn't match anything known -- instead of pressing on with a value of
"UNKNOWN" it now throws an "elog(ERROR...". The previous behavior made
no sense to me, but you may not want to change existing behavior in this
way (even though it seems to me that it is a "should not happen" kind of
error).

If this patch is acceptable, I'll make similar changes to plpython. If
not, let me know what to change.

Thanks,

Joe
Index: src/pl/tcl/pltcl.c
===================================================================
RCS file: /opt/src/cvs/pgsql-server/src/pl/tcl/pltcl.c,v
retrieving revision 1.74
diff -c -r1.74 pltcl.c
*** src/pl/tcl/pltcl.c    4 Aug 2003 00:43:33 -0000    1.74
--- src/pl/tcl/pltcl.c    4 Aug 2003 05:36:12 -0000
***************
*** 708,770 ****
      else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
          Tcl_DStringAppendElement(&tcl_cmd, "AFTER");
      else
!         Tcl_DStringAppendElement(&tcl_cmd, "UNKNOWN");

      /* The level part of the event for TG_level */
      if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
          Tcl_DStringAppendElement(&tcl_cmd, "ROW");
      else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
          Tcl_DStringAppendElement(&tcl_cmd, "STATEMENT");
-     else
-         Tcl_DStringAppendElement(&tcl_cmd, "UNKNOWN");

!     /* Build the data list for the trigtuple */
!     pltcl_build_tuple_argument(trigdata->tg_trigtuple,
!                                tupdesc, &tcl_trigtup);
!
!     /*
!      * Now the command part of the event for TG_op and data for NEW and
!      * OLD
!      */
!     if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
!     {
!         Tcl_DStringAppendElement(&tcl_cmd, "INSERT");

-         Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
          Tcl_DStringAppendElement(&tcl_cmd, "");
-
-         rettup = trigdata->tg_trigtuple;
-     }
-     else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
-     {
-         Tcl_DStringAppendElement(&tcl_cmd, "DELETE");
-
          Tcl_DStringAppendElement(&tcl_cmd, "");
-         Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));

!         rettup = trigdata->tg_trigtuple;
!     }
!     else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
!     {
!         Tcl_DStringAppendElement(&tcl_cmd, "UPDATE");
!
!         pltcl_build_tuple_argument(trigdata->tg_newtuple,
!                                    tupdesc, &tcl_newtup);
!
!         Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_newtup));
!         Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
!
!         rettup = trigdata->tg_newtuple;
      }
      else
!     {
!         Tcl_DStringAppendElement(&tcl_cmd, "UNKNOWN");
!
!         Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
!         Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
!
!         rettup = trigdata->tg_trigtuple;
!     }

      memcpy(&Warn_restart, &save_restart, sizeof(Warn_restart));
      Tcl_DStringFree(&tcl_trigtup);
--- 708,785 ----
      else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
          Tcl_DStringAppendElement(&tcl_cmd, "AFTER");
      else
!         /* internal error */
!         elog(ERROR, "unrecognized WHEN tg_event");

      /* The level part of the event for TG_level */
      if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
+     {
          Tcl_DStringAppendElement(&tcl_cmd, "ROW");
+
+         /* Build the data list for the trigtuple */
+         pltcl_build_tuple_argument(trigdata->tg_trigtuple,
+                                    tupdesc, &tcl_trigtup);
+
+         /*
+          * Now the command part of the event for TG_op and data for NEW and
+          * OLD
+          */
+         if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
+         {
+             Tcl_DStringAppendElement(&tcl_cmd, "INSERT");
+
+             Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
+             Tcl_DStringAppendElement(&tcl_cmd, "");
+
+             rettup = trigdata->tg_trigtuple;
+         }
+         else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
+         {
+             Tcl_DStringAppendElement(&tcl_cmd, "DELETE");
+
+             Tcl_DStringAppendElement(&tcl_cmd, "");
+             Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
+
+             rettup = trigdata->tg_trigtuple;
+         }
+         else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
+         {
+             Tcl_DStringAppendElement(&tcl_cmd, "UPDATE");
+
+             pltcl_build_tuple_argument(trigdata->tg_newtuple,
+                                        tupdesc, &tcl_newtup);
+
+             Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_newtup));
+             Tcl_DStringAppendElement(&tcl_cmd, Tcl_DStringValue(&tcl_trigtup));
+
+             rettup = trigdata->tg_newtuple;
+         }
+         else
+             /* internal error */
+             elog(ERROR, "unrecognized OP tg_event");
+     }
      else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
+     {
          Tcl_DStringAppendElement(&tcl_cmd, "STATEMENT");

!         if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
!             Tcl_DStringAppendElement(&tcl_cmd, "INSERT");
!         else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
!             Tcl_DStringAppendElement(&tcl_cmd, "DELETE");
!         else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
!             Tcl_DStringAppendElement(&tcl_cmd, "UPDATE");
!         else
!             /* internal error */
!             elog(ERROR, "unrecognized OP tg_event");

          Tcl_DStringAppendElement(&tcl_cmd, "");
          Tcl_DStringAppendElement(&tcl_cmd, "");

!         rettup = (HeapTuple) NULL;
      }
      else
!         /* internal error */
!         elog(ERROR, "unrecognized LEVEL tg_event");

      memcpy(&Warn_restart, &save_restart, sizeof(Warn_restart));
      Tcl_DStringFree(&tcl_trigtup);

pgsql-patches by date:

Previous
From: Guillaume LELARGE
Date:
Subject: Updated french po files
Next
From: Peter Eisentraut
Date:
Subject: Re: [pgsql-rus] Russian NLS update: pg_dump, libpq,