Thread: Problems with insert rule called from plpython

Problems with insert rule called from plpython

From
Pavel Hanak
Date:
Hello,

I've noticed one problem by upgrading from postgresql-7.3.1 to 7.3.2.
The example of this problem looks like this:

   create table test (a int, b text);
   create view testview as select * from test;

   create or replace rule testview_ins as
   on insert to testview do instead
   (
   insert into test values (1, 'a');
   insert into test values (2, 'b');
   );

   create or replace function testfun() returns text as
   '
   plpy.execute("""insert into testview values (3, ''c'')""")
   return "test"
   ' language plpython;

Now calling "select testfun()" shows this fatal error:

   FATAL:  SPI: improper call to spi_dest_setup
   server closed the connection unexpectedly
           This probably means the server terminated abnormally
           before or while processing the request.
   The connection to the server was lost. Attempting reset: Succeeded.


This example worked in postgresql-7.3.1. In postgresql-7.3.2 works only
one command used in instead:

   create or replace rule testview_ins as
   on insert to testview do instead
   (
   insert into test values (1, 'a');
   );

Can you explain me this problem? Is it possible to reply also to my
email address, since I am not member of any postgresql mailing list.
Thanks.

--
Pavel Hanak

Re: Problems with insert rule called from plpython

From
Tom Lane
Date:
Pavel Hanak <hanak@brailcom.cz> writes:
> Now calling "select testfun()" shows this fatal error:
>    FATAL:  SPI: improper call to spi_dest_setup

Hm, I'm glad I put in that test --- it exposed a problem.  Here is
the patch for 7.3.

            regards, tom lane


*** src/backend/executor/spi.c.orig    Wed Jan 29 10:24:57 2003
--- src/backend/executor/spi.c    Fri Feb 14 16:09:38 2003
***************
*** 1097,1102 ****
--- 1097,1111 ----
              else
                  canSetResult = false;

+             /* Reset state if can set result */
+             if (canSetResult)
+             {
+                 SPI_processed = 0;
+                 SPI_lastoid = InvalidOid;
+                 SPI_tuptable = NULL;
+                 _SPI_current->tuptable = NULL;
+             }
+
              if (queryTree->commandType == CMD_UTILITY)
              {
                  if (IsA(queryTree->utilityStmt, CopyStmt))
***************
*** 1206,1211 ****
--- 1215,1229 ----
                  canSetResult = true;
              else
                  canSetResult = false;
+
+             /* Reset state if can set result */
+             if (canSetResult)
+             {
+                 SPI_processed = 0;
+                 SPI_lastoid = InvalidOid;
+                 SPI_tuptable = NULL;
+                 _SPI_current->tuptable = NULL;
+             }

              if (queryTree->commandType == CMD_UTILITY)
              {