Take fewer snapshots.
authorRobert Haas <rhaas@postgresql.org>
Wed, 21 Dec 2011 14:16:55 +0000 (09:16 -0500)
committerRobert Haas <rhaas@postgresql.org>
Wed, 21 Dec 2011 14:16:55 +0000 (09:16 -0500)
When a PORTAL_ONE_SELECT query is executed, we can opportunistically
reuse the parse/plan shot for the execution phase.  This cuts down the
number of snapshots per simple query from 2 to 1 for the simple
protocol, and 3 to 2 for the extended protocol.  Since we are only
reusing a snapshot taken early in the processing of the same protocol
message, the change shouldn't be user-visible, except that the remote
possibility of the planning and execution snapshots being different is
eliminated.

Note that this change does not make it safe to assume that the parse/plan
snapshot will certainly be reused; that will currently only happen if
PortalStart() decides to use the PORTAL_ONE_SELECT strategy.  It might
be worth trying to provide some stronger guarantees here in the future,
but for now we don't.

Patch by me; review by Dimitri Fontaine.

src/backend/commands/portalcmds.c
src/backend/commands/prepare.c
src/backend/executor/spi.c
src/backend/tcop/postgres.c
src/backend/tcop/pquery.c
src/include/tcop/pquery.h

index 89086aa371738620f049993a328525bc88209a65..497657ee73fe5e4fd291e9069ec559cd88fe741e 100644 (file)
@@ -121,7 +121,7 @@ PerformCursorOpen(PlannedStmt *stmt, ParamListInfo params,
    /*
     * Start execution, inserting parameters if any.
     */
-   PortalStart(portal, params, GetActiveSnapshot());
+   PortalStart(portal, params, true);
 
    Assert(portal->strategy == PORTAL_ONE_SELECT);
 
index a94921574fa40b0e2441a030ba4c90cb075e5426..dd0e837ebcc5f115212549f98ebd8dbbdb0ed627 100644 (file)
@@ -278,7 +278,7 @@ ExecuteQuery(ExecuteStmt *stmt, const char *queryString,
    /*
     * Run the portal to completion.
     */
-   PortalStart(portal, paramLI, GetActiveSnapshot());
+   PortalStart(portal, paramLI, true);
 
    (void) PortalRun(portal, FETCH_ALL, false, dest, dest, completionTag);
 
index 688279c716ef783d2d7e0d951e421bd3ca058362..7af3abf3f7ae1dc2ede6677578eb8a09ce5b928f 100644 (file)
@@ -1126,7 +1126,6 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
    CachedPlan *cplan;
    List       *stmt_list;
    char       *query_string;
-   Snapshot    snapshot;
    MemoryContext oldcontext;
    Portal      portal;
 
@@ -1269,15 +1268,6 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
        }
    }
 
-   /* Set up the snapshot to use. */
-   if (read_only)
-       snapshot = GetActiveSnapshot();
-   else
-   {
-       CommandCounterIncrement();
-       snapshot = GetTransactionSnapshot();
-   }
-
    /*
     * If the plan has parameters, copy them into the portal.  Note that this
     * must be done after revalidating the plan, because in dynamic parameter
@@ -1293,7 +1283,13 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
    /*
     * Start portal execution.
     */
-   PortalStart(portal, paramLI, snapshot);
+   if (read_only)
+       PortalStart(portal, paramLI, true);
+   else
+   {
+       CommandCounterIncrement();
+       PortalStart(portal, paramLI, false);
+   }
 
    Assert(portal->strategy != PORTAL_MULTI_QUERY);
 
index 5bb16e010cc9975e16409c4356894d95fa251f8f..f789a1e6276d175f4c14891cb943f14d6b539b80 100644 (file)
@@ -943,10 +943,6 @@ exec_simple_query(const char *query_string)
 
        plantree_list = pg_plan_queries(querytree_list, 0, NULL);
 
-       /* Done with the snapshot used for parsing/planning */
-       if (snapshot_set)
-           PopActiveSnapshot();
-
        /* If we got a cancel signal in analysis or planning, quit */
        CHECK_FOR_INTERRUPTS();
 
