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

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

◆ get_per_buffer_data()

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

Definition at line 161 of file read_stream.c.

162 {
163  return (char *) stream->per_buffer_data +
164  stream->per_buffer_data_size * buffer_index;
165 }
void * per_buffer_data
Definition: read_stream.c:142
size_t per_buffer_data_size
Definition: read_stream.c:141

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

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

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

834 {
835  read_stream_reset(stream);
836  pfree(stream);
837 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void read_stream_reset(ReadStream *stream)
Definition: read_stream.c:807

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

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

Definition at line 189 of file read_stream.c.

190 {
191  BlockNumber blocknum;
192 
193  blocknum = stream->buffered_blocknum;
194  if (blocknum != InvalidBlockNumber)
196  else
197  blocknum = stream->callback(stream,
198  stream->callback_private_data,
199  per_buffer_data);
200 
201  return blocknum;
202 }
uint32 BlockNumber
Definition: block.h:31

References ReadStream::buffered_blocknum, ReadStream::callback, ReadStream::callback_private_data, and InvalidBlockNumber.

Referenced by read_stream_look_ahead(), and read_stream_next_buffer().

◆ read_stream_look_ahead()

static void read_stream_look_ahead ( ReadStream stream,
bool  suppress_advice 
)
static

Definition at line 308 of file read_stream.c.

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

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 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 }
#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
#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:308
int16 buffer_index
Definition: read_stream.c:103
Buffer * buffers
Definition: bufmgr.h:129
int16 oldest_buffer_index
Definition: read_stream.c:152
BlockNumber seq_blocknum
Definition: read_stream.c:134
int16 oldest_io_index
Definition: read_stream.c:146
int16 next_io_index
Definition: read_stream.c:147
bool fast_path
Definition: read_stream.c:149

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

808 {
809  Buffer buffer;
810 
811  /* Stop looking ahead. */
812  stream->distance = 0;
813 
814  /* Forget buffered block number and fast path state. */
816  stream->fast_path = false;
817 
818  /* Unpin anything that wasn't consumed. */
819  while ((buffer = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
820  ReleaseBuffer(buffer);
821 
822  Assert(stream->pinned_buffers == 0);
823  Assert(stream->ios_in_progress == 0);
824 
825  /* Start off assuming data is cached. */
826  stream->distance = 1;
827 }
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4906
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
Definition: read_stream.c:606

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

◆ read_stream_start_pending_read()

static void read_stream_start_pending_read ( ReadStream stream,
bool  suppress_advice 
)
static

Definition at line 218 of file read_stream.c.

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

210 {
211  /* We shouldn't ever unget more than one block. */
213  Assert(blocknum != InvalidBlockNumber);
214  stream->buffered_blocknum = blocknum;
215 }

References Assert, ReadStream::buffered_blocknum, and InvalidBlockNumber.

Referenced by read_stream_look_ahead().