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
 

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.

Typedef Documentation

◆ BlockRangeReadStreamPrivate

◆ ReadStream

typedef struct ReadStream ReadStream

Definition at line 1 of file read_stream.h.

◆ ReadStreamBlockNumberCB

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

Definition at line 56 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 
)

Definition at line 172 of file read_stream.c.

175 {
176  BlockRangeReadStreamPrivate *p = callback_private_data;
177 
178  if (p->current_blocknum < p->last_exclusive)
179  return p->current_blocknum++;
180 
181  return InvalidBlockNumber;
182 }
#define InvalidBlockNumber
Definition: block.h:33

References BlockRangeReadStreamPrivate::current_blocknum, InvalidBlockNumber, and BlockRangeReadStreamPrivate::last_exclusive.

Referenced by collect_visibility_data(), pg_prewarm(), and RelationCopyStorageUsingBuffer().

◆ 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 552 of file read_stream.c.

559 {
560  return read_stream_begin_impl(flags,
561  strategy,
562  rel,
563  RelationGetSmgr(rel),
564  rel->rd_rel->relpersistence,
565  forknum,
566  callback,
567  callback_private_data,
568  per_buffer_data_size);
569 }
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:395
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:567
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(), collect_corrupt_items(), collect_visibility_data(), heap_beginscan(), and pg_prewarm().

◆ 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 576 of file read_stream.c.

584 {
585  return read_stream_begin_impl(flags,
586  strategy,
587  NULL,
588  smgr,
589  smgr_persistence,
590  forknum,
591  callback,
592  callback_private_data,
593  per_buffer_data_size);
594 }

References callback(), and read_stream_begin_impl().

Referenced by RelationCopyStorageUsingBuffer().

◆ read_stream_end()

void read_stream_end ( ReadStream stream)

Definition at line 847 of file read_stream.c.

