Add SPI_push/SPI_pop calls so that datatype input and output functions called
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 30 Jan 2007 18:02:40 +0000 (18:02 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 30 Jan 2007 18:02:40 +0000 (18:02 +0000)
by plpgsql can themselves use SPI --- possibly indirectly, as in the case
of domain_in() invoking plpgsql functions in a domain check constraint.
Per bug #2945 from Sergiy Vyshnevetskiy.

Somewhat arbitrarily, I've chosen to back-patch this as far as 8.0.  Given
the lack of prior complaints, it doesn't seem critical for 7.x.

src/pl/plpgsql/src/pl_exec.c

index 13f1c17c8264362e4cdc8c8c92c02eeb776636b6..abeea96ef4d23f253d5f34fc38fd1897ac399a66 100644 (file)
@@ -3930,16 +3930,23 @@ make_tuple_from_row(PLpgSQL_execstate *estate,
 static char *
 convert_value_to_string(Datum value, Oid valtype)
 {
+       char       *str;
        Oid                     typoutput;
        Oid                     typioparam;
        bool            typIsVarlena;
 
        getTypeOutputInfo(valtype, &typoutput, &typioparam, &typIsVarlena);
 
-       return DatumGetCString(OidFunctionCall3(typoutput,
-                                                                                       value,
-                                                                                       ObjectIdGetDatum(typioparam),
-                                                                                       Int32GetDatum(-1)));
+       SPI_push();
+
+       str = DatumGetCString(OidFunctionCall3(typoutput,
+                                                                                  value,
+                                                                                  ObjectIdGetDatum(typioparam),
+                                                                                  Int32GetDatum(-1)));
+
+       SPI_pop();
+
+       return str;
 }
 
 /* ----------
@@ -3965,10 +3972,12 @@ exec_cast_value(Datum value, Oid valtype,
                        char       *extval;
 
                        extval = convert_value_to_string(value, valtype);
+                       SPI_push();
                        value = FunctionCall3(reqinput,
                                                                  CStringGetDatum(extval),
                                                                  ObjectIdGetDatum(reqtypioparam),
                                                                  Int32GetDatum(reqtypmod));
+                       SPI_pop();
                        pfree(extval);
                }
        }