@@ -107,9 +107,10 @@ struct Tuplestorestate
107
107
bool backward ; /* store extra length words in file? */
108
108
bool interXact ; /* keep open through transactions? */
109
109
bool truncated ; /* tuplestore_trim has removed tuples? */
110
+ bool usedDisk ; /* used by tuplestore_get_stats() */
111
+ int64 maxSpace ; /* used by tuplestore_get_stats() */
110
112
int64 availMem ; /* remaining memory available, in bytes */
111
113
int64 allowedMem ; /* total memory allowed, in bytes */
112
- int64 maxSpace ; /* maximum space used in memory */
113
114
int64 tuples ; /* number of tuples added */
114
115
BufFile * myfile ; /* underlying file, or NULL if none */
115
116
MemoryContext context ; /* memory context for holding tuples */
@@ -262,9 +263,10 @@ tuplestore_begin_common(int eflags, bool interXact, int maxKBytes)
262
263
state -> eflags = eflags ;
263
264
state -> interXact = interXact ;
264
265
state -> truncated = false;
266
+ state -> usedDisk = false;
267
+ state -> maxSpace = 0 ;
265
268
state -> allowedMem = maxKBytes * 1024L ;
266
269
state -> availMem = state -> allowedMem ;
267
- state -> maxSpace = 0 ;
268
270
state -> myfile = NULL ;
269
271
270
272
/*
@@ -870,6 +872,14 @@ tuplestore_puttuple_common(Tuplestorestate *state, void *tuple)
870
872
* though callers might drop the requirement.
871
873
*/
872
874
state -> backward = (state -> eflags & EXEC_FLAG_BACKWARD ) != 0 ;
875
+
876
+ /*
877
+ * Update the maximum space used before dumping the tuples. It's
878
+ * possible that more space will be used by the tuples in memory
879
+ * than the space that will be used on disk.
880
+ */
881
+ tuplestore_updatemax (state );
882
+
873
883
state -> status = TSS_WRITEFILE ;
874
884
dumptuples (state );
875
885
break ;
@@ -1444,7 +1454,7 @@ tuplestore_trim(Tuplestorestate *state)
1444
1454
Assert (nremove >= state -> memtupdeleted );
1445
1455
Assert (nremove <= state -> memtupcount );
1446
1456
1447
- /* before freeing any memory, update maxSpace */
1457
+ /* before freeing any memory, update the statistics */
1448
1458
tuplestore_updatemax (state );
1449
1459
1450
1460
/* Release no-longer-needed tuples */
@@ -1491,45 +1501,46 @@ tuplestore_trim(Tuplestorestate *state)
1491
1501
1492
1502
/*
1493
1503
* tuplestore_updatemax
1494
- * Update maxSpace field
1504
+ * Update the maximum space used by this tuplestore and the method used
1505
+ * for storage.
1495
1506
*/
1496
1507
static void
1497
1508
tuplestore_updatemax (Tuplestorestate * state )
1498
1509
{
1499
1510
if (state -> status == TSS_INMEM )
1500
1511
state -> maxSpace = Max (state -> maxSpace ,
1501
1512
state -> allowedMem - state -> availMem );
1502
- }
1503
-
1504
- /*
1505
- * tuplestore_storage_type_name
1506
- * Return a string description of the storage method used to store the
1507
- * tuples.
1508
- */
1509
- const char *
1510
- tuplestore_storage_type_name (Tuplestorestate * state )
1511
- {
1512
- if (state -> status == TSS_INMEM )
1513
- return "Memory" ;
1514
1513
else
1515
- return "Disk" ;
1514
+ {
1515
+ state -> maxSpace = Max (state -> maxSpace ,
1516
+ BufFileSize (state -> myfile ));
1517
+
1518
+ /*
1519
+ * usedDisk never gets set to false again after spilling to disk, even
1520
+ * if tuplestore_clear() is called and new tuples go to memory again.
1521
+ */
1522
+ state -> usedDisk = true;
1523
+ }
1516
1524
}
1517
1525
1518
1526
/*
1519
- * tuplestore_space_used
1520
- * Return the maximum space used in memory unless the tuplestore has spilled
1521
- * to disk, in which case, return the disk space used.
1527
+ * tuplestore_get_stats
1528
+ * Obtain statistics about the maximum space used by the tuplestore.
1529
+ * These statistics are the maximums and are not reset by calls to
1530
+ * tuplestore_trim() or tuplestore_clear().
1522
1531
*/
1523
- int64
1524
- tuplestore_space_used (Tuplestorestate * state )
1532
+ void
1533
+ tuplestore_get_stats (Tuplestorestate * state , char * * max_storage_type ,
1534
+ int64 * max_space )
1525
1535
{
1526
- /* First, update the maxSpace field */
1527
1536
tuplestore_updatemax (state );
1528
1537
1529
- if (state -> status == TSS_INMEM )
1530
- return state -> maxSpace ;
1538
+ if (state -> usedDisk )
1539
+ * max_storage_type = "Disk" ;
1531
1540
else
1532
- return BufFileSize (state -> myfile );
1541
+ * max_storage_type = "Memory" ;
1542
+
1543
+ * max_space = state -> maxSpace ;
1533
1544
}
1534
1545
1535
1546
/*
@@ -1601,7 +1612,6 @@ writetup_heap(Tuplestorestate *state, void *tup)
1601
1612
if (state -> backward ) /* need trailing length word? */
1602
1613
BufFileWrite (state -> myfile , & tuplen , sizeof (tuplen ));
1603
1614
1604
- /* no need to call tuplestore_updatemax() when not in TSS_INMEM */
1605
1615
FREEMEM (state , GetMemoryChunkSpace (tuple ));
1606
1616
heap_free_minimal_tuple (tuple );
1607
1617
}
0 commit comments