Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit05bba3d

Browse files
committed
Be more tense about not creating tuplestores with randomAccess = true unless
backwards scan could actually happen. In particular, pass a flag tomaterialize-mode SRFs that tells them whether they need to require randomaccess. In passing, also suppress unneeded backward-scan overhead for aPortal's holdStore tuplestore. Per my proposal about reducing I/O costs fortuplestores.
1 parente3e3d2a commit05bba3d

File tree

11 files changed

+67
-31
lines changed

11 files changed

+67
-31
lines changed

‎contrib/tablefunc/tablefunc.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.54 2008/10/28 22:02:05 tgl Exp $
2+
* $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.55 2008/10/29 00:00:38 tgl Exp $
33
*
44
*
55
* tablefunc
@@ -51,7 +51,8 @@ static HTAB *load_categories_hash(char *cats_sql, MemoryContext per_query_ctx);
5151
staticTuplestorestate*get_crosstab_tuplestore(char*sql,
5252
HTAB*crosstab_hash,
5353
TupleDesctupdesc,
54-
MemoryContextper_query_ctx);
54+
MemoryContextper_query_ctx,
55+
boolrandomAccess);
5556
staticvoidvalidateConnectbyTupleDesc(TupleDesctupdesc,boolshow_branch,boolshow_serial);
5657
staticboolcompatCrosstabTupleDescs(TupleDesctupdesc1,TupleDesctupdesc2);
5758
staticboolcompatConnectbyTupleDescs(TupleDesctupdesc1,TupleDesctupdesc2);
@@ -66,6 +67,7 @@ static Tuplestorestate *connectby(char *relname,
6667
boolshow_branch,
6768
boolshow_serial,
6869
MemoryContextper_query_ctx,
70+
boolrandomAccess,
6971
AttInMetadata*attinmeta);
7072
staticTuplestorestate*build_tuplestore_recursively(char*key_fld,
7173
char*parent_key_fld,
@@ -745,7 +747,8 @@ crosstab_hash(PG_FUNCTION_ARGS)
745747
rsinfo->setResult=get_crosstab_tuplestore(sql,
746748
crosstab_hash,
747749
tupdesc,
748-
per_query_ctx);
750+
per_query_ctx,
751+
rsinfo->allowedModes&SFRM_Materialize_Random);
749752

750753
/*
751754
* SFRM_Materialize mode expects us to return a NULL Datum. The actual
@@ -852,7 +855,8 @@ static Tuplestorestate *
852855
get_crosstab_tuplestore(char*sql,
853856
HTAB*crosstab_hash,
854857
TupleDesctupdesc,
855-
MemoryContextper_query_ctx)
858+
MemoryContextper_query_ctx,
859+
boolrandomAccess)
856860
{
857861
Tuplestorestate*tupstore;
858862
intnum_categories=hash_get_num_entries(crosstab_hash);
@@ -863,8 +867,8 @@ get_crosstab_tuplestore(char *sql,
863867
intproc;
864868
MemoryContextSPIcontext;
865869

866-
/* initialize our tuplestore */
867-
tupstore=tuplestore_begin_heap(true, false,work_mem);
870+
/* initialize our tuplestore(while still in query context!)*/
871+
tupstore=tuplestore_begin_heap(randomAccess, false,work_mem);
868872

869873
/* Connect to SPI manager */
870874
if ((ret=SPI_connect())<0)
@@ -1113,6 +1117,7 @@ connectby_text(PG_FUNCTION_ARGS)
11131117
show_branch,
11141118
show_serial,
11151119
per_query_ctx,
1120+
rsinfo->allowedModes&SFRM_Materialize_Random,
11161121
attinmeta);
11171122
rsinfo->setDesc=tupdesc;
11181123

@@ -1192,6 +1197,7 @@ connectby_text_serial(PG_FUNCTION_ARGS)
11921197
show_branch,
11931198
show_serial,
11941199
per_query_ctx,
1200+
rsinfo->allowedModes&SFRM_Materialize_Random,
11951201
attinmeta);
11961202
rsinfo->setDesc=tupdesc;
11971203

