summaryrefslogtreecommitdiff
path: root/src/backend/commands/portalcmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-04-13 13:59:17 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2014-04-13 13:59:17 -0400
commite0c91a7ff015fab0ccbb0f75b6819f29ae00295e (patch)
tree77fefd1afb935e9fd742261f14d660eeeb21de1e /src/backend/commands/portalcmds.c
parent46a60abfe9fa13087dbbe15953c20df35f006968 (diff)
Improve some O(N^2) behavior in window function evaluation.
Repositioning the tuplestore seek pointer in window_gettupleslot() turns out to be a very significant expense when the window frame is sizable and the frame end can move. To fix, introduce a tuplestore function for skipping an arbitrary number of tuples in one call, parallel to the one we introduced for tuplesort objects in commit 8d65da1f. This reduces the cost of window_gettupleslot() to O(1) if the tuplestore has not spilled to disk. As in the previous commit, I didn't try to do any real optimization of tuplestore_skiptuples for the case where the tuplestore has spilled to disk. There is probably no practical way to get the cost to less than O(N) anyway, but perhaps someone can think of something later. Also fix PersistHoldablePortal() to make use of this API now that we have it. Based on a suggestion by Dean Rasheed, though this turns out not to look much like his patch.
Diffstat (limited to 'src/backend/commands/portalcmds.c')
-rw-r--r--src/backend/commands/portalcmds.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/src/backend/commands/portalcmds.c b/src/backend/commands/portalcmds.c
index 1f50993621f..e7c681ab7f4 100644
--- a/src/backend/commands/portalcmds.c
+++ b/src/backend/commands/portalcmds.c
@@ -389,26 +389,22 @@ PersistHoldablePortal(Portal portal)
FreeQueryDesc(queryDesc);
/*
- * Set the position in the result set: ideally, this could be
- * implemented by just skipping straight to the tuple # that we need
- * to be at, but the tuplestore API doesn't support that. So we start
- * at the beginning of the tuplestore and iterate through it until we
- * reach where we need to be. FIXME someday? (Fortunately, the
- * typical case is that we're supposed to be at or near the start of
- * the result set, so this isn't as bad as it sounds.)
+ * Set the position in the result set.
*/
MemoryContextSwitchTo(portal->holdContext);
if (portal->atEnd)
{
- /* we can handle this case even if posOverflow */
- while (tuplestore_advance(portal->holdStore, true))
+ /*
+ * We can handle this case even if posOverflow: just force the
+ * tuplestore forward to its end. The size of the skip request
+ * here is arbitrary.
+ */
+ while (tuplestore_skiptuples(portal->holdStore, 1000000, true))
/* continue */ ;
}
else
{
- long store_pos;
-
if (portal->posOverflow) /* oops, cannot trust portalPos */
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
@@ -416,11 +412,10 @@ PersistHoldablePortal(Portal portal)
tuplestore_rescan(portal->holdStore);
- for (store_pos = 0; store_pos < portal->portalPos; store_pos++)
- {
- if (!tuplestore_advance(portal->holdStore, true))
- elog(ERROR, "unexpected end of tuple stream");
- }
+ if (!tuplestore_skiptuples(portal->holdStore,
+ portal->portalPos,
+ true))
+ elog(ERROR, "unexpected end of tuple stream");
}
}
PG_CATCH();