PostgreSQL Source Code  git master
read_stream.c File Reference
#include "postgres.h"
#include "catalog/pg_tablespace.h"
#include "miscadmin.h"
#include "storage/fd.h"
#include "storage/smgr.h"
#include "storage/read_stream.h"
#include "utils/memdebug.h"
#include "utils/rel.h"
#include "utils/spccache.h"
Include dependency graph for read_stream.c:

Go to the source code of this file.

Data Structures

struct  InProgressIO
 
struct  ReadStream
 

Typedefs

typedef struct InProgressIO InProgressIO
 

Functions

static void * get_per_buffer_data (ReadStream *stream, int16 buffer_index)
 
static BlockNumber read_stream_get_block (ReadStream *stream, void *per_buffer_data)
 
static void read_stream_unget_block (ReadStream *stream, BlockNumber blocknum)
 
static void read_stream_fill_blocknums (ReadStream *stream)
 
static void read_stream_start_pending_read (ReadStream *stream, bool suppress_advice)
 
static void read_stream_look_ahead (ReadStream *stream, bool suppress_advice)
 
static ReadStreamread_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)
 
ReadStreamread_stream_begin_relation (int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
 
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)
 
Buffer read_stream_next_buffer (ReadStream *stream, void **per_buffer_data)
 
void read_stream_reset (ReadStream *stream)
 
void read_stream_end (ReadStream *stream)
 

Typedef Documentation

◆ InProgressIO

typedef struct InProgressIO InProgressIO

Function Documentation

◆ get_per_buffer_data()

static void* get_per_buffer_data ( ReadStream stream,
int16  buffer_index 
)
inlinestatic

Definition at line 163 of file read_stream.c.

164 {
165  return (char *) stream->per_buffer_data +
166  stream->per_buffer_data_size * buffer_index;
167 }
void * per_buffer_data
Definition: read_stream.c:144
size_t per_buffer_data_size
Definition: read_stream.c:143

References ReadStream::per_buffer_data, and ReadStream::per_buffer_data_size.

Referenced by read_stream_look_ahead(), and read_stream_next_buffer().

◆ read_stream_begin_impl()

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 
)
static

Definition at line 410 of file read_stream.c.

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

Referenced by read_stream_begin_relation(), and read_stream_begin_smgr_relation().

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

573 {
574  return read_stream_begin_impl(flags,
575  strategy,
576  rel,
577  RelationGetSmgr(rel),
578  rel->rd_rel->relpersistence,
579  forknum,
580  callback,
581  callback_private_data,
582  per_buffer_data_size);
583 }
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:410
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:567
Form_pg_class rd_rel
Definition: rel.h:111

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

Referenced by acquire_sample_rows(), 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 590 of file read_stream.c.

598 {
599  return read_stream_begin_impl(flags,
600  strategy,
601  NULL,
602  smgr,
603  smgr_persistence,
604  forknum,
605  callback,
606  callback_private_data,
607  per_buffer_data_size);
608 }

References callback(), and read_stream_begin_impl().

Referenced by RelationCopyStorageUsingBuffer().

◆ read_stream_end()

void read_stream_end ( ReadStream stream)

Definition at line 850 of file read_stream.c.

