PostgreSQL Source Code  git master
read_stream.h File Reference
#include "storage/bufmgr.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.

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 BlockNumber(* ReadStreamBlockNumberCB) (ReadStream *stream, void *callback_private_data, void *per_buffer_data)
 

Functions

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_private)
 
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 20 of file read_stream.h.

◆ READ_STREAM_FULL

#define READ_STREAM_FULL   0x04

Definition at line 42 of file read_stream.h.

◆ READ_STREAM_MAINTENANCE

#define READ_STREAM_MAINTENANCE   0x01

Definition at line 27 of file read_stream.h.

◆ READ_STREAM_SEQUENTIAL

#define READ_STREAM_SEQUENTIAL   0x02

Definition at line 35 of file read_stream.h.

Typedef Documentation

◆ 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 48 of file read_stream.h.

Function Documentation

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

417 {
418  ReadStream *stream;
419  size_t size;
420  int16 queue_size;
421  int max_ios;
422  int strategy_pin_limit;
423  uint32 max_pinned_buffers;
424  Oid tablespace_id;
425  SMgrRelation smgr;
426 
427  smgr = RelationGetSmgr(rel);
428 
429  /*
430  * Decide how many I/Os we will allow to run at the same time. That
431  * currently means advice to the kernel to tell it that we will soon read.
432  * This number also affects how far we look ahead for opportunities to
433  * start more I/Os.
434  */
435  tablespace_id = smgr->smgr_rlocator.locator.spcOid;
436  if (!OidIsValid(MyDatabaseId) ||
437  IsCatalogRelation(rel) ||
439  {
440  /*
441  * Avoid circularity while trying to look up tablespace settings or
442  * before spccache.c is ready.
443  */
444  max_ios = effective_io_concurrency;
445  }
446  else if (flags & READ_STREAM_MAINTENANCE)
447  max_ios = get_tablespace_maintenance_io_concurrency(tablespace_id);
448  else
449  max_ios = get_tablespace_io_concurrency(tablespace_id);
450 
451  /* Cap to INT16_MAX to avoid overflowing below */
452  max_ios = Min(max_ios, PG_INT16_MAX);
453 
454  /*
455  * Choose the maximum number of buffers we're prepared to pin. We try to
456  * pin fewer if we can, though. We clamp it to at least io_combine_limit
457  * so that we can have a chance to build up a full io_combine_limit sized
458  * read, even when max_ios is zero. Be careful not to allow int16 to
459  * overflow (even though that's not possible with the current GUC range
460  * limits), allowing also for the spare entry and the overflow space.
461  */
462  max_pinned_buffers = Max(max_ios * 4, io_combine_limit);
463  max_pinned_buffers = Min(max_pinned_buffers,
465 
466  /* Give the strategy a chance to limit the number of buffers we pin. */
467  strategy_pin_limit = GetAccessStrategyPinLimit(strategy);
468  max_pinned_buffers = Min(strategy_pin_limit, max_pinned_buffers);
469 
470  /* Don't allow this backend to pin more than its share of buffers. */
471  if (SmgrIsTemp(smgr))
472  LimitAdditionalLocalPins(&max_pinned_buffers);
473  else
474  LimitAdditionalPins(&max_pinned_buffers);
475  Assert(max_pinned_buffers > 0);
476 
477  /*
478  * We need one extra entry for buffers and per-buffer data, because users
479  * of per-buffer data have access to the object until the next call to
480  * read_stream_next_buffer(), so we need a gap between the head and tail
481  * of the queue so that we don't clobber it.
482  */
483  queue_size = max_pinned_buffers + 1;
484 
485  /*
486  * Allocate the object, the buffers, the ios and per_data_data space in
487  * one big chunk. Though we have queue_size buffers, we want to be able
488  * to assume that all the buffers for a single read are contiguous (i.e.
489  * don't wrap around halfway through), so we allow temporary overflows of
490  * up to the maximum possible read size by allocating an extra
491  * io_combine_limit - 1 elements.
492  */
493  size = offsetof(ReadStream, buffers);
494  size += sizeof(Buffer) * (queue_size + io_combine_limit - 1);
495  size += sizeof(InProgressIO) * Max(1, max_ios);
496  size += per_buffer_data_size * queue_size;
497  size += MAXIMUM_ALIGNOF * 2;
498  stream = (ReadStream *) palloc(size);
499  memset(stream, 0, offsetof(ReadStream, buffers));
500  stream->ios = (InProgressIO *)
501  MAXALIGN(&stream->buffers[queue_size + io_combine_limit - 1]);
502  if (per_buffer_data_size > 0)
503  stream->per_buffer_data = (void *)
504  MAXALIGN(&stream->ios[Max(1, max_ios)]);
505 
506 #ifdef USE_PREFETCH
507 
508  /*
509  * This system supports prefetching advice. We can use it as long as
510  * direct I/O isn't enabled, the caller hasn't promised sequential access
511  * (overriding our detection heuristics), and max_ios hasn't been set to
512  * zero.
513  */
514  if ((io_direct_flags & IO_DIRECT_DATA) == 0 &&
515  (flags & READ_STREAM_SEQUENTIAL) == 0 &&
516  max_ios > 0)
517  stream->advice_enabled = true;
518 #endif
519 
520  /*
521  * For now, max_ios = 0 is interpreted as max_ios = 1 with advice disabled
522  * above. If we had real asynchronous I/O we might need a slightly
523  * different definition.
524  */
525  if (max_ios == 0)
526  max_ios = 1;
527 
528  stream->max_ios = max_ios;
529  stream->per_buffer_data_size = per_buffer_data_size;
530  stream->max_pinned_buffers = max_pinned_buffers;
531  stream->queue_size = queue_size;
532  stream->callback = callback;
533  stream->callback_private_data = callback_private_data;
534 
535  /*
536  * Skip the initial ramp-up phase if the caller says we're going to be
537  * reading the whole relation. This way we start out assuming we'll be
538  * doing full io_combine_limit sized reads (behavior B).
539  */
540  if (flags & READ_STREAM_FULL)
541  stream->distance = Min(max_pinned_buffers, io_combine_limit);
542  else
543  stream->distance = 1;
544 
545  /*
546  * Since we always access the same relation, we can initialize parts of
547  * the ReadBuffersOperation objects and leave them that way, to avoid
548  * wasting CPU cycles writing to them for each read.
549  */
550  for (int i = 0; i < max_ios; ++i)
551  {
552  stream->ios[i].op.rel = rel;
553  stream->ios[i].op.smgr = RelationGetSmgr(rel);
554  stream->ios[i].op.smgr_persistence = 0;
555  stream->ios[i].op.forknum = forknum;
556  stream->ios[i].op.strategy = strategy;
557  }
558 
559  return stream;
560 }
int Buffer
Definition: buf.h:23
void LimitAdditionalPins(uint32 *additional_pins)
Definition: bufmgr.c:2058
int effective_io_concurrency
Definition: bufmgr.c:150
int io_combine_limit
Definition: bufmgr.c:164
unsigned int uint32
Definition: c.h:506
#define Min(x, y)
Definition: c.h:1004
signed short int16
Definition: c.h:493
#define MAXALIGN(LEN)
Definition: c.h:811
#define Max(x, y)
Definition: c.h:998
#define Assert(condition)
Definition: c.h:858
#define PG_INT16_MAX
Definition: c.h:586
#define OidIsValid(objectId)
Definition: c.h:775
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:103
bool IsCatalogRelationOid(Oid relid)
Definition: catalog.c:120
int io_direct_flags
Definition: fd.c:168
#define IO_DIRECT_DATA
Definition: fd.h:54
int GetAccessStrategyPinLimit(BufferAccessStrategy strategy)
Definition: freelist.c:647
Oid MyDatabaseId
Definition: globals.c:91
int i
Definition: isn.c:73
void LimitAdditionalLocalPins(uint32 *additional_pins)
Definition: localbuf.c:290
void * palloc(Size size)
Definition: mcxt.c:1316
unsigned int Oid
Definition: postgres_ext.h:31
struct InProgressIO InProgressIO
#define READ_STREAM_MAINTENANCE
Definition: read_stream.h:27
#define READ_STREAM_FULL
Definition: read_stream.h:42
#define READ_STREAM_SEQUENTIAL
Definition: read_stream.h:35
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:567
static pg_noinline void Size size
Definition: slab.c:607
#define SmgrIsTemp(smgr)
Definition: smgr.h:73
int get_tablespace_io_concurrency(Oid spcid)
Definition: spccache.c:215
int get_tablespace_maintenance_io_concurrency(Oid spcid)
Definition: spccache.c:229
ReadBuffersOperation op
Definition: read_stream.c:103
ForkNumber forknum
Definition: bufmgr.h:125
BufferAccessStrategy strategy
Definition: bufmgr.h:126
struct SMgrRelationData * smgr
Definition: bufmgr.h:123
int16 distance
Definition: read_stream.c:116
void * per_buffer_data
Definition: read_stream.c:144
int16 max_ios
Definition: read_stream.c:111
bool advice_enabled
Definition: read_stream.c:117
int16 max_pinned_buffers
Definition: read_stream.c:114
InProgressIO * ios
Definition: read_stream.c:147
int16 queue_size
Definition: read_stream.c:113
size_t per_buffer_data_size
Definition: read_stream.c:143
ReadStreamBlockNumberCB callback
Definition: read_stream.c:132
void * callback_private_data
Definition: read_stream.c:133
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: read_stream.c:156
RelFileLocator locator
RelFileNumber relNumber
RelFileLocatorBackend smgr_rlocator
Definition: smgr.h:37
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References ReadStream::advice_enabled, Assert, ReadStream::buffers, ReadStream::callback, callback(), ReadStream::callback_private_data, ReadStream::distance, effective_io_concurrency, ReadBuffersOperation::forknum, get_tablespace_io_concurrency(), get_tablespace_maintenance_io_concurrency(), GetAccessStrategyPinLimit(), i, io_combine_limit, IO_DIRECT_DATA, io_direct_flags, ReadStream::ios, IsCatalogRelation(), IsCatalogRelationOid(), LimitAdditionalLocalPins(), LimitAdditionalPins(), RelFileLocatorBackend::locator, Max, ReadStream::max_ios, ReadStream::max_pinned_buffers, MAXALIGN, Min, MyDatabaseId, OidIsValid, InProgressIO::op, palloc(), ReadStream::per_buffer_data, ReadStream::per_buffer_data_size, PG_INT16_MAX, ReadStream::queue_size, READ_STREAM_FULL, READ_STREAM_MAINTENANCE, READ_STREAM_SEQUENTIAL, ReadBuffersOperation::rel, RelationGetSmgr(), RelFileLocator::relNumber, size, ReadBuffersOperation::smgr, ReadBuffersOperation::smgr_persistence, SMgrRelationData::smgr_rlocator, SmgrIsTemp, RelFileLocator::spcOid, and ReadBuffersOperation::strategy.

Referenced by acquire_sample_rows(), heap_beginscan(), and pg_prewarm().

◆ read_stream_end()

void read_stream_end ( ReadStream stream)

Definition at line 802 of file read_stream.c.

803 {
804  read_stream_reset(stream);
805  pfree(stream);
806 }
void pfree(void *pointer)
Definition: mcxt.c:1520
void read_stream_reset(ReadStream *stream)
Definition: read_stream.c:775

References pfree(), and read_stream_reset().

Referenced by acquire_sample_rows(), heap_endscan(), and pg_prewarm().

◆ read_stream_next_buffer()

Buffer read_stream_next_buffer ( ReadStream stream,
void **  per_buffer_private 
)

Definition at line 572 of file read_stream.c.

573 {
574  Buffer buffer;
575  int16 oldest_buffer_index;
576 
577 #ifndef READ_STREAM_DISABLE_FAST_PATH
578 
579  /*
580  * A fast path for all-cached scans (behavior A). This is the same as the
581  * usual algorithm, but it is specialized for no I/O and no per-buffer
582  * data, so we can skip the queue management code, stay in the same buffer
583  * slot and use singular StartReadBuffer().
584  */
585  if (likely(stream->fast_path))
586  {
587  BlockNumber next_blocknum;
588 
589  /* Fast path assumptions. */
590  Assert(stream->ios_in_progress == 0);
591  Assert(stream->pinned_buffers == 1);
592  Assert(stream->distance == 1);
593  Assert(stream->pending_read_nblocks == 0);
594  Assert(stream->per_buffer_data_size == 0);
595 
596  /* We're going to return the buffer we pinned last time. */
597  oldest_buffer_index = stream->oldest_buffer_index;
598  Assert((oldest_buffer_index + 1) % stream->queue_size ==
599  stream->next_buffer_index);
600  buffer = stream->buffers[oldest_buffer_index];
601  Assert(buffer != InvalidBuffer);
602 
603  /* Choose the next block to pin. */
604  if (unlikely(stream->blocknums_next == stream->blocknums_count))
606  next_blocknum = stream->blocknums[stream->blocknums_next++];
607 
608  if (likely(next_blocknum != InvalidBlockNumber))
609  {
610  /*
611  * Pin a buffer for the next call. Same buffer entry, and
612  * arbitrary I/O entry (they're all free). We don't have to
613  * adjust pinned_buffers because we're transferring one to caller
614  * but pinning one more.
615  */
616  if (likely(!StartReadBuffer(&stream->ios[0].op,
617  &stream->buffers[oldest_buffer_index],
618  next_blocknum,
619  stream->advice_enabled ?
621  {
622  /* Fast return. */
623  return buffer;
624  }
625 
626  /* Next call must wait for I/O for the newly pinned buffer. */
627  stream->oldest_io_index = 0;
628  stream->next_io_index = stream->max_ios > 1 ? 1 : 0;
629  stream->ios_in_progress = 1;
630  stream->ios[0].buffer_index = oldest_buffer_index;
631  stream->seq_blocknum = next_blocknum + 1;
632  }
633  else
634  {
635  /* No more blocks, end of stream. */
636  stream->distance = 0;
637  stream->oldest_buffer_index = stream->next_buffer_index;
638  stream->pinned_buffers = 0;
639  }
640 
641  stream->fast_path = false;
642  return buffer;
643  }
644 #endif
645 
646  if (unlikely(stream->pinned_buffers == 0))
647  {
648  Assert(stream->oldest_buffer_index == stream->next_buffer_index);
649 
650  /* End of stream reached? */
651  if (stream->distance == 0)
652  return InvalidBuffer;
653 
654  /*
655  * The usual order of operations is that we look ahead at the bottom
656  * of this function after potentially finishing an I/O and making
657  * space for more, but if we're just starting up we'll need to crank
658  * the handle to get started.
659  */
660  read_stream_look_ahead(stream, true);
661 
662  /* End of stream reached? */
663  if (stream->pinned_buffers == 0)
664  {
665  Assert(stream->distance == 0);
666  return InvalidBuffer;
667  }
668  }
669 
670  /* Grab the oldest pinned buffer and associated per-buffer data. */
671  Assert(stream->pinned_buffers > 0);
672  oldest_buffer_index = stream->oldest_buffer_index;
673  Assert(oldest_buffer_index >= 0 &&
674  oldest_buffer_index < stream->queue_size);
675  buffer = stream->buffers[oldest_buffer_index];
676  if (per_buffer_data)
677  *per_buffer_data = get_per_buffer_data(stream, oldest_buffer_index);
678 
679  Assert(BufferIsValid(buffer));
680 
681  /* Do we have to wait for an associated I/O first? */
682  if (stream->ios_in_progress > 0 &&
683  stream->ios[stream->oldest_io_index].buffer_index == oldest_buffer_index)
684  {
685  int16 io_index = stream->oldest_io_index;
686  int16 distance;
687 
688  /* Sanity check that we still agree on the buffers. */
689  Assert(stream->ios[io_index].op.buffers ==
690  &stream->buffers[oldest_buffer_index]);
691 
692  WaitReadBuffers(&stream->ios[io_index].op);
693 
694  Assert(stream->ios_in_progress > 0);
695  stream->ios_in_progress--;
696  if (++stream->oldest_io_index == stream->max_ios)
697  stream->oldest_io_index = 0;
698 
699  if (stream->ios[io_index].op.flags & READ_BUFFERS_ISSUE_ADVICE)
700  {
701  /* Distance ramps up fast (behavior C). */
702  distance = stream->distance * 2;
703  distance = Min(distance, stream->max_pinned_buffers);
704  stream->distance = distance;
705  }
706  else
707  {
708  /* No advice; move towards io_combine_limit (behavior B). */
709  if (stream->distance > io_combine_limit)
710  {
711  stream->distance--;
712  }
713  else
714  {
715  distance = stream->distance * 2;
716  distance = Min(distance, io_combine_limit);
717  distance = Min(distance, stream->max_pinned_buffers);
718  stream->distance = distance;
719  }
720  }
721  }
722 
723 #ifdef CLOBBER_FREED_MEMORY
724  /* Clobber old buffer and per-buffer data for debugging purposes. */
725  stream->buffers[oldest_buffer_index] = InvalidBuffer;
726 
727  /*
728  * The caller will get access to the per-buffer data, until the next call.
729  * We wipe the one before, which is never occupied because queue_size
730  * allowed one extra element. This will hopefully trip up client code
731  * that is holding a dangling pointer to it.
732  */
733  if (stream->per_buffer_data)
734  wipe_mem(get_per_buffer_data(stream,
735  oldest_buffer_index == 0 ?
736  stream->queue_size - 1 :
737  oldest_buffer_index - 1),
738  stream->per_buffer_data_size);
739 #endif
740 
741  /* Pin transferred to caller. */
742  Assert(stream->pinned_buffers > 0);
743  stream->pinned_buffers--;
744 
745  /* Advance oldest buffer, with wrap-around. */
746  stream->oldest_buffer_index++;
747  if (stream->oldest_buffer_index == stream->queue_size)
748  stream->oldest_buffer_index = 0;
749 
750  /* Prepare for the next call. */
751  read_stream_look_ahead(stream, false);
752 
753 #ifndef READ_STREAM_DISABLE_FAST_PATH
754  /* See if we can take the fast path for all-cached scans next time. */
755  if (stream->ios_in_progress == 0 &&
756  stream->pinned_buffers == 1 &&
757  stream->distance == 1 &&
758  stream->pending_read_nblocks == 0 &&
759  stream->per_buffer_data_size == 0)
760  {
761  stream->fast_path = true;
762  }
763 #endif
764 
765  return buffer;
766 }
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#define InvalidBuffer
Definition: buf.h:25
void WaitReadBuffers(ReadBuffersOperation *operation)
Definition: bufmgr.c:1349
bool StartReadBuffer(ReadBuffersOperation *operation, Buffer *buffer, BlockNumber blocknum, int flags)
Definition: bufmgr.c:1321
#define READ_BUFFERS_ISSUE_ADVICE
Definition: bufmgr.h:113
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:355
#define likely(x)
Definition: c.h:310
#define unlikely(x)
Definition: c.h:311
static void * get_per_buffer_data(ReadStream *stream, int16 buffer_index)
Definition: read_stream.c:163
static void read_stream_look_ahead(ReadStream *stream, bool suppress_advice)
Definition: read_stream.c:323
static void read_stream_fill_blocknums(ReadStream *stream)
Definition: read_stream.c:214
int16 buffer_index
Definition: read_stream.c:102
Buffer * buffers
Definition: bufmgr.h:133
int16 blocknums_next
Definition: read_stream.c:126
int16 ios_in_progress
Definition: read_stream.c:112
int16 pinned_buffers
Definition: read_stream.c:115
int16 oldest_buffer_index
Definition: read_stream.c:154
BlockNumber seq_blocknum
Definition: read_stream.c:136
int16 oldest_io_index
Definition: read_stream.c:148
int16 blocknums_count
Definition: read_stream.c:125
BlockNumber blocknums[16]
Definition: read_stream.c:124
int16 next_buffer_index
Definition: read_stream.c:155
int16 next_io_index
Definition: read_stream.c:149
bool fast_path
Definition: read_stream.c:151
int16 pending_read_nblocks
Definition: read_stream.c:140

References ReadStream::advice_enabled, Assert, ReadStream::blocknums, ReadStream::blocknums_count, ReadStream::blocknums_next, 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_fill_blocknums(), read_stream_look_ahead(), ReadStream::seq_blocknum, StartReadBuffer(), unlikely, and WaitReadBuffers().

Referenced by heap_fetch_next_buffer(), heapam_scan_analyze_next_block(), pg_prewarm(), and read_stream_reset().

◆ read_stream_reset()

void read_stream_reset ( ReadStream stream)

Definition at line 775 of file read_stream.c.

776 {
777  Buffer buffer;
778 
779  /* Stop looking ahead. */
780  stream->distance = 0;
781 
782  /* Forget buffered block numbers and fast path state. */
783  stream->blocknums_next = 0;
784  stream->blocknums_count = 0;
785  stream->fast_path = false;
786 
787  /* Unpin anything that wasn't consumed. */
788  while ((buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
789  ReleaseBuffer(buffer);
790 
791  Assert(stream->pinned_buffers == 0);
792  Assert(stream->ios_in_progress == 0);
793 
794  /* Start off assuming data is cached. */
795  stream->distance = 1;
796 }
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4850
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
Definition: read_stream.c:572

References Assert, ReadStream::blocknums_count, ReadStream::blocknums_next, ReadStream::distance, ReadStream::fast_path, 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().