PostgreSQL Source Code git master
read_stream.h File Reference
#include "storage/bufmgr.h"
#include "storage/smgr.h"
Include dependency graph for read_stream.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BlockRangeReadStreamPrivate
 

Macros

#define READ_STREAM_DEFAULT   0x00
 
#define READ_STREAM_MAINTENANCE   0x01
 
#define READ_STREAM_SEQUENTIAL   0x02
 
#define READ_STREAM_FULL   0x04
 
#define READ_STREAM_USE_BATCHING   0x08
 

Typedefs

typedef struct ReadStream ReadStream
 
typedef struct BlockRangeReadStreamPrivate BlockRangeReadStreamPrivate
 
typedef BlockNumber(* ReadStreamBlockNumberCB) (ReadStream *stream, void *callback_private_data, void *per_buffer_data)
 

Functions

BlockNumber block_range_read_stream_cb (ReadStream *stream, void *callback_private_data, void *per_buffer_data)
 
ReadStreamread_stream_begin_relation (int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
 
Buffer read_stream_next_buffer (ReadStream *stream, void **per_buffer_data)
 
BlockNumber read_stream_next_block (ReadStream *stream, BufferAccessStrategy *strategy)
 
ReadStreamread_stream_begin_smgr_relation (int flags, BufferAccessStrategy strategy, SMgrRelation smgr, char smgr_persistence, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
 
void read_stream_reset (ReadStream *stream)
 
void read_stream_end (ReadStream *stream)
 

Macro Definition Documentation

◆ READ_STREAM_DEFAULT

#define READ_STREAM_DEFAULT   0x00

Definition at line 21 of file read_stream.h.

◆ READ_STREAM_FULL

#define READ_STREAM_FULL   0x04

Definition at line 43 of file read_stream.h.

◆ READ_STREAM_MAINTENANCE

#define READ_STREAM_MAINTENANCE   0x01

Definition at line 28 of file read_stream.h.

◆ READ_STREAM_SEQUENTIAL

#define READ_STREAM_SEQUENTIAL   0x02

Definition at line 36 of file read_stream.h.

◆ READ_STREAM_USE_BATCHING

#define READ_STREAM_USE_BATCHING   0x08

Definition at line 64 of file read_stream.h.

Typedef Documentation

◆ BlockRangeReadStreamPrivate

◆ ReadStream

typedef struct ReadStream ReadStream

Definition at line 67 of file read_stream.h.

◆ ReadStreamBlockNumberCB

typedef BlockNumber(* ReadStreamBlockNumberCB) (ReadStream *stream, void *callback_private_data, void *per_buffer_data)

Definition at line 77 of file read_stream.h.

Function Documentation

◆ block_range_read_stream_cb()

BlockNumber block_range_read_stream_cb ( ReadStream stream,
void *  callback_private_data,
void *  per_buffer_data 
)

◆ read_stream_begin_relation()

ReadStream * read_stream_begin_relation ( int  flags,
BufferAccessStrategy  strategy,
Relation  rel,
ForkNumber  forknum,
ReadStreamBlockNumberCB  callback,
void *  callback_private_data,
size_t  per_buffer_data_size 
)

Definition at line 737 of file read_stream.c.

744{
745 return read_stream_begin_impl(flags,
746 strategy,
747 rel,
748 RelationGetSmgr(rel),
749 rel->rd_rel->relpersistence,
750 forknum,
751 callback,
752 callback_private_data,
753 per_buffer_data_size);
754}
static ReadStream * read_stream_begin_impl(int flags, BufferAccessStrategy strategy, Relation rel, SMgrRelation smgr, char persistence, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
Definition: read_stream.c:538
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:577
Form_pg_class rd_rel
Definition: rel.h:111
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References callback(), RelationData::rd_rel, read_stream_begin_impl(), and RelationGetSmgr().

Referenced by acquire_sample_rows(), autoprewarm_database_main(), brin_vacuum_scan(), btvacuumscan(), collect_corrupt_items(), collect_visibility_data(), gistvacuumscan(), heap_beginscan(), lazy_scan_heap(), lazy_vacuum_heap_rel(), pg_prewarm(), spgvacuumscan(), and verify_heapam().

◆ read_stream_begin_smgr_relation()

ReadStream * read_stream_begin_smgr_relation ( int  flags,
BufferAccessStrategy  strategy,
SMgrRelation  smgr,
char  smgr_persistence,
ForkNumber  forknum,
ReadStreamBlockNumberCB  callback,
void *  callback_private_data,
size_t  per_buffer_data_size 
)

Definition at line 761 of file read_stream.c.

769{
770 return read_stream_begin_impl(flags,
771 strategy,
772 NULL,
773 smgr,
774 smgr_persistence,
775 forknum,
776 callback,
777 callback_private_data,
778 per_buffer_data_size);
779}

References callback(), and read_stream_begin_impl().

Referenced by RelationCopyStorageUsingBuffer().

◆ read_stream_end()

void read_stream_end ( ReadStream stream)

◆ read_stream_next_block()

BlockNumber read_stream_next_block ( ReadStream stream,
BufferAccessStrategy strategy 
)

Definition at line 1031 of file read_stream.c.

1032{
1033 *strategy = stream->ios[0].op.strategy;
1034 return read_stream_get_block(stream, NULL);
1035}
static BlockNumber read_stream_get_block(ReadStream *stream, void *per_buffer_data)
Definition: read_stream.c:179
ReadBuffersOperation op
Definition: read_stream.c:86
BufferAccessStrategy strategy
Definition: bufmgr.h:138
InProgressIO * ios
Definition: read_stream.c:135

References ReadStream::ios, InProgressIO::op, read_stream_get_block(), and ReadBuffersOperation::strategy.

◆ read_stream_next_buffer()

Buffer read_stream_next_buffer ( ReadStream stream,
void **  per_buffer_data 
)

Definition at line 791 of file read_stream.c.

792{
793 Buffer buffer;
794 int16 oldest_buffer_index;
795
796#ifndef READ_STREAM_DISABLE_FAST_PATH
797
798 /*
799 * A fast path for all-cached scans. This is the same as the usual
800 * algorithm, but it is specialized for no I/O and no per-buffer data, so
801 * we can skip the queue management code, stay in the same buffer slot and
802 * use singular StartReadBuffer().
803 */
804 if (likely(stream->fast_path))
805 {
806 BlockNumber next_blocknum;
807
808 /* Fast path assumptions. */
809 Assert(stream->ios_in_progress == 0);
810 Assert(stream->forwarded_buffers == 0);
811 Assert(stream->pinned_buffers == 1);
812 Assert(stream->distance == 1);
813 Assert(stream->pending_read_nblocks == 0);
814 Assert(stream->per_buffer_data_size == 0);
816
817 /* We're going to return the buffer we pinned last time. */
818 oldest_buffer_index = stream->oldest_buffer_index;
819 Assert((oldest_buffer_index + 1) % stream->queue_size ==
820 stream->next_buffer_index);
821 buffer = stream->buffers[oldest_buffer_index];
822 Assert(buffer != InvalidBuffer);
823
824 /* Choose the next block to pin. */
825 next_blocknum = read_stream_get_block(stream, NULL);
826
827 if (likely(next_blocknum != InvalidBlockNumber))
828 {
829 int flags = stream->read_buffers_flags;
830
831 if (stream->advice_enabled)
833
834 /*
835 * Pin a buffer for the next call. Same buffer entry, and
836 * arbitrary I/O entry (they're all free). We don't have to
837 * adjust pinned_buffers because we're transferring one to caller
838 * but pinning one more.
839 *
840 * In the fast path we don't need to check the pin limit. We're
841 * always allowed at least one pin so that progress can be made,
842 * and that's all we need here. Although two pins are momentarily
843 * held at the same time, the model used here is that the stream
844 * holds only one, and the other now belongs to the caller.
845 */
846 if (likely(!StartReadBuffer(&stream->ios[0].op,
847 &stream->buffers[oldest_buffer_index],
848 next_blocknum,
849 flags)))
850 {
851 /* Fast return. */
852 return buffer;
853 }
854
855 /* Next call must wait for I/O for the newly pinned buffer. */
856 stream->oldest_io_index = 0;
857 stream->next_io_index = stream->max_ios > 1 ? 1 : 0;
858 stream->ios_in_progress = 1;
859 stream->ios[0].buffer_index = oldest_buffer_index;
860 stream->seq_blocknum = next_blocknum + 1;
861 }
862 else
863 {
864 /* No more blocks, end of stream. */
865 stream->distance = 0;
866 stream->oldest_buffer_index = stream->next_buffer_index;
867 stream->pinned_buffers = 0;
868 stream->buffers[oldest_buffer_index] = InvalidBuffer;
869 }
870
871 stream->fast_path = false;
872 return buffer;
873 }
874#endif
875
876 if (unlikely(stream->pinned_buffers == 0))
877 {
878 Assert(stream->oldest_buffer_index == stream->next_buffer_index);
879
880 /* End of stream reached? */
881 if (stream->distance == 0)
882 return InvalidBuffer;
883
884 /*
885 * The usual order of operations is that we look ahead at the bottom
886 * of this function after potentially finishing an I/O and making
887 * space for more, but if we're just starting up we'll need to crank
888 * the handle to get started.
889 */
891
892 /* End of stream reached? */
893 if (stream->pinned_buffers == 0)
894 {
895 Assert(stream->distance == 0);
896 return InvalidBuffer;
897 }
898 }
899
900 /* Grab the oldest pinned buffer and associated per-buffer data. */
901 Assert(stream->pinned_buffers > 0);
902 oldest_buffer_index = stream->oldest_buffer_index;
903 Assert(oldest_buffer_index >= 0 &&
904 oldest_buffer_index < stream->queue_size);
905 buffer = stream->buffers[oldest_buffer_index];
906 if (per_buffer_data)
907 *per_buffer_data = get_per_buffer_data(stream, oldest_buffer_index);
908
909 Assert(BufferIsValid(buffer));
910
911 /* Do we have to wait for an associated I/O first? */
912 if (stream->ios_in_progress > 0 &&
913 stream->ios[stream->oldest_io_index].buffer_index == oldest_buffer_index)
914 {
915 int16 io_index = stream->oldest_io_index;
916 int32 distance; /* wider temporary value, clamped below */
917
918 /* Sanity check that we still agree on the buffers. */
919 Assert(stream->ios[io_index].op.buffers ==
920 &stream->buffers[oldest_buffer_index]);
921
922 WaitReadBuffers(&stream->ios[io_index].op);
923
924 Assert(stream->ios_in_progress > 0);
925 stream->ios_in_progress--;
926 if (++stream->oldest_io_index == stream->max_ios)
927 stream->oldest_io_index = 0;
928
929 /* Look-ahead distance ramps up rapidly after we do I/O. */
930 distance = stream->distance * 2;
931 distance = Min(distance, stream->max_pinned_buffers);
932 stream->distance = distance;
933
934 /*
935 * If we've reached the first block of a sequential region we're
936 * issuing advice for, cancel that until the next jump. The kernel
937 * will see the sequential preadv() pattern starting here.
938 */
939 if (stream->advice_enabled &&
940 stream->ios[io_index].op.blocknum == stream->seq_until_processed)
942 }
943
944 /*
945 * We must zap this queue entry, or else it would appear as a forwarded
946 * buffer. If it's potentially in the overflow zone (ie from a
947 * multi-block I/O that wrapped around the queue), also zap the copy.
948 */
949 stream->buffers[oldest_buffer_index] = InvalidBuffer;
950 if (oldest_buffer_index < stream->io_combine_limit - 1)
951 stream->buffers[stream->queue_size + oldest_buffer_index] =
953
954#if defined(CLOBBER_FREED_MEMORY) || defined(USE_VALGRIND)
955
956 /*
957 * The caller will get access to the per-buffer data, until the next call.
958 * We wipe the one before, which is never occupied because queue_size
959 * allowed one extra element. This will hopefully trip up client code
960 * that is holding a dangling pointer to it.
961 */
962 if (stream->per_buffer_data)
963 {
964 void *per_buffer_data;
965
966 per_buffer_data = get_per_buffer_data(stream,
967 oldest_buffer_index == 0 ?
968 stream->queue_size - 1 :
969 oldest_buffer_index - 1);
970
971#if defined(CLOBBER_FREED_MEMORY)
972 /* This also tells Valgrind the memory is "noaccess". */
973 wipe_mem(per_buffer_data, stream->per_buffer_data_size);
974#elif defined(USE_VALGRIND)
975 /* Tell it ourselves. */
976 VALGRIND_MAKE_MEM_NOACCESS(per_buffer_data,
977 stream->per_buffer_data_size);
978#endif
979 }
980#endif
981
982 /* Pin transferred to caller. */
983 Assert(stream->pinned_buffers > 0);
984 stream->pinned_buffers--;
985
986 /* Advance oldest buffer, with wrap-around. */
987 stream->oldest_buffer_index++;
988 if (stream->oldest_buffer_index == stream->queue_size)
989 stream->oldest_buffer_index = 0;
990
991 /* Prepare for the next call. */
993
994#ifndef READ_STREAM_DISABLE_FAST_PATH
995 /* See if we can take the fast path for all-cached scans next time. */
996 if (stream->ios_in_progress == 0 &&
997 stream->forwarded_buffers == 0 &&
998 stream->pinned_buffers == 1 &&
999 stream->distance == 1 &&
1000 stream->pending_read_nblocks == 0 &&
1001 stream->per_buffer_data_size == 0)
1002 {
1003 /*
1004 * The fast path spins on one buffer entry repeatedly instead of
1005 * rotating through the whole queue and clearing the entries behind
1006 * it. If the buffer it starts with happened to be forwarded between
1007 * StartReadBuffers() calls and also wrapped around the circular queue
1008 * partway through, then a copy also exists in the overflow zone, and
1009 * it won't clear it out as the regular path would. Do that now, so
1010 * it doesn't need code for that.
1011 */
1012 if (stream->oldest_buffer_index < stream->io_combine_limit - 1)
1013 stream->buffers[stream->queue_size + stream->oldest_buffer_index] =
1015
1016 stream->fast_path = true;
1017 }
1018#endif
1019
1020 return buffer;
1021}
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
#define InvalidBuffer
Definition: buf.h:25
void WaitReadBuffers(ReadBuffersOperation *operation)
Definition: bufmgr.c:1613
bool StartReadBuffer(ReadBuffersOperation *operation, Buffer *buffer, BlockNumber blocknum, int flags)
Definition: bufmgr.c:1489
int io_combine_limit
Definition: bufmgr.c:170
#define READ_BUFFERS_ISSUE_ADVICE
Definition: bufmgr.h:124
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:387
#define Min(x, y)
Definition: c.h:1006
#define likely(x)
Definition: c.h:406
int16_t int16
Definition: c.h:536
int32_t int32
Definition: c.h:537
#define unlikely(x)
Definition: c.h:407
Assert(PointerIsAligned(start, uint64))
#define VALGRIND_MAKE_MEM_NOACCESS(addr, size)
Definition: memdebug.h:27
static void * get_per_buffer_data(ReadStream *stream, int16 buffer_index)
Definition: read_stream.c:151
static void read_stream_look_ahead(ReadStream *stream)
Definition: read_stream.c:429
int16 buffer_index
Definition: read_stream.c:85
Buffer * buffers
Definition: bufmgr.h:145
BlockNumber blocknum
Definition: bufmgr.h:146
int16 io_combine_limit
Definition: read_stream.c:95
int16 distance
Definition: read_stream.c:101
int16 ios_in_progress
Definition: read_stream.c:96
void * per_buffer_data
Definition: read_stream.c:132
BlockNumber seq_until_processed
Definition: read_stream.c:124
int16 pinned_buffers
Definition: read_stream.c:100
int16 max_ios
Definition: read_stream.c:94
int16 oldest_buffer_index
Definition: read_stream.c:142
BlockNumber seq_blocknum
Definition: read_stream.c:123
bool advice_enabled
Definition: read_stream.c:106
int16 max_pinned_buffers
Definition: read_stream.c:98
int16 oldest_io_index
Definition: read_stream.c:136
int read_buffers_flags
Definition: read_stream.c:103
int16 queue_size
Definition: read_stream.c:97
int16 next_buffer_index
Definition: read_stream.c:143
int16 initialized_buffers
Definition: read_stream.c:102
size_t per_buffer_data_size
Definition: read_stream.c:131
int16 forwarded_buffers
Definition: read_stream.c:99
int16 next_io_index
Definition: read_stream.c:137
bool fast_path
Definition: read_stream.c:139
int16 pending_read_nblocks
Definition: read_stream.c:128
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: read_stream.c:144

References ReadStream::advice_enabled, Assert(), ReadBuffersOperation::blocknum, InProgressIO::buffer_index, BufferIsValid(), ReadStream::buffers, ReadBuffersOperation::buffers, ReadStream::distance, ReadStream::fast_path, ReadStream::forwarded_buffers, get_per_buffer_data(), ReadStream::initialized_buffers, InvalidBlockNumber, InvalidBuffer, ReadStream::io_combine_limit, io_combine_limit, ReadStream::ios, ReadStream::ios_in_progress, likely, ReadStream::max_ios, ReadStream::max_pinned_buffers, Min, ReadStream::next_buffer_index, ReadStream::next_io_index, ReadStream::oldest_buffer_index, ReadStream::oldest_io_index, InProgressIO::op, ReadStream::pending_read_nblocks, ReadStream::per_buffer_data, ReadStream::per_buffer_data_size, ReadStream::pinned_buffers, ReadStream::queue_size, ReadStream::read_buffers_flags, READ_BUFFERS_ISSUE_ADVICE, read_stream_get_block(), read_stream_look_ahead(), ReadStream::seq_blocknum, ReadStream::seq_until_processed, StartReadBuffer(), unlikely, VALGRIND_MAKE_MEM_NOACCESS, and WaitReadBuffers().

Referenced by autoprewarm_database_main(), BitmapHeapScanNextBlock(), brin_vacuum_scan(), btvacuumscan(), collect_corrupt_items(), collect_visibility_data(), gistvacuumscan(), heap_fetch_next_buffer(), heapam_scan_analyze_next_block(), lazy_scan_heap(), lazy_vacuum_heap_rel(), pg_prewarm(), read_stream_reset(), RelationCopyStorageUsingBuffer(), spgvacuumscan(), and verify_heapam().

◆ read_stream_reset()

void read_stream_reset ( ReadStream stream)

Definition at line 1044 of file read_stream.c.

1045{
1046 int16 index;
1047 Buffer buffer;
1048
1049 /* Stop looking ahead. */
1050 stream->distance = 0;
1051
1052 /* Forget buffered block number and fast path state. */
1054 stream->fast_path = false;
1055
1056 /* Unpin anything that wasn't consumed. */
1057 while ((buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
1058 ReleaseBuffer(buffer);
1059
1060 /* Unpin any unused forwarded buffers. */
1061 index = stream->next_buffer_index;
1062 while (index < stream->initialized_buffers &&
1063 (buffer = stream->buffers[index]) != InvalidBuffer)
1064 {
1065 Assert(stream->forwarded_buffers > 0);
1066 stream->forwarded_buffers--;
1067 ReleaseBuffer(buffer);
1068
1069 stream->buffers[index] = InvalidBuffer;
1071 stream->buffers[stream->queue_size + index] = InvalidBuffer;
1072
1073 if (++index == stream->queue_size)
1074 index = 0;
1075 }
1076
1077 Assert(stream->forwarded_buffers == 0);
1078 Assert(stream->pinned_buffers == 0);
1079 Assert(stream->ios_in_progress == 0);
1080
1081 /* Start off assuming data is cached. */
1082 stream->distance = 1;
1083}
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5366
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
Definition: read_stream.c:791
BlockNumber buffered_blocknum
Definition: read_stream.c:113
Definition: type.h:96

References Assert(), ReadStream::buffered_blocknum, ReadStream::buffers, ReadStream::distance, ReadStream::fast_path, ReadStream::forwarded_buffers, InvalidBlockNumber, InvalidBuffer, io_combine_limit, ReadStream::ios_in_progress, ReadStream::next_buffer_index, ReadStream::pinned_buffers, ReadStream::queue_size, read_stream_next_buffer(), and ReleaseBuffer().

Referenced by btvacuumscan(), gistvacuumscan(), heap_fetch_next_buffer(), heap_rescan(), read_stream_end(), and spgvacuumscan().