@@ -971,9 +967,19 @@ exec_simple_query(const char *query_string)
                          NULL);
 
        /*
-        * Start the portal.  No parameters here.
+        * Start the portal.
+        * 
+        * If we took a snapshot for parsing/planning, the portal may be
+        * able to reuse it for the execution phase.  Currently, this will only
+        * happen in PORTAL_ONE_SELECT mode.  But even if PortalStart doesn't
+        * end up being able to do this, keeping the parse/plan snapshot around
+        * until after we start the portal doesn't cost much.
         */
-       PortalStart(portal, NULL, InvalidSnapshot);
+       PortalStart(portal, NULL, snapshot_set);
+
+       /* Done with the snapshot used for parsing/planning */
+       if (snapshot_set)
+           PopActiveSnapshot();
 
        /*
         * Select the appropriate output format: text unless we are doing a
@@ -1696,14 +1702,18 @@ exec_bind_message(StringInfo input_message)
                      cplan->stmt_list,
                      cplan);
 
-   /* Done with the snapshot used for parameter I/O and parsing/planning */
-   if (snapshot_set)
-       PopActiveSnapshot();
-
    /*
     * And we're ready to start portal execution.
+    *
+    * If we took a snapshot for parsing/planning, we'll try to reuse it
+    * for query execution (currently, reuse will only occur if
+    * PORTAL_ONE_SELECT mode is chosen).
     */
-   PortalStart(portal, params, InvalidSnapshot);
+   PortalStart(portal, params, snapshot_set);
+
+   /* Done with the snapshot used for parameter I/O and parsing/planning */
+   if (snapshot_set)
+       PopActiveSnapshot();
 
    /*
     * Apply the result format requests to the portal.
index 466727b7b8279e0840cd22a39d8e003bcd87b7c6..75c7dee3cba2bc3ef8e445b9116688ea207cfe0a 100644 (file)
@@ -446,16 +446,17 @@ FetchStatementTargetList(Node *stmt)
  * the query, they must be passed in here (caller is responsible for
  * giving them appropriate lifetime).
  *
- * The caller can optionally pass a snapshot to be used; pass InvalidSnapshot
- * for the normal behavior of setting a new snapshot.  This parameter is
- * presently ignored for non-PORTAL_ONE_SELECT portals (it's only intended
- * to be used for cursors).
+ * The use_active_snapshot parameter is currently used only for
+ * PORTAL_ONE_SELECT portals.  If it is true, the active snapshot will
+ * be used when starting up the executor; if false, a new snapshot will
+ * be taken.  This is used both for cursors and to avoid taking an entirely
+ * new snapshot when it isn't necessary.
  *
  * On return, portal is ready to accept PortalRun() calls, and the result
  * tupdesc (if any) is known.
  */
 void
-PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
+PortalStart(Portal portal, ParamListInfo params, bool use_active_snapshot)
 {
    Portal      saveActivePortal;
    ResourceOwner saveResourceOwner;
@@ -497,8 +498,8 @@ PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
            case PORTAL_ONE_SELECT:
 
                /* Must set snapshot before starting executor. */
-               if (snapshot)
-                   PushActiveSnapshot(snapshot);
+               if (use_active_snapshot)
+                   PushActiveSnapshot(GetActiveSnapshot());
                else
                    PushActiveSnapshot(GetTransactionSnapshot());
 
index 2a5e871c92a0a4ab1a99508baa0104061d3dc7fe..ce37af3be10cb5d0ac4d1d54296ae4703e0b594d 100644 (file)
@@ -28,7 +28,7 @@ extern List *FetchPortalTargetList(Portal portal);
 extern List *FetchStatementTargetList(Node *stmt);
 
 extern void PortalStart(Portal portal, ParamListInfo params,
-           Snapshot snapshot);
+           bool use_active_snapshot);
 
 extern void PortalSetResultFormat(Portal portal, int nFormats,
                      int16 *formats);