@@ -1222,6 +1228,7 @@ connectby(char *relname,
12221228
boolshow_branch,
12231229
boolshow_serial,
12241230
MemoryContextper_query_ctx,
1231+
boolrandomAccess,
12251232
AttInMetadata*attinmeta)
12261233
{
12271234
Tuplestorestate*tupstore=NULL;
@@ -1239,7 +1246,7 @@ connectby(char *relname,
12391246
oldcontext=MemoryContextSwitchTo(per_query_ctx);
12401247

12411248
/* initialize our tuplestore */
1242-
tupstore=tuplestore_begin_heap(true, false,work_mem);
1249+
tupstore=tuplestore_begin_heap(randomAccess, false,work_mem);
12431250

12441251
MemoryContextSwitchTo(oldcontext);
12451252

‎contrib/xml2/xpath.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/xml2/xpath.c,v 1.20 2008/05/17 01:28:22 adunstan Exp $
2+
* $PostgreSQL: pgsql/contrib/xml2/xpath.c,v 1.21 2008/10/29 00:00:38 tgl Exp $
33
*
44
* Parser interface for DOM-based parser (libxml) rather than
55
stream-based SAX-type parser */
@@ -688,7 +688,9 @@ xpath_table(PG_FUNCTION_ARGS)
688688
* Create the tuplestore - work_mem is the max in-memory size before a
689689
* file is created on disk to hold it.
690690
*/
691-
tupstore=tuplestore_begin_heap(true, false,work_mem);
691+
tupstore=
692+
tuplestore_begin_heap(rsinfo->allowedModes&SFRM_Materialize_Random,
693+
false,work_mem);
692694

693695
MemoryContextSwitchTo(oldcontext);
694696

‎src/backend/commands/prepare.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.91 2008/08/28 23:09:45 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.92 2008/10/29 00:00:38 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -766,7 +766,9 @@ pg_prepared_statement(PG_FUNCTION_ARGS)
766766
* We put all the tuples into a tuplestore in one scan of the hashtable.
767767
* This avoids any issue of the hashtable possibly changing between calls.
768768
*/
769-
tupstore=tuplestore_begin_heap(true, false,work_mem);
769+
tupstore=
770+
tuplestore_begin_heap(rsinfo->allowedModes&SFRM_Materialize_Random,
771+
false,work_mem);
770772

771773
/* hash table might be uninitialized */
772774
if (prepared_queries)

‎src/backend/executor/execQual.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.234 2008/10/28 22:02:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.235 2008/10/29 00:00:38 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1429,6 +1429,7 @@ ExecMakeFunctionResult(FuncExprState *fcache,
14291429
rsinfo.econtext=econtext;
14301430
rsinfo.expectedDesc=fcache->funcResultDesc;
14311431
rsinfo.allowedModes= (int) (SFRM_ValuePerCall |SFRM_Materialize);
1432+
/* note we do not set SFRM_Materialize_Random */
14321433
rsinfo.returnMode=SFRM_ValuePerCall;
14331434
/* isDone is filled below */
14341435
rsinfo.setResult=NULL;
@@ -1702,7 +1703,8 @@ ExecMakeFunctionResultNoSets(FuncExprState *fcache,
17021703
Tuplestorestate*
17031704
ExecMakeTableFunctionResult(ExprState*funcexpr,
17041705
ExprContext*econtext,
1705-
TupleDescexpectedDesc)
1706+
TupleDescexpectedDesc,
1707+
boolrandomAccess)
17061708
{
17071709
Tuplestorestate*tupstore=NULL;
17081710
TupleDesctupdesc=NULL;
@@ -1736,6 +1738,8 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
17361738
rsinfo.econtext=econtext;
17371739
rsinfo.expectedDesc=expectedDesc;
17381740
rsinfo.allowedModes= (int) (SFRM_ValuePerCall |SFRM_Materialize);
1741+
if (randomAccess)
1742+
rsinfo.allowedModes |= (int)SFRM_Materialize_Random;
17391743
rsinfo.returnMode=SFRM_ValuePerCall;
17401744
/* isDone is filled below */
17411745
rsinfo.setResult=NULL;
@@ -1909,7 +1913,7 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
19091913
-1,
19101914
0);
19111915
}
1912-
tupstore=tuplestore_begin_heap(true, false,work_mem);
1916+
tupstore=tuplestore_begin_heap(randomAccess, false,work_mem);
19131917
MemoryContextSwitchTo(oldcontext);
19141918
rsinfo.setResult=tupstore;
19151919
rsinfo.setDesc=tupdesc;
@@ -1976,7 +1980,7 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
19761980
if (rsinfo.setResult==NULL)
19771981
{
19781982
MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1979-
tupstore=tuplestore_begin_heap(true, false,work_mem);
1983+
tupstore=tuplestore_begin_heap(randomAccess, false,work_mem);
19801984
rsinfo.setResult=tupstore;
19811985
if (!returnsSet)
19821986
{

‎src/backend/executor/nodeFunctionscan.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/nodeFunctionscan.c,v 1.48 2008/10/28 22:02:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/nodeFunctionscan.c,v 1.49 2008/10/29 00:00:38 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -64,7 +64,8 @@ FunctionNext(FunctionScanState *node)
6464
node->tuplestorestate=tuplestorestate=
6565
ExecMakeTableFunctionResult(node->funcexpr,
6666
node->ss.ps.ps_ExprContext,
67-
node->tupdesc);
67+
node->tupdesc,
68+
node->eflags&EXEC_FLAG_BACKWARD);
6869
}
6970

7071
/*
@@ -123,6 +124,7 @@ ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
123124
scanstate=makeNode(FunctionScanState);
124125
scanstate->ss.ps.plan= (Plan*)node;
125126
scanstate->ss.ps.state=estate;
127+
scanstate->eflags=eflags;
126128

127129
/*
128130
* Miscellaneous initialization

‎src/backend/utils/fmgr/README

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$PostgreSQL: pgsql/src/backend/utils/fmgr/README,v 1.14 2008/10/28 22:02:05 tgl Exp $
1+
$PostgreSQL: pgsql/src/backend/utils/fmgr/README,v 1.15 2008/10/29 00:00:38 tgl Exp $
22

33
Function Manager
44
================
@@ -432,6 +432,10 @@ function is called in). The function stores pointers to the Tuplestore and
432432
TupleDesc into ReturnSetInfo, sets returnMode to indicate materialize mode,
433433
and returns null. isDone is not used and should be left at ExprSingleResult.
434434

435+
The Tuplestore must be created with randomAccess = true if
436+
SFRM_Materialize_Random is set in allowedModes, but it can (and preferably
437+
should) be created with randomAccess = false if not.
438+
435439
If available, the expected tuple descriptor is passed in ReturnSetInfo;
436440
in other contexts the expectedDesc field will be NULL. The function need
437441
not pay attention to expectedDesc, but it may be useful in special cases.

‎src/backend/utils/mmgr/portalmem.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.111 2008/07/18 20:26:06 tgl Exp $
15+
* $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.112 2008/10/29 00:00:38 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -354,11 +354,17 @@ PortalCreateHoldStore(Portal portal)
354354
ALLOCSET_DEFAULT_INITSIZE,
355355
ALLOCSET_DEFAULT_MAXSIZE);
356356

357-
/* Create the tuple store, selecting cross-transaction temp files. */
357+
/*
358+
* Create the tuple store, selecting cross-transaction temp files, and
359+
* enabling random access only if cursor requires scrolling.
360+
*
361+
* XXX: Should maintenance_work_mem be used for the portal size?
362+
*/
358363
oldcxt=MemoryContextSwitchTo(portal->holdContext);
359364

360-
/* XXX: Should maintenance_work_mem be used for the portal size? */
361-
portal->holdStore=tuplestore_begin_heap(true, true,work_mem);
365+
portal->holdStore=
366+
tuplestore_begin_heap(portal->cursorOptions&CURSOR_OPT_SCROLL,
367+
true,work_mem);
362368

363369
MemoryContextSwitchTo(oldcxt);
364370
}
@@ -913,7 +919,9 @@ pg_cursor(PG_FUNCTION_ARGS)
913919
* We put all the tuples into a tuplestore in one scan of the hashtable.
914920
* This avoids any issue of the hashtable possibly changing between calls.
915921
*/
916-
tupstore=tuplestore_begin_heap(true, false,work_mem);
922+
tupstore=
923+
tuplestore_begin_heap(rsinfo->allowedModes&SFRM_Materialize_Random,
924+
false,work_mem);
917925

918926
hash_seq_init(&hash_seq,PortalHashTable);
919927
while ((hentry=hash_seq_search(&hash_seq))!=NULL)

‎src/include/executor/executor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/executor/executor.h,v 1.150 2008/10/28 22:02:05 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/executor/executor.h,v 1.151 2008/10/29 00:00:39 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -178,7 +178,8 @@ extern Datum GetAttributeByName(HeapTupleHeader tuple, const char *attname,
178178
bool*isNull);
179179
externTuplestorestate*ExecMakeTableFunctionResult(ExprState*funcexpr,
180180
ExprContext*econtext,
181-
TupleDescexpectedDesc);
181+
TupleDescexpectedDesc,
182+
boolrandomAccess);
182183
externDatumExecEvalExprSwitchContext(ExprState*expression,ExprContext*econtext,
183184
bool*isNull,ExprDoneCond*isDone);
184185
externExprState*ExecInitExpr(Expr*node,PlanState*parent);

