aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/nodeSort.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2001-05-07 00:43:27 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2001-05-07 00:43:27 +0000
commitf905d65ee35b3f84b6d4433a5198af0e2e7bd090 (patch)
tree68f5955bb1a7ecaa531cf6b3752f563943dbe079 /src/backend/executor/nodeSort.c
parent9583aea9d09f6b3839ede8e57f990262b24e6979 (diff)
downloadpostgresql-f905d65ee35b3f84b6d4433a5198af0e2e7bd090.tar.gz
postgresql-f905d65ee35b3f84b6d4433a5198af0e2e7bd090.zip
Rewrite of planner statistics-gathering code. ANALYZE is now available as
a separate statement (though it can still be invoked as part of VACUUM, too). pg_statistic redesigned to be more flexible about what statistics are stored. ANALYZE now collects a list of several of the most common values, not just one, plus a histogram (not just the min and max values). Random sampling is used to make the process reasonably fast even on very large tables. The number of values and histogram bins collected is now user-settable via an ALTER TABLE command. There is more still to do; the new stats are not being used everywhere they could be in the planner. But the remaining changes for this project should be localized, and the behavior is already better than before. A not-very-related change is that sorting now makes use of btree comparison routines if it can find one, rather than invoking '<' twice.
Diffstat (limited to 'src/backend/executor/nodeSort.c')
-rw-r--r--src/backend/executor/nodeSort.c79
1 files changed, 35 insertions, 44 deletions
diff --git a/src/backend/executor/nodeSort.c b/src/backend/executor/nodeSort.c
index 12c6f82a8b2..e0543a28109 100644
--- a/src/backend/executor/nodeSort.c
+++ b/src/backend/executor/nodeSort.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/executor/nodeSort.c,v 1.32 2001/03/22 06:16:13 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/executor/nodeSort.c,v 1.33 2001/05/07 00:43:18 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -20,24 +20,24 @@
#include "utils/tuplesort.h"
/* ----------------------------------------------------------------
- * FormSortKeys(node)
+ * ExtractSortKeys
*
- * Forms the structure containing information used to sort the relation.
+ * Extract the sorting key information from the plan node.
*
- * Returns an array of ScanKeyData.
+ * Returns two palloc'd arrays, one of sort operator OIDs and
+ * one of attribute numbers.
* ----------------------------------------------------------------
*/
-static ScanKey
-FormSortKeys(Sort *sortnode)
+static void
+ExtractSortKeys(Sort *sortnode,
+ Oid **sortOperators,
+ AttrNumber **attNums)
{
- ScanKey sortkeys;
List *targetList;
- List *tl;
int keycount;
- Resdom *resdom;
- AttrNumber resno;
- Index reskey;
- Oid reskeyop;
+ Oid *sortOps;
+ AttrNumber *attNos;
+ List *tl;
/*
* get information from the node
@@ -46,36 +46,33 @@ FormSortKeys(Sort *sortnode)
keycount = sortnode->keycount;
/*
- * first allocate space for scan keys
+ * first allocate space for results
*/
if (keycount <= 0)
- elog(ERROR, "FormSortKeys: keycount <= 0");
- sortkeys = (ScanKey) palloc(keycount * sizeof(ScanKeyData));
- MemSet((char *) sortkeys, 0, keycount * sizeof(ScanKeyData));
+ elog(ERROR, "ExtractSortKeys: keycount <= 0");
+ sortOps = (Oid *) palloc(keycount * sizeof(Oid));
+ MemSet(sortOps, 0, keycount * sizeof(Oid));
+ *sortOperators = sortOps;
+ attNos = (AttrNumber *) palloc(keycount * sizeof(AttrNumber));
+ MemSet(attNos, 0, keycount * sizeof(AttrNumber));
+ *attNums = attNos;
/*
- * form each scan key from the resdom info in the target list
+ * extract info from the resdom nodes in the target list
*/
foreach(tl, targetList)
{
TargetEntry *target = (TargetEntry *) lfirst(tl);
-
- resdom = target->resdom;
- resno = resdom->resno;
- reskey = resdom->reskey;
- reskeyop = resdom->reskeyop;
+ Resdom *resdom = target->resdom;
+ Index reskey = resdom->reskey;
if (reskey > 0) /* ignore TLEs that are not sort keys */
{
- ScanKeyEntryInitialize(&sortkeys[reskey - 1],
- 0x0,
- resno,
- (RegProcedure) reskeyop,
- (Datum) 0);
+ Assert(reskey <= keycount);
+ sortOps[reskey - 1] = resdom->reskeyop;
+ attNos[reskey - 1] = resdom->resno;
}
}
-
- return sortkeys;
}
/* ----------------------------------------------------------------
@@ -124,8 +121,8 @@ ExecSort(Sort *node)
{
Plan *outerNode;
TupleDesc tupDesc;
- int keycount;
- ScanKey sortkeys;
+ Oid *sortOperators;
+ AttrNumber *attNums;
SO1_printf("ExecSort: %s\n",
"sorting subplan");
@@ -145,14 +142,17 @@ ExecSort(Sort *node)
outerNode = outerPlan((Plan *) node);
tupDesc = ExecGetTupType(outerNode);
- keycount = node->keycount;
- sortkeys = (ScanKey) sortstate->sort_Keys;
- tuplesortstate = tuplesort_begin_heap(tupDesc, keycount, sortkeys,
- true /* randomAccess */ );
+ ExtractSortKeys(node, &sortOperators, &attNums);
+ tuplesortstate = tuplesort_begin_heap(tupDesc, node->keycount,
+ sortOperators, attNums,
+ true /* randomAccess */ );
sortstate->tuplesortstate = (void *) tuplesortstate;
+ pfree(sortOperators);
+ pfree(attNums);
+
/*
* Scan the subplan and feed all the tuples to tuplesort.
*/
@@ -230,7 +230,6 @@ ExecInitSort(Sort *node, EState *estate, Plan *parent)
*/
sortstate = makeNode(SortState);
sortstate->sort_Done = false;
- sortstate->sort_Keys = NULL;
sortstate->tuplesortstate = NULL;
node->sortstate = sortstate;
@@ -259,11 +258,6 @@ ExecInitSort(Sort *node, EState *estate, Plan *parent)
ExecInitNode(outerPlan, estate, (Plan *) node);
/*
- * initialize sortstate information
- */
- sortstate->sort_Keys = FormSortKeys(node);
-
- /*
* initialize tuple type. no need to initialize projection info
* because this node doesn't do projections.
*/
@@ -321,9 +315,6 @@ ExecEndSort(Sort *node)
tuplesort_end((Tuplesortstate *) sortstate->tuplesortstate);
sortstate->tuplesortstate = NULL;
- if (sortstate->sort_Keys != NULL)
- pfree(sortstate->sort_Keys);
-
pfree(sortstate);
node->sortstate = NULL;