851 {
852  read_stream_reset(stream);
853  pfree(stream);
854 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void read_stream_reset(ReadStream *stream)
Definition: read_stream.c:823

References pfree(), and read_stream_reset().

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

◆ read_stream_fill_blocknums()

static void read_stream_fill_blocknums ( ReadStream stream)
static

Definition at line 214 of file read_stream.c.

215 {
216  BlockNumber blocknum;
217  int i = 0;
218 
219  do
220  {
221  blocknum = stream->callback(stream,
222  stream->callback_private_data,
223  NULL);
224  stream->blocknums[i++] = blocknum;
225  } while (i < lengthof(stream->blocknums) &&
226  blocknum != InvalidBlockNumber);
227  stream->blocknums_count = i;
228  stream->blocknums_next = 0;
229 }
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
#define lengthof(array)
Definition: c.h:788
int16 blocknums_next
Definition: read_stream.c:126
int16 blocknums_count
Definition: read_stream.c:125
BlockNumber blocknums[16]
Definition: read_stream.c:124

References ReadStream::blocknums, ReadStream::blocknums_count, ReadStream::blocknums_next, ReadStream::callback, ReadStream::callback_private_data, i, InvalidBlockNumber, and lengthof.

Referenced by read_stream_next_buffer().

◆ read_stream_get_block()

static BlockNumber read_stream_get_block ( ReadStream stream,
void *  per_buffer_data 
)
inlinestatic

Definition at line 175 of file read_stream.c.

176 {
177  if (stream->blocknums_next < stream->blocknums_count)
178  return stream->blocknums[stream->blocknums_next++];
179 
180  /*
181  * We only bother to fetch one at a time here (but see the fast path which
182  * uses more).
183  */
184  return stream->callback(stream,
185  stream->callback_private_data,
186  per_buffer_data);
187 }

References ReadStream::blocknums, ReadStream::blocknums_count, ReadStream::blocknums_next, ReadStream::callback, and ReadStream::callback_private_data.

Referenced by read_stream_look_ahead().

◆ read_stream_look_ahead()

static void read_stream_look_ahead ( ReadStream stream,
bool  suppress_advice 
)
static

Definition at line 323 of file read_stream.c.

324 {
325  while (stream->ios_in_progress < stream->max_ios &&
326  stream->pinned_buffers + stream->pending_read_nblocks < stream->distance)
327  {
328  BlockNumber blocknum;
329  int16 buffer_index;
330  void *per_buffer_data;
331 
332  if (stream->pending_read_nblocks == io_combine_limit)
333  {
334  read_stream_start_pending_read(stream, suppress_advice);
335  suppress_advice = false;
336  continue;
337  }
338 
339  /*
340  * See which block the callback wants next in the stream. We need to
341  * compute the index of the Nth block of the pending read including
342  * wrap-around, but we don't want to use the expensive % operator.
343  */
344  buffer_index = stream->next_buffer_index + stream->pending_read_nblocks;
345  if (buffer_index >= stream->queue_size)
346  buffer_index -= stream->queue_size;
347  Assert(buffer_index >= 0 && buffer_index < stream->queue_size);
348  per_buffer_data = get_per_buffer_data(stream, buffer_index);
349  blocknum = read_stream_get_block(stream, per_buffer_data);
350  if (blocknum == InvalidBlockNumber)
351  {
352  /* End of stream. */
353  stream->distance = 0;
354  break;
355  }
356 
357  /* Can we merge it with the pending read? */
358  if (stream->pending_read_nblocks > 0 &&
359  stream->pending_read_blocknum + stream->pending_read_nblocks == blocknum)
360  {
361  stream->pending_read_nblocks++;
362  continue;
363  }
364 
365  /* We have to start the pending read before we can build another. */
366  while (stream->pending_read_nblocks > 0)
367  {
368  read_stream_start_pending_read(stream, suppress_advice);
369  suppress_advice = false;
370  if (stream->ios_in_progress == stream->max_ios)
371  {
372  /* And we've hit the limit. Rewind, and stop here. */
373  read_stream_unget_block(stream, blocknum);
374  return;
375  }
376  }
377 
378  /* This is the start of a new pending read. */
379  stream->pending_read_blocknum = blocknum;
380  stream->pending_read_nblocks = 1;
381  }
382 
383  /*
384  * We don't start the pending read just because we've hit the distance
385  * limit, preferring to give it another chance to grow to full
386  * io_combine_limit size once more buffers have been consumed. However,
387  * if we've already reached io_combine_limit, or we've reached the
388  * distance limit and there isn't anything pinned yet, or the callback has
389  * signaled end-of-stream, we start the read immediately.
390  */
391  if (stream->pending_read_nblocks > 0 &&
393  (stream->pending_read_nblocks == stream->distance &&
394  stream->pinned_buffers == 0) ||
395  stream->distance == 0) &&
396  stream->ios_in_progress < stream->max_ios)
397  read_stream_start_pending_read(stream, suppress_advice);
398 }
static void * get_per_buffer_data(ReadStream *stream, int16 buffer_index)
Definition: read_stream.c:163
static BlockNumber read_stream_get_block(ReadStream *stream, void *per_buffer_data)
Definition: read_stream.c:175
static void read_stream_unget_block(ReadStream *stream, BlockNumber blocknum)
Definition: read_stream.c:194
static void read_stream_start_pending_read(ReadStream *stream, bool suppress_advice)
Definition: read_stream.c:233
int16 ios_in_progress
Definition: read_stream.c:112
int16 pinned_buffers
Definition: read_stream.c:115
BlockNumber pending_read_blocknum
Definition: read_stream.c:139
int16 next_buffer_index
Definition: read_stream.c:155
int16 pending_read_nblocks
Definition: read_stream.c:140

References Assert, ReadStream::distance, get_per_buffer_data(), InvalidBlockNumber, io_combine_limit, ReadStream::ios_in_progress, ReadStream::max_ios, ReadStream::next_buffer_index, ReadStream::pending_read_blocknum, ReadStream::pending_read_nblocks, ReadStream::pinned_buffers, ReadStream::queue_size, read_stream_get_block(), read_stream_start_pending_read(), and read_stream_unget_block().

Referenced by read_stream_next_buffer().

◆ read_stream_next_buffer()

Buffer read_stream_next_buffer ( ReadStream stream,
void **  per_buffer_data 
)

Definition at line 620 of file read_stream.c.

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

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(), read_stream_reset(), and RelationCopyStorageUsingBuffer().

◆ read_stream_reset()

void read_stream_reset ( ReadStream stream)

Definition at line 823 of file read_stream.c.

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

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().

◆ read_stream_start_pending_read()

static void read_stream_start_pending_read ( ReadStream stream,
bool  suppress_advice 
)
static

Definition at line 233 of file read_stream.c.

