1515#include <sys/stat.h>
1616#include <unistd.h>
1717
18+ #include "access/heapam.h"
1819#include "access/relation.h"
1920#include "fmgr.h"
2021#include "miscadmin.h"
@@ -35,6 +36,7 @@ typedef enum
3536 PREWARM_PREFETCH ,
3637 PREWARM_READ ,
3738 PREWARM_BUFFER ,
39+ PREWARM_TUPLE ,
3840} PrewarmType ;
3941
4042static PGIOAlignedBlock blockbuffer ;
@@ -106,6 +108,8 @@ pg_prewarm(PG_FUNCTION_ARGS)
106108 ptype = PREWARM_READ ;
107109 else if (strcmp (ttype , "buffer" ) == 0 )
108110 ptype = PREWARM_BUFFER ;
111+ else if (strcmp (ttype , "tuple" ) == 0 )
112+ ptype = PREWARM_TUPLE ;
109113 else
110114 {
111115 ereport (ERROR ,
@@ -233,6 +237,31 @@ pg_prewarm(PG_FUNCTION_ARGS)
233237 }
234238 Assert (read_stream_next_buffer (stream , NULL ) == InvalidBuffer );
235239 read_stream_end (stream );
240+ } else if (ptype == PREWARM_TUPLE )
241+ {
242+ if (get_relkind_objtype (rel -> rd_rel -> relkind ) == OBJECT_TABLE && forkNumber == MAIN_FORKNUM )
243+ {
244+ uint32 scan_flags = SO_TYPE_SEQSCAN | SO_TEMP_SNAPSHOT ;
245+ HeapTuple tuple ;
246+ Snapshot snapshot ;
247+ TableScanDesc scan ;
248+
249+ elog (LOG , "pg_prewarm: SeqScan relation \"%s\" starting %ld for %ld blocks" , RelationGetRelationName (rel ), first_block , last_block - first_block + 1 );
250+ // Use heap scan to set hint bits on every tuple. SO_ALLOW_PAGEMODE is intentionally NOT SET.
251+ // Otherwise, when a page is all visible, tuple hint bits won't be set.
252+ snapshot = RegisterSnapshot (GetTransactionSnapshot ());
253+ scan = heap_beginscan (rel , snapshot , 0 , NULL , NULL , scan_flags );
254+ heap_setscanlimits (scan , first_block , last_block - first_block + 1 );
255+ while ((tuple = heap_getnext (scan , ForwardScanDirection )) != NULL )
256+ {
257+ CHECK_FOR_INTERRUPTS ();
258+ }
259+ heap_endscan (scan );
260+ } else {
261+ ereport (INFO ,
262+ (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
263+ errmsg ("tuple prewarm is only supported for heap tables in main fork" )));
264+ }
236265 }
237266
238267 /* Close relation, release lock. */
0 commit comments