‎src/include/nodes/execnodes.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.192 2008/10/28 22:02:05 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.193 2008/10/29 00:00:39 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -156,7 +156,8 @@ typedef enum
156156
typedefenum
157157
{
158158
SFRM_ValuePerCall=0x01,/* one value returned per call */
159-
SFRM_Materialize=0x02/* result set instantiated in Tuplestore */
159+
SFRM_Materialize=0x02,/* result set instantiated in Tuplestore */
160+
SFRM_Materialize_Random=0x04/* Tuplestore needs randomAccess */
160161
}SetFunctionReturnMode;
161162

162163
/*
@@ -1180,6 +1181,7 @@ typedef struct SubqueryScanState
11801181
*Function nodes are used to scan the results of a
11811182
*function appearing in FROM (typically a function returning set).
11821183
*
1184+
*eflagsnode's capability flags
11831185
*tupdescexpected return tuple description
11841186
*tuplestorestateprivate state of tuplestore.c
11851187
*funcexprstate for function expression being evaluated
@@ -1188,6 +1190,7 @@ typedef struct SubqueryScanState
11881190
typedefstructFunctionScanState
11891191
{
11901192
ScanStatess;/* its first field is NodeTag */
1193+
inteflags;
11911194
TupleDesctupdesc;
11921195
Tuplestorestate*tuplestorestate;
11931196
ExprState*funcexpr;

