]> granicus.if.org Git - postgresql/commitdiff
Make executor's SELECT INTO code save and restore original tuple receiver.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 4 Jan 2012 23:30:55 +0000 (18:30 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 4 Jan 2012 23:30:55 +0000 (18:30 -0500)
As previously coded, the QueryDesc's dest pointer was left dangling
(pointing at an already-freed receiver object) after ExecutorEnd.  It's a
bit astonishing that it took us this long to notice, and I'm not sure that
the known problem case with SQL functions is the only one.  Fix it by
saving and restoring the original receiver pointer, which seems the most
bulletproof way of ensuring any related bugs are also covered.

Per bug #6379 from Paul Ramsey.  Back-patch to 8.4 where the current
handling of SELECT INTO was introduced.

src/backend/executor/execMain.c
src/test/regress/expected/select_into.out
src/test/regress/sql/select_into.sql

index 54df18d3c799dfce33095c82ec7b5d42c0f279e9..569d0ba7ade1e0cd64c5566f2ecbbd38773f2b25 100644 (file)
@@ -2445,6 +2445,7 @@ typedef struct
 {
        DestReceiver pub;                       /* publicly-known function pointers */
        EState     *estate;                     /* EState we are working with */
+       DestReceiver *origdest;         /* QueryDesc's original receiver */
        Relation        rel;                    /* Relation to write to */
        int                     hi_options;             /* heap_insert performance options */
        BulkInsertState bistate;        /* bulk insert state */
@@ -2651,12 +2652,14 @@ OpenIntoRel(QueryDesc *queryDesc)
        /*
         * Now replace the query's DestReceiver with one for SELECT INTO
         */
-       queryDesc->dest = CreateDestReceiver(DestIntoRel);
-       myState = (DR_intorel *) queryDesc->dest;
+       myState = (DR_intorel *) CreateDestReceiver(DestIntoRel);
        Assert(myState->pub.mydest == DestIntoRel);
        myState->estate = estate;
+       myState->origdest = queryDesc->dest;
        myState->rel = intoRelationDesc;
 
+       queryDesc->dest = (DestReceiver *) myState;
+
        /*
         * We can skip WAL-logging the insertions, unless PITR or streaming
         * replication is in use. We can skip the FSM in any case.
@@ -2677,8 +2680,11 @@ CloseIntoRel(QueryDesc *queryDesc)
 {
        DR_intorel *myState = (DR_intorel *) queryDesc->dest;
 
-       /* OpenIntoRel might never have gotten called */
-       if (myState && myState->pub.mydest == DestIntoRel && myState->rel)
+       /*
+        * OpenIntoRel might never have gotten called, and we also want to guard
+        * against double destruction.
+        */
+       if (myState && myState->pub.mydest == DestIntoRel)
        {
                FreeBulkInsertState(myState->bistate);
 
@@ -2689,7 +2695,11 @@ CloseIntoRel(QueryDesc *queryDesc)
                /* close rel, but keep lock until commit */
                heap_close(myState->rel, NoLock);
 
-               myState->rel = NULL;
+               /* restore the receiver belonging to executor's caller */
+               queryDesc->dest = myState->origdest;
+
+               /* might as well invoke my destructor */
+               intorel_destroy((DestReceiver *) myState);
        }
 }
 
index 9ed4229b50600da4ed8cff51dd5303ff18fc2719..c8327f677ae34987de1ff21062162e4ed3ed1f5c 100644 (file)
@@ -50,3 +50,28 @@ DETAIL:  drop cascades to table selinto_schema.tmp1
 drop cascades to table selinto_schema.tmp2
 drop cascades to table selinto_schema.tmp3
 DROP USER selinto_user;
+--
+-- CREATE TABLE AS/SELECT INTO as last command in a SQL function
+-- have been known to cause problems
+--
+CREATE FUNCTION make_table() RETURNS VOID
+AS $$
+  CREATE TABLE created_table AS SELECT * FROM int8_tbl;
+$$ LANGUAGE SQL;
+SELECT make_table();
+ make_table 
+------------
+(1 row)
+
+SELECT * FROM created_table;
+        q1        |        q2         
+------------------+-------------------
+              123 |               456
+              123 |  4567890123456789
+ 4567890123456789 |               123
+ 4567890123456789 |  4567890123456789
+ 4567890123456789 | -4567890123456789
+(5 rows)
+
+DROP TABLE created_table;
index 039d35cd34cd7acb99289fe16c16f589cb712075..09d210bc6f98bbe998730a53f14d46a277b8d00b 100644 (file)
@@ -52,3 +52,18 @@ RESET SESSION AUTHORIZATION;
 
 DROP SCHEMA selinto_schema CASCADE;
 DROP USER selinto_user;
+
+--
+-- CREATE TABLE AS/SELECT INTO as last command in a SQL function
+-- have been known to cause problems
+--
+CREATE FUNCTION make_table() RETURNS VOID
+AS $$
+  CREATE TABLE created_table AS SELECT * FROM int8_tbl;
+$$ LANGUAGE SQL;
+
+SELECT make_table();
+
+SELECT * FROM created_table;
+
+DROP TABLE created_table;