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);
5151static Tuplestorestate * get_crosstab_tuplestore (char * sql ,
5252HTAB * crosstab_hash ,
5353TupleDesc tupdesc ,
54- MemoryContext per_query_ctx );
54+ MemoryContext per_query_ctx ,
55+ bool randomAccess );
5556static void validateConnectbyTupleDesc (TupleDesc tupdesc ,bool show_branch ,bool show_serial );
5657static bool compatCrosstabTupleDescs (TupleDesc tupdesc1 ,TupleDesc tupdesc2 );
5758static bool compatConnectbyTupleDescs (TupleDesc tupdesc1 ,TupleDesc tupdesc2 );
@@ -66,6 +67,7 @@ static Tuplestorestate *connectby(char *relname,
6667bool show_branch ,
6768bool show_serial ,
6869MemoryContext per_query_ctx ,
70+ bool randomAccess ,
6971AttInMetadata * attinmeta );
7072static Tuplestorestate * build_tuplestore_recursively (char * key_fld ,
7173char * parent_key_fld ,
@@ -745,7 +747,8 @@ crosstab_hash(PG_FUNCTION_ARGS)
745747rsinfo -> setResult = get_crosstab_tuplestore (sql ,
746748crosstab_hash ,
747749tupdesc ,
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 *
852855get_crosstab_tuplestore (char * sql ,
853856HTAB * crosstab_hash ,
854857TupleDesc tupdesc ,
855- MemoryContext per_query_ctx )
858+ MemoryContext per_query_ctx ,
859+ bool randomAccess )
856860{
857861Tuplestorestate * tupstore ;
858862int num_categories = hash_get_num_entries (crosstab_hash );
@@ -863,8 +867,8 @@ get_crosstab_tuplestore(char *sql,
863867int proc ;
864868MemoryContext SPIcontext ;
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 */
870874if ((ret = SPI_connect ())< 0 )
@@ -1113,6 +1117,7 @@ connectby_text(PG_FUNCTION_ARGS)
11131117show_branch ,
11141118show_serial ,
11151119per_query_ctx ,
1120+ rsinfo -> allowedModes & SFRM_Materialize_Random ,
11161121attinmeta );
11171122rsinfo -> setDesc = tupdesc ;
11181123
@@ -1192,6 +1197,7 @@ connectby_text_serial(PG_FUNCTION_ARGS)
11921197show_branch ,
11931198show_serial ,
11941199per_query_ctx ,
1200+ rsinfo -> allowedModes & SFRM_Materialize_Random ,
11951201attinmeta );
11961202rsinfo -> setDesc = tupdesc ;
11971203
@@ -1222,6 +1228,7 @@ connectby(char *relname,
12221228bool show_branch ,
12231229bool show_serial ,
12241230MemoryContext per_query_ctx ,
1231+ bool randomAccess ,
12251232AttInMetadata * attinmeta )
12261233{
12271234Tuplestorestate * tupstore = NULL ;
@@ -1239,7 +1246,7 @@ connectby(char *relname,
12391246oldcontext = 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
12441251MemoryContextSwitchTo (oldcontext );
12451252