848 {
849  read_stream_reset(stream);
850  pfree(stream);
851 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void read_stream_reset(ReadStream *stream)
Definition: read_stream.c:821

References pfree(), and read_stream_reset().

Referenced by acquire_sample_rows(), collect_corrupt_items(), collect_visibility_data(), heap_endscan(), pg_prewarm(), and RelationCopyStorageUsingBuffer().

◆ read_stream_next_block()

BlockNumber read_stream_next_block ( ReadStream stream,
BufferAccessStrategy strategy 
)

Definition at line 808 of file read_stream.c.

809 {
810  *strategy = stream->ios[0].op.strategy;
811  return read_stream_get_block(stream, NULL);
812 }
static BlockNumber read_stream_get_block(ReadStream *stream, void *per_buffer_data)
Definition: read_stream.c:189
ReadBuffersOperation op
Definition: read_stream.c:104
BufferAccessStrategy strategy
Definition: bufmgr.h:122
InProgressIO * ios
Definition: read_stream.c:145

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 606 of file read_stream.c.

607 {
608  Buffer buffer;
609  int16 oldest_buffer_index;
610 
611 #ifndef READ_STREAM_DISABLE_FAST_PATH
612 
613  /*
614  * A fast path for all-cached scans (behavior A). This is the same as the
615  * usual algorithm, but it is specialized for no I/O and no per-buffer
616  * data, so we can skip the queue management code, stay in the same buffer
617  * slot and use singular StartReadBuffer().
618  */
619  if (likely(stream->fast_path))
620  {
621  BlockNumber next_blocknum;
622 
623  /* Fast path assumptions. */
624  Assert(stream->ios_in_progress == 0);
625  Assert(stream->pinned_buffers == 1);
626  Assert(stream->distance == 1);
627  Assert(stream->pending_read_nblocks == 0);
628  Assert(stream->per_buffer_data_size == 0);
629 
630  /* We're going to return the buffer we pinned last time. */
631  oldest_buffer_index = stream->oldest_buffer_index;
632  Assert((oldest_buffer_index + 1) % stream->queue_size ==
633  stream->next_buffer_index);
634  buffer = stream->buffers[oldest_buffer_index];
635  Assert(buffer != InvalidBuffer);
636 
637  /* Choose the next block to pin. */
638  next_blocknum = read_stream_get_block(stream, NULL);
639 
640  if (likely(next_blocknum != InvalidBlockNumber))
641  {
642  /*
643  * Pin a buffer for the next call. Same buffer entry, and
644  * arbitrary I/O entry (they're all free). We don't have to
645  * adjust pinned_buffers because we're transferring one to caller
646  * but pinning one more.
647  */
648  if (likely(!StartReadBuffer(&stream->ios[0].op,
649  &stream->buffers[oldest_buffer_index],
650  next_blocknum,
651  stream->advice_enabled ?
653  {
654  /* Fast return. */
655  return buffer;
656  }
657 
658  /* Next call must wait for I/O for the newly pinned buffer. */
659  stream->oldest_io_index = 0;
660  stream->next_io_index = stream->max_ios > 1 ? 1 : 0;
661  stream->ios_in_progress = 1;
662  stream->ios[0].buffer_index = oldest_buffer_index;
663  stream->seq_blocknum = next_blocknum + 1;
664  }
665  else
666  {
667  /* No more blocks, end of stream. */
668  stream->distance = 0;
669  stream->oldest_buffer_index = stream->next_buffer_index;
670  stream->pinned_buffers = 0;
671  }
672 
673  stream->fast_path = false;
674  return buffer;
675  }
676 #endif
677 
678  if (unlikely(stream->pinned_buffers == 0))
679  {
680  Assert(stream->oldest_buffer_index == stream->next_buffer_index);
681 
682  /* End of stream reached? */
683  if (stream->distance == 0)
684  return InvalidBuffer;
685 
686  /*
687  * The usual order of operations is that we look ahead at the bottom
688  * of this function after potentially finishing an I/O and making
689  * space for more, but if we're just starting up we'll need to crank
690  * the handle to get started.
691  */
692  read_stream_look_ahead(stream, true);
693 
694  /* End of stream reached? */
695  if (stream->pinned_buffers == 0)
696  {
697  Assert(stream->distance == 0);
698  return InvalidBuffer;
699  }
700  }
701 
702  /* Grab the oldest pinned buffer and associated per-buffer data. */
703  Assert(stream->pinned_buffers > 0);
704  oldest_buffer_index = stream->oldest_buffer_index;
705  Assert(oldest_buffer_index >= 0 &&
706  oldest_buffer_index < stream->queue_size);
707  buffer = stream->buffers[oldest_buffer_index];
708  if (per_buffer_data)
709  *per_buffer_data = get_per_buffer_data(stream, oldest_buffer_index);
710 
711  Assert(BufferIsValid(buffer));
712 
713  /* Do we have to wait for an associated I/O first? */
714  if (stream->ios_in_progress > 0 &&
715  stream->ios[stream->oldest_io_index].buffer_index == oldest_buffer_index)
716  {
717  int16 io_index = stream->oldest_io_index;
718  int16 distance;
719 
720  /* Sanity check that we still agree on the buffers. */
721  Assert(stream->ios[io_index].op.buffers ==
722  &stream->buffers[oldest_buffer_index]);
723 
724  WaitReadBuffers(&stream->ios[io_index].op);
725 
726  Assert(stream->ios_in_progress > 0);
727  stream->ios_in_progress--;
728  if (++stream->oldest_io_index == stream->max_ios)
729  stream->oldest_io_index = 0;
730 
731  if (stream->ios[io_index].op.flags & READ_BUFFERS_ISSUE_ADVICE)
732  {
733  /* Distance ramps up fast (behavior C). */
734  distance = stream->distance * 2;
735  distance = Min(distance, stream->max_pinned_buffers);
736  stream->distance = distance;
737  }
738  else
739  {
740  /* No advice; move towards io_combine_limit (behavior B). */
741  if (stream->distance > io_combine_limit)
742  {
743  stream->distance--;
744  }
745  else
746  {
747  distance = stream->distance * 2;
748  distance = Min(distance, io_combine_limit);
749  distance = Min(distance, stream->max_pinned_buffers);
750  stream->distance = distance;
751  }
752  }
753  }
754 
755 #ifdef CLOBBER_FREED_MEMORY
756  /* Clobber old buffer and per-buffer data for debugging purposes. */
757  stream->buffers[oldest_buffer_index] = InvalidBuffer;
758 
759  /*
760  * The caller will get access to the per-buffer data, until the next call.
761  * We wipe the one before, which is never occupied because queue_size
762  * allowed one extra element. This will hopefully trip up client code
763  * that is holding a dangling pointer to it.
764  */
765  if (stream->per_buffer_data)
766  wipe_mem(get_per_buffer_data(stream,
767  oldest_buffer_index == 0 ?
768  stream->queue_size - 1 :
769  oldest_buffer_index - 1),
770  stream->per_buffer_data_size);
771 #endif
772 
773  /* Pin transferred to caller. */
774  Assert(stream->pinned_buffers > 0);
775  stream->pinned_buffers--;
776 
777  /* Advance oldest buffer, with wrap-around. */
778  stream->oldest_buffer_index++;
779  if (stream->oldest_buffer_index == stream->queue_size)
780  stream->oldest_buffer_index = 0;
781 
782  /* Prepare for the next call. */
783  read_stream_look_ahead(stream, false);
784 
785 #ifndef READ_STREAM_DISABLE_FAST_PATH
786  /* See if we can take the fast path for all-cached scans next time. */
787  if (stream->ios_in_progress == 0 &&
788  stream->pinned_buffers == 1 &&
789  stream->distance == 1 &&
790  stream->pending_read_nblocks == 0 &&
791  stream->per_buffer_data_size == 0)
792  {
793  stream->fast_path = true;
794  }
795 #endif
796 
797  return buffer;
798 }
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:1392
bool StartReadBuffer(ReadBuffersOperation *operation, Buffer *buffer, BlockNumber blocknum, int flags)
Definition: bufmgr.c:1364
int io_combine_limit
Definition: bufmgr.c:165
#define READ_BUFFERS_ISSUE_ADVICE
Definition: bufmgr.h:113
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:351
#define Min(x, y)
Definition: c.h:1004
#define likely(x)
Definition: c.h:310
signed short int16
Definition: c.h:493
#define Assert(condition)
Definition: c.h:858
#define unlikely(x)
Definition: c.h:311
static void * get_per_buffer_data(ReadStream *stream, int16 buffer_index)
Definition: read_stream.c:161
static void read_stream_look_ahead(ReadStream *stream, bool suppress_advice)
Definition: read_stream.c:308
int16 buffer_index
Definition: read_stream.c:103
Buffer * buffers
Definition: bufmgr.h:129
int16 distance
Definition: read_stream.c:117
int16 ios_in_progress
Definition: read_stream.c:113
void * per_buffer_data
Definition: read_stream.c:142
int16 pinned_buffers
Definition: read_stream.c:116
int16 max_ios
Definition: read_stream.c:112
int16 oldest_buffer_index
Definition: read_stream.c:152
BlockNumber seq_blocknum
Definition: read_stream.c:134
bool advice_enabled
Definition: read_stream.c:118
int16 max_pinned_buffers
Definition: read_stream.c:115
int16 oldest_io_index
Definition: read_stream.c:146
int16 queue_size
Definition: read_stream.c:114
int16 next_buffer_index
Definition: read_stream.c:153
size_t per_buffer_data_size
Definition: read_stream.c:141
int16 next_io_index
Definition: read_stream.c:147
bool fast_path
Definition: read_stream.c:149
int16 pending_read_nblocks
Definition: read_stream.c:138
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: read_stream.c:154

References ReadStream::advice_enabled, Assert, InProgressIO::buffer_index, BufferIsValid(), ReadStream::buffers, ReadBuffersOperation::buffers, ReadStream::distance, ReadStream::fast_path, ReadBuffersOperation::flags, get_per_buffer_data(), InvalidBlockNumber, InvalidBuffer, 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, READ_BUFFERS_ISSUE_ADVICE, read_stream_get_block(), read_stream_look_ahead(), ReadStream::seq_blocknum, StartReadBuffer(), unlikely, and WaitReadBuffers().

Referenced by collect_corrupt_items(), collect_visibility_data(), heap_fetch_next_buffer(), heapam_scan_analyze_next_block(), pg_prewarm(), read_stream_reset(), and RelationCopyStorageUsingBuffer().

◆ read_stream_reset()

void read_stream_reset ( ReadStream stream)

Definition at line 821 of file read_stream.c.

822 {
823  Buffer buffer;
824 
825  /* Stop looking ahead. */
826  stream->distance = 0;
827 
828  /* Forget buffered block number and fast path state. */
830  stream->fast_path = false;
831 
832  /* Unpin anything that wasn't consumed. */
833  while ((buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
834  ReleaseBuffer(buffer);
835 
836  Assert(stream->pinned_buffers == 0);
837  Assert(stream->ios_in_progress == 0);
838 
839  /* Start off assuming data is cached. */
840  stream->distance = 1;
841 }
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4906
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
Definition: read_stream.c:606
BlockNumber buffered_blocknum
Definition: read_stream.c:124

References Assert, ReadStream::buffered_blocknum, ReadStream::distance, ReadStream::fast_path, InvalidBlockNumber, InvalidBuffer, ReadStream::ios_in_progress, ReadStream::pinned_buffers, read_stream_next_buffer(), and ReleaseBuffer().

Referenced by heap_fetch_next_buffer(), heap_rescan(), and read_stream_end().