234 {
235  bool need_wait;
236  int nblocks;
237  int flags;
238  int16 io_index;
239  int16 overflow;
240  int16 buffer_index;
241 
242  /* This should only be called with a pending read. */
243  Assert(stream->pending_read_nblocks > 0);
245 
246  /* We had better not exceed the pin limit by starting this read. */
247  Assert(stream->pinned_buffers + stream->pending_read_nblocks <=
248  stream->max_pinned_buffers);
249 
250  /* We had better not be overwriting an existing pinned buffer. */
251  if (stream->pinned_buffers > 0)
252  Assert(stream->next_buffer_index != stream->oldest_buffer_index);
253  else
254  Assert(stream->next_buffer_index == stream->oldest_buffer_index);
255 
256  /*
257  * If advice hasn't been suppressed, this system supports it, and this
258  * isn't a strictly sequential pattern, then we'll issue advice.
259  */
260  if (!suppress_advice &&
261  stream->advice_enabled &&
262  stream->pending_read_blocknum != stream->seq_blocknum)
264  else
265  flags = 0;
266 
267  /* We say how many blocks we want to read, but may be smaller on return. */
268  buffer_index = stream->next_buffer_index;
269  io_index = stream->next_io_index;
270  nblocks = stream->pending_read_nblocks;
271  need_wait = StartReadBuffers(&stream->ios[io_index].op,
272  &stream->buffers[buffer_index],
273  stream->pending_read_blocknum,
274  &nblocks,
275  flags);
276  stream->pinned_buffers += nblocks;
277 
278  /* Remember whether we need to wait before returning this buffer. */
279  if (!need_wait)
280  {
281  /* Look-ahead distance decays, no I/O necessary (behavior A). */
282  if (stream->distance > 1)
283  stream->distance--;
284  }
285  else
286  {
287  /*
288  * Remember to call WaitReadBuffers() before returning head buffer.
289  * Look-ahead distance will be adjusted after waiting.
290  */
291  stream->ios[io_index].buffer_index = buffer_index;
292  if (++stream->next_io_index == stream->max_ios)
293  stream->next_io_index = 0;
294  Assert(stream->ios_in_progress < stream->max_ios);
295  stream->ios_in_progress++;
296  stream->seq_blocknum = stream->pending_read_blocknum + nblocks;
297  }
298 
299  /*
300  * We gave a contiguous range of buffer space to StartReadBuffers(), but
301  * we want it to wrap around at queue_size. Slide overflowing buffers to
302  * the front of the array.
303  */
304  overflow = (buffer_index + nblocks) - stream->queue_size;
305  if (overflow > 0)
306  memmove(&stream->buffers[0],
307  &stream->buffers[stream->queue_size],
308  sizeof(stream->buffers[0]) * overflow);
309 
310  /* Compute location of start of next read, without using % operator. */
311  buffer_index += nblocks;
312  if (buffer_index >= stream->queue_size)
313  buffer_index -= stream->queue_size;
314  Assert(buffer_index >= 0 && buffer_index < stream->queue_size);
315  stream->next_buffer_index = buffer_index;
316 
317  /* Adjust the pending read to cover the remaining portion, if any. */
318  stream->pending_read_blocknum += nblocks;
319  stream->pending_read_nblocks -= nblocks;
320 }
bool StartReadBuffers(ReadBuffersOperation *operation, Buffer *buffers, BlockNumber blockNum, int *nblocks, int flags)
Definition: bufmgr.c:1377
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

References ReadStream::advice_enabled, Assert, InProgressIO::buffer_index, ReadStream::buffers, ReadStream::distance, if(), io_combine_limit, ReadStream::ios, ReadStream::ios_in_progress, ReadStream::max_ios, ReadStream::max_pinned_buffers, ReadStream::next_buffer_index, ReadStream::next_io_index, ReadStream::oldest_buffer_index, InProgressIO::op, ReadStream::pending_read_blocknum, ReadStream::pending_read_nblocks, ReadStream::pinned_buffers, ReadStream::queue_size, READ_BUFFERS_ISSUE_ADVICE, ReadStream::seq_blocknum, and StartReadBuffers().

Referenced by read_stream_look_ahead().

◆ read_stream_unget_block()

static void read_stream_unget_block ( ReadStream stream,
BlockNumber  blocknum 
)
inlinestatic

Definition at line 194 of file read_stream.c.

195 {
196  if (stream->blocknums_next == stream->blocknums_count)
197  {
198  /* Never initialized or entirely consumed. Re-initialize. */
199  stream->blocknums[0] = blocknum;
200  stream->blocknums_count = 1;
201  stream->blocknums_next = 0;
202  }
203  else
204  {
205  /* Must be the last value return from blocknums array. */
206  Assert(stream->blocknums_next > 0);
207  stream->blocknums_next--;
208  Assert(stream->blocknums[stream->blocknums_next] == blocknum);
209  }
210 }

References Assert, ReadStream::blocknums, ReadStream::blocknums_count, and ReadStream::blocknums_next.

Referenced by read_stream_look_ahead().