‎src/pl/plperl/plperl.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**********************************************************************
22
* plperl.c - perl as a procedural language for PostgreSQL
33
*
4-
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.140 2008/10/09 17:24:05 alvherre Exp $
4+
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.141 2008/10/29 00:00:39 tgl Exp $
55
*
66
**********************************************************************/
77

@@ -1922,7 +1922,8 @@ plperl_return_next(SV *sv)
19221922

19231923
current_call_data->ret_tdesc=CreateTupleDescCopy(tupdesc);
19241924
current_call_data->tuple_store=
1925-
tuplestore_begin_heap(true, false,work_mem);
1925+
tuplestore_begin_heap(rsi->allowedModes&SFRM_Materialize_Random,
1926+
false,work_mem);
19261927
if (prodesc->fn_retistuple)
19271928
{
19281929
current_call_data->attinmeta=

‎src/pl/plpgsql/src/pl_exec.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.221 2008/09/24 14:40:00 tgl Exp $
11+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.222 2008/10/29 00:00:39 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2357,7 +2357,9 @@ exec_init_tuple_store(PLpgSQL_execstate *estate)
23572357
estate->tuple_store_cxt=rsi->econtext->ecxt_per_query_memory;
23582358

23592359
oldcxt=MemoryContextSwitchTo(estate->tuple_store_cxt);
2360-
estate->tuple_store=tuplestore_begin_heap(true, false,work_mem);
2360+
estate->tuple_store=
2361+
tuplestore_begin_heap(rsi->allowedModes&SFRM_Materialize_Random,
2362+
false,work_mem);
23612363
MemoryContextSwitchTo(oldcxt);
23622364

23632365
estate->rettupdesc=rsi->expectedDesc;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp