PostgreSQL Source Code  git master
sharedtuplestore.c File Reference
#include "postgres.h"
#include "access/htup.h"
#include "access/htup_details.h"
#include "miscadmin.h"
#include "storage/buffile.h"
#include "storage/lwlock.h"
#include "storage/sharedfileset.h"
#include "utils/sharedtuplestore.h"
Include dependency graph for sharedtuplestore.c:

Go to the source code of this file.

Data Structures

struct  SharedTuplestoreChunk
 
struct  SharedTuplestoreParticipant
 
struct  SharedTuplestore
 
struct  SharedTuplestoreAccessor
 

Macros

#define STS_CHUNK_PAGES   4
 
#define STS_CHUNK_HEADER_SIZE   offsetof(SharedTuplestoreChunk, data)
 
#define STS_CHUNK_DATA_SIZE   (STS_CHUNK_PAGES * BLCKSZ - STS_CHUNK_HEADER_SIZE)
 

Typedefs

typedef struct SharedTuplestoreChunk SharedTuplestoreChunk
 
typedef struct SharedTuplestoreParticipant SharedTuplestoreParticipant
 

Functions

static void sts_filename (char *name, SharedTuplestoreAccessor *accessor, int participant)
 
size_t sts_estimate (int participants)
 
SharedTuplestoreAccessorsts_initialize (SharedTuplestore *sts, int participants, int my_participant_number, size_t meta_data_size, int flags, SharedFileSet *fileset, const char *name)
 
SharedTuplestoreAccessorsts_attach (SharedTuplestore *sts, int my_participant_number, SharedFileSet *fileset)
 
static void sts_flush_chunk (SharedTuplestoreAccessor *accessor)
 
void sts_end_write (SharedTuplestoreAccessor *accessor)
 
void sts_reinitialize (SharedTuplestoreAccessor *accessor)
 
void sts_begin_parallel_scan (SharedTuplestoreAccessor *accessor)
 
void sts_end_parallel_scan (SharedTuplestoreAccessor *accessor)
 
void sts_puttuple (SharedTuplestoreAccessor *accessor, void *meta_data, MinimalTuple tuple)
 
static MinimalTuple sts_read_tuple (SharedTuplestoreAccessor *accessor, void *meta_data)
 
MinimalTuple sts_parallel_scan_next (SharedTuplestoreAccessor *accessor, void *meta_data)
 

Macro Definition Documentation

◆ STS_CHUNK_DATA_SIZE

#define STS_CHUNK_DATA_SIZE   (STS_CHUNK_PAGES * BLCKSZ - STS_CHUNK_HEADER_SIZE)

Definition at line 40 of file sharedtuplestore.c.

Referenced by sts_initialize(), and sts_puttuple().

◆ STS_CHUNK_HEADER_SIZE

#define STS_CHUNK_HEADER_SIZE   offsetof(SharedTuplestoreChunk, data)

Definition at line 39 of file sharedtuplestore.c.

Referenced by sts_parallel_scan_next(), and sts_read_tuple().

◆ STS_CHUNK_PAGES

#define STS_CHUNK_PAGES   4

Typedef Documentation

◆ SharedTuplestoreChunk

◆ SharedTuplestoreParticipant

Function Documentation

◆ sts_attach()

SharedTuplestoreAccessor* sts_attach ( SharedTuplestore sts,
int  my_participant_number,
SharedFileSet fileset 
)

Definition at line 178 of file sharedtuplestore.c.

References Assert, SharedTuplestoreAccessor::context, CurrentMemoryContext, SharedTuplestoreAccessor::fileset, palloc0(), SharedTuplestoreAccessor::participant, and SharedTuplestoreAccessor::sts.

Referenced by ExecParallelHashEnsureBatchAccessors(), and ExecParallelHashRepartitionRest().

181 {
182  SharedTuplestoreAccessor *accessor;
183 
184  Assert(my_participant_number < sts->nparticipants);
185 
186  accessor = palloc0(sizeof(SharedTuplestoreAccessor));
187  accessor->participant = my_participant_number;
188  accessor->sts = sts;
189  accessor->fileset = fileset;
190  accessor->context = CurrentMemoryContext;
191 
192  return accessor;
193 }
SharedTuplestore * sts
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Assert(condition)
Definition: c.h:804

◆ sts_begin_parallel_scan()

void sts_begin_parallel_scan ( SharedTuplestoreAccessor accessor)

Definition at line 253 of file sharedtuplestore.c.

References Assert, i, SharedTuplestore::nparticipants, SharedTuplestoreAccessor::participant, SharedTuplestore::participants, PG_USED_FOR_ASSERTS_ONLY, SharedTuplestoreAccessor::read_file, SharedTuplestoreAccessor::read_next_page, SharedTuplestoreAccessor::read_participant, SharedTuplestoreAccessor::sts, sts_end_parallel_scan(), and SharedTuplestoreParticipant::writing.

Referenced by ExecParallelHashJoinNewBatch(), and ExecParallelHashRepartitionRest().

254 {
256 
257  /* End any existing scan that was in progress. */
258  sts_end_parallel_scan(accessor);
259 
260  /*
261  * Any backend that might have written into this shared tuplestore must
262  * have called sts_end_write(), so that all buffers are flushed and the
263  * files have stopped growing.
264  */
265  for (i = 0; i < accessor->sts->nparticipants; ++i)
266  Assert(!accessor->sts->participants[i].writing);
267 
268  /*
269  * We will start out reading the file that THIS backend wrote. There may
270  * be some caching locality advantage to that.
271  */
272  accessor->read_participant = accessor->participant;
273  accessor->read_file = NULL;
274  accessor->read_next_page = 0;
275 }
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
SharedTuplestore * sts
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
#define Assert(condition)
Definition: c.h:804
int i
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155

◆ sts_end_parallel_scan()

void sts_end_parallel_scan ( SharedTuplestoreAccessor accessor)

Definition at line 281 of file sharedtuplestore.c.

References BufFileClose(), and SharedTuplestoreAccessor::read_file.

Referenced by ExecHashTableDetach(), ExecHashTableDetachBatch(), ExecParallelHashCloseBatchAccessors(), ExecParallelHashJoinNewBatch(), ExecParallelHashRepartitionRest(), and sts_begin_parallel_scan().

282 {
283  /*
284  * Here we could delete all files if SHARED_TUPLESTORE_SINGLE_PASS, but
285  * we'd probably need a reference count of current parallel scanners so we
286  * could safely do it only when the reference count reaches zero.
287  */
288  if (accessor->read_file != NULL)
289  {
290  BufFileClose(accessor->read_file);
291  accessor->read_file = NULL;
292  }
293 }
void BufFileClose(BufFile *file)
Definition: buffile.c:407

◆ sts_end_write()

void sts_end_write ( SharedTuplestoreAccessor accessor)

Definition at line 213 of file sharedtuplestore.c.

References BufFileClose(), SharedTuplestoreAccessor::participant, SharedTuplestore::participants, pfree(), SharedTuplestoreAccessor::sts, sts_flush_chunk(), SharedTuplestoreAccessor::write_chunk, SharedTuplestoreAccessor::write_file, and SharedTuplestoreParticipant::writing.

Referenced by ExecHashTableDetach(), ExecParallelHashCloseBatchAccessors(), ExecParallelHashJoinPartitionOuter(), and MultiExecParallelHash().

214 {
215  if (accessor->write_file != NULL)
216  {
217  sts_flush_chunk(accessor);
218  BufFileClose(accessor->write_file);
219  pfree(accessor->write_chunk);
220  accessor->write_chunk = NULL;
221  accessor->write_file = NULL;
222  accessor->sts->participants[accessor->participant].writing = false;
223  }
224 }
static void sts_flush_chunk(SharedTuplestoreAccessor *accessor)
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
SharedTuplestore * sts
void BufFileClose(BufFile *file)
Definition: buffile.c:407
void pfree(void *pointer)
Definition: mcxt.c:1169
SharedTuplestoreChunk * write_chunk

◆ sts_estimate()

size_t sts_estimate ( int  participants)

Definition at line 105 of file sharedtuplestore.c.

References offsetof.

106 {
107  return offsetof(SharedTuplestore, participants) +
108  sizeof(SharedTuplestoreParticipant) * participants;
109 }
struct SharedTuplestoreParticipant SharedTuplestoreParticipant
#define offsetof(type, field)
Definition: c.h:727

◆ sts_filename()

static void sts_filename ( char *  name,
SharedTuplestoreAccessor accessor,
int  participant 
)
static

Definition at line 627 of file sharedtuplestore.c.

References MAXPGPATH, SharedTuplestore::name, snprintf, and SharedTuplestoreAccessor::sts.

Referenced by sts_parallel_scan_next(), and sts_puttuple().

628 {
629  snprintf(name, MAXPGPATH, "%s.p%d", accessor->sts->name, participant);
630 }
char name[NAMEDATALEN]
SharedTuplestore * sts
#define MAXPGPATH
const char * name
Definition: encode.c:561
#define snprintf
Definition: port.h:217

◆ sts_flush_chunk()

static void sts_flush_chunk ( SharedTuplestoreAccessor accessor)
static

Definition at line 196 of file sharedtuplestore.c.

References BufFileWrite(), SharedTuplestoreChunk::data, SharedTuplestoreParticipant::npages, SharedTuplestoreAccessor::participant, SharedTuplestore::participants, SharedTuplestoreAccessor::sts, STS_CHUNK_PAGES, SharedTuplestoreAccessor::write_chunk, SharedTuplestoreAccessor::write_file, and SharedTuplestoreAccessor::write_pointer.

Referenced by sts_end_write(), and sts_puttuple().

197 {
198  size_t size;
199 
200  size = STS_CHUNK_PAGES * BLCKSZ;
201  BufFileWrite(accessor->write_file, accessor->write_chunk, size);
202  memset(accessor->write_chunk, 0, size);
203  accessor->write_pointer = &accessor->write_chunk->data[0];
204  accessor->sts->participants[accessor->participant].npages +=
206 }
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
SharedTuplestore * sts
SharedTuplestoreChunk * write_chunk
#define STS_CHUNK_PAGES
void BufFileWrite(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:598
char data[FLEXIBLE_ARRAY_MEMBER]

◆ sts_initialize()

SharedTuplestoreAccessor* sts_initialize ( SharedTuplestore sts,
int  participants,
int  my_participant_number,
size_t  meta_data_size,
int  flags,
SharedFileSet fileset,
const char *  name 
)

Definition at line 127 of file sharedtuplestore.c.

References Assert, SharedTuplestoreAccessor::context, CurrentMemoryContext, elog, ERROR, SharedTuplestoreAccessor::fileset, SharedTuplestore::flags, i, SharedTuplestoreParticipant::lock, LWLockInitialize(), LWTRANCHE_SHARED_TUPLESTORE, SharedTuplestore::meta_data_size, SharedTuplestore::name, SharedTuplestore::nparticipants, palloc0(), SharedTuplestoreAccessor::participant, SharedTuplestore::participants, SharedTuplestoreParticipant::read_page, SharedTuplestoreAccessor::sts, STS_CHUNK_DATA_SIZE, and SharedTuplestoreParticipant::writing.

Referenced by ExecParallelHashJoinSetUpBatches().

133 {
134  SharedTuplestoreAccessor *accessor;
135  int i;
136 
137  Assert(my_participant_number < participants);
138 
139  sts->nparticipants = participants;
140  sts->meta_data_size = meta_data_size;
141  sts->flags = flags;
142 
143  if (strlen(name) > sizeof(sts->name) - 1)
144  elog(ERROR, "SharedTuplestore name too long");
145  strcpy(sts->name, name);
146 
147  /*
148  * Limit meta-data so it + tuple size always fits into a single chunk.
149  * sts_puttuple() and sts_read_tuple() could be made to support scenarios
150  * where that's not the case, but it's not currently required. If so,
151  * meta-data size probably should be made variable, too.
152  */
153  if (meta_data_size + sizeof(uint32) >= STS_CHUNK_DATA_SIZE)
154  elog(ERROR, "meta-data too long");
155 
156  for (i = 0; i < participants; ++i)
157  {
160  sts->participants[i].read_page = 0;
161  sts->participants[i].writing = false;
162  }
163 
164  accessor = palloc0(sizeof(SharedTuplestoreAccessor));
165  accessor->participant = my_participant_number;
166  accessor->sts = sts;
167  accessor->fileset = fileset;
168  accessor->context = CurrentMemoryContext;
169 
170  return accessor;
171 }
char name[NAMEDATALEN]
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
SharedTuplestore * sts
#define ERROR
Definition: elog.h:46
unsigned int uint32
Definition: c.h:441
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:736
void * palloc0(Size size)
Definition: mcxt.c:1093
#define Assert(condition)
Definition: c.h:804
#define STS_CHUNK_DATA_SIZE
const char * name
Definition: encode.c:561
#define elog(elevel,...)
Definition: elog.h:232
int i

◆ sts_parallel_scan_next()

MinimalTuple sts_parallel_scan_next ( SharedTuplestoreAccessor accessor,
void *  meta_data 
)

Definition at line 521 of file sharedtuplestore.c.

References BufFileClose(), BufFileOpenFileSet(), BufFileRead(), BufFileSeekBlock(), ereport, errcode_for_file_access(), errmsg(), ERROR, SharedTuplestoreAccessor::fileset, SharedFileSet::fs, SharedTuplestoreParticipant::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, name, SharedTuplestoreParticipant::npages, SharedTuplestore::nparticipants, SharedTuplestoreChunk::ntuples, SharedTuplestoreChunk::overflow, SharedTuplestoreAccessor::participant, SharedTuplestore::participants, SharedTuplestoreAccessor::read_bytes, SharedTuplestoreAccessor::read_file, SharedTuplestoreAccessor::read_next_page, SharedTuplestoreAccessor::read_ntuples, SharedTuplestoreAccessor::read_ntuples_available, SharedTuplestoreParticipant::read_page, SharedTuplestoreAccessor::read_participant, SharedTuplestoreAccessor::sts, STS_CHUNK_HEADER_SIZE, STS_CHUNK_PAGES, sts_filename(), and sts_read_tuple().

Referenced by ExecParallelHashJoinNewBatch(), ExecParallelHashJoinOuterGetTuple(), and ExecParallelHashRepartitionRest().

522 {
524  BlockNumber read_page;
525  bool eof;
526 
527  for (;;)
528  {
529  /* Can we read more tuples from the current chunk? */
530  if (accessor->read_ntuples < accessor->read_ntuples_available)
531  return sts_read_tuple(accessor, meta_data);
532 
533  /* Find the location of a new chunk to read. */
534  p = &accessor->sts->participants[accessor->read_participant];
535 
537  /* We can skip directly past overflow pages we know about. */
538  if (p->read_page < accessor->read_next_page)
539  p->read_page = accessor->read_next_page;
540  eof = p->read_page >= p->npages;
541  if (!eof)
542  {
543  /* Claim the next chunk. */
544  read_page = p->read_page;
545  /* Advance the read head for the next reader. */
547  accessor->read_next_page = p->read_page;
548  }
549  LWLockRelease(&p->lock);
550 
551  if (!eof)
552  {
553  SharedTuplestoreChunk chunk_header;
554  size_t nread;
555 
556  /* Make sure we have the file open. */
557  if (accessor->read_file == NULL)
558  {
559  char name[MAXPGPATH];
560 
561  sts_filename(name, accessor, accessor->read_participant);
562  accessor->read_file =
563  BufFileOpenFileSet(&accessor->fileset->fs, name, O_RDONLY,
564  false);
565  }
566 
567  /* Seek and load the chunk header. */
568  if (BufFileSeekBlock(accessor->read_file, read_page) != 0)
569  ereport(ERROR,
571  errmsg("could not seek to block %u in shared tuplestore temporary file",
572  read_page)));
573  nread = BufFileRead(accessor->read_file, &chunk_header,
575  if (nread != STS_CHUNK_HEADER_SIZE)
576  ereport(ERROR,
578  errmsg("could not read from shared tuplestore temporary file: read only %zu of %zu bytes",
579  nread, STS_CHUNK_HEADER_SIZE)));
580 
581  /*
582  * If this is an overflow chunk, we skip it and any following
583  * overflow chunks all at once.
584  */
585  if (chunk_header.overflow > 0)
586  {
587  accessor->read_next_page = read_page +
588  chunk_header.overflow * STS_CHUNK_PAGES;
589  continue;
590  }
591 
592  accessor->read_ntuples = 0;
593  accessor->read_ntuples_available = chunk_header.ntuples;
594  accessor->read_bytes = STS_CHUNK_HEADER_SIZE;
595 
596  /* Go around again, so we can get a tuple from this chunk. */
597  }
598  else
599  {
600  if (accessor->read_file != NULL)
601  {
602  BufFileClose(accessor->read_file);
603  accessor->read_file = NULL;
604  }
605 
606  /*
607  * Try the next participant's file. If we've gone full circle,
608  * we're done.
609  */
610  accessor->read_participant = (accessor->read_participant + 1) %
611  accessor->sts->nparticipants;
612  if (accessor->read_participant == accessor->participant)
613  break;
614  accessor->read_next_page = 0;
615 
616  /* Go around again, so we can get a chunk from this file. */
617  }
618  }
619 
620  return NULL;
621 }
BufFile * BufFileOpenFileSet(FileSet *fileset, const char *name, int mode, bool missing_ok)
Definition: buffile.c:286
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
static void sts_filename(char *name, SharedTuplestoreAccessor *accessor, int participant)
uint32 BlockNumber
Definition: block.h:31
SharedTuplestore * sts
void BufFileClose(BufFile *file)
Definition: buffile.c:407
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define STS_CHUNK_HEADER_SIZE
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:721
static MinimalTuple sts_read_tuple(SharedTuplestoreAccessor *accessor, void *meta_data)
int BufFileSeekBlock(BufFile *file, long blknum)
Definition: buffile.c:773
#define ereport(elevel,...)
Definition: elog.h:157
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
const char * name
Definition: encode.c:561
#define STS_CHUNK_PAGES
int errmsg(const char *fmt,...)
Definition: elog.c:909
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:555

◆ sts_puttuple()

void sts_puttuple ( SharedTuplestoreAccessor accessor,
void *  meta_data,
MinimalTuple  tuple 
)

Definition at line 300 of file sharedtuplestore.c.

References Assert, BufFileCreateFileSet(), SharedTuplestoreAccessor::context, SharedTuplestoreChunk::data, SharedTuplestoreAccessor::fileset, SharedFileSet::fs, MAXPGPATH, MemoryContextAllocZero(), SharedTuplestore::meta_data_size, Min, name, SharedTuplestoreChunk::ntuples, SharedTuplestoreChunk::overflow, SharedTuplestoreAccessor::participant, SharedTuplestore::participants, SharedTuplestoreAccessor::sts, STS_CHUNK_DATA_SIZE, STS_CHUNK_PAGES, sts_filename(), sts_flush_chunk(), MinimalTupleData::t_len, SharedTuplestoreAccessor::write_chunk, SharedTuplestoreAccessor::write_end, SharedTuplestoreAccessor::write_file, SharedTuplestoreAccessor::write_pointer, and SharedTuplestoreParticipant::writing.

Referenced by ExecParallelHashJoinPartitionOuter(), ExecParallelHashRepartitionFirst(), ExecParallelHashRepartitionRest(), and ExecParallelHashTableInsert().

302 {
303  size_t size;
304 
305  /* Do we have our own file yet? */
306  if (accessor->write_file == NULL)
307  {
308  SharedTuplestoreParticipant *participant;
309  char name[MAXPGPATH];
310 
311  /* Create one. Only this backend will write into it. */
312  sts_filename(name, accessor, accessor->participant);
313  accessor->write_file =
314  BufFileCreateFileSet(&accessor->fileset->fs, name);
315 
316  /* Set up the shared state for this backend's file. */
317  participant = &accessor->sts->participants[accessor->participant];
318  participant->writing = true; /* for assertions only */
319  }
320 
321  /* Do we have space? */
322  size = accessor->sts->meta_data_size + tuple->t_len;
323  if (accessor->write_pointer + size >= accessor->write_end)
324  {
325  if (accessor->write_chunk == NULL)
326  {
327  /* First time through. Allocate chunk. */
328  accessor->write_chunk = (SharedTuplestoreChunk *)
330  STS_CHUNK_PAGES * BLCKSZ);
331  accessor->write_chunk->ntuples = 0;
332  accessor->write_pointer = &accessor->write_chunk->data[0];
333  accessor->write_end = (char *)
334  accessor->write_chunk + STS_CHUNK_PAGES * BLCKSZ;
335  }
336  else
337  {
338  /* See if flushing helps. */
339  sts_flush_chunk(accessor);
340  }
341 
342  /* It may still not be enough in the case of a gigantic tuple. */
343  if (accessor->write_pointer + size >= accessor->write_end)
344  {
345  size_t written;
346 
347  /*
348  * We'll write the beginning of the oversized tuple, and then
349  * write the rest in some number of 'overflow' chunks.
350  *
351  * sts_initialize() verifies that the size of the tuple +
352  * meta-data always fits into a chunk. Because the chunk has been
353  * flushed above, we can be sure to have all of a chunk's usable
354  * space available.
355  */
356  Assert(accessor->write_pointer + accessor->sts->meta_data_size +
357  sizeof(uint32) < accessor->write_end);
358 
359  /* Write the meta-data as one chunk. */
360  if (accessor->sts->meta_data_size > 0)
361  memcpy(accessor->write_pointer, meta_data,
362  accessor->sts->meta_data_size);
363 
364  /*
365  * Write as much of the tuple as we can fit. This includes the
366  * tuple's size at the start.
367  */
368  written = accessor->write_end - accessor->write_pointer -
369  accessor->sts->meta_data_size;
370  memcpy(accessor->write_pointer + accessor->sts->meta_data_size,
371  tuple, written);
372  ++accessor->write_chunk->ntuples;
373  size -= accessor->sts->meta_data_size;
374  size -= written;
375  /* Now write as many overflow chunks as we need for the rest. */
376  while (size > 0)
377  {
378  size_t written_this_chunk;
379 
380  sts_flush_chunk(accessor);
381 
382  /*
383  * How many overflow chunks to go? This will allow readers to
384  * skip all of them at once instead of reading each one.
385  */
386  accessor->write_chunk->overflow = (size + STS_CHUNK_DATA_SIZE - 1) /
388  written_this_chunk =
389  Min(accessor->write_end - accessor->write_pointer, size);
390  memcpy(accessor->write_pointer, (char *) tuple + written,
391  written_this_chunk);
392  accessor->write_pointer += written_this_chunk;
393  size -= written_this_chunk;
394  written += written_this_chunk;
395  }
396  return;
397  }
398  }
399 
400  /* Copy meta-data and tuple into buffer. */
401  if (accessor->sts->meta_data_size > 0)
402  memcpy(accessor->write_pointer, meta_data,
403  accessor->sts->meta_data_size);
404  memcpy(accessor->write_pointer + accessor->sts->meta_data_size, tuple,
405  tuple->t_len);
406  accessor->write_pointer += size;
407  ++accessor->write_chunk->ntuples;
408 }
#define Min(x, y)
Definition: c.h:986
static void sts_flush_chunk(SharedTuplestoreAccessor *accessor)
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
static void sts_filename(char *name, SharedTuplestoreAccessor *accessor, int participant)
SharedTuplestore * sts
#define MAXPGPATH
unsigned int uint32
Definition: c.h:441
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
BufFile * BufFileCreateFileSet(FileSet *fileset, const char *name)
Definition: buffile.c:262
#define Assert(condition)
Definition: c.h:804
#define STS_CHUNK_DATA_SIZE
SharedTuplestoreChunk * write_chunk
const char * name
Definition: encode.c:561
#define STS_CHUNK_PAGES
char data[FLEXIBLE_ARRAY_MEMBER]

◆ sts_read_tuple()

static MinimalTuple sts_read_tuple ( SharedTuplestoreAccessor accessor,
void *  meta_data 
)
static

Definition at line 411 of file sharedtuplestore.c.

References BufFileRead(), SharedTuplestoreAccessor::context, ereport, errcode_for_file_access(), errdetail_internal(), errmsg(), ERROR, Max, MemoryContextAlloc(), SharedTuplestore::meta_data_size, Min, SharedTuplestoreChunk::ntuples, SharedTuplestoreChunk::overflow, pfree(), SharedTuplestoreAccessor::read_buffer, SharedTuplestoreAccessor::read_buffer_size, SharedTuplestoreAccessor::read_bytes, SharedTuplestoreAccessor::read_file, SharedTuplestoreAccessor::read_next_page, SharedTuplestoreAccessor::read_ntuples, SharedTuplestoreAccessor::read_ntuples_available, SharedTuplestoreAccessor::sts, STS_CHUNK_HEADER_SIZE, STS_CHUNK_PAGES, and MinimalTupleData::t_len.

Referenced by sts_parallel_scan_next().

412 {
413  MinimalTuple tuple;
414  uint32 size;
415  size_t remaining_size;
416  size_t this_chunk_size;
417  char *destination;
418 
419  /*
420  * We'll keep track of bytes read from this chunk so that we can detect an
421  * overflowing tuple and switch to reading overflow pages.
422  */
423  if (accessor->sts->meta_data_size > 0)
424  {
425  if (BufFileRead(accessor->read_file,
426  meta_data,
427  accessor->sts->meta_data_size) !=
428  accessor->sts->meta_data_size)
429  ereport(ERROR,
431  errmsg("could not read from shared tuplestore temporary file"),
432  errdetail_internal("Short read while reading meta-data.")));
433  accessor->read_bytes += accessor->sts->meta_data_size;
434  }
435  if (BufFileRead(accessor->read_file,
436  &size,
437  sizeof(size)) != sizeof(size))
438  ereport(ERROR,
440  errmsg("could not read from shared tuplestore temporary file"),
441  errdetail_internal("Short read while reading size.")));
442  accessor->read_bytes += sizeof(size);
443  if (size > accessor->read_buffer_size)
444  {
445  size_t new_read_buffer_size;
446 
447  if (accessor->read_buffer != NULL)
448  pfree(accessor->read_buffer);
449  new_read_buffer_size = Max(size, accessor->read_buffer_size * 2);
450  accessor->read_buffer =
451  MemoryContextAlloc(accessor->context, new_read_buffer_size);
452  accessor->read_buffer_size = new_read_buffer_size;
453  }
454  remaining_size = size - sizeof(uint32);
455  this_chunk_size = Min(remaining_size,
456  BLCKSZ * STS_CHUNK_PAGES - accessor->read_bytes);
457  destination = accessor->read_buffer + sizeof(uint32);
458  if (BufFileRead(accessor->read_file,
459  destination,
460  this_chunk_size) != this_chunk_size)
461  ereport(ERROR,
463  errmsg("could not read from shared tuplestore temporary file"),
464  errdetail_internal("Short read while reading tuple.")));
465  accessor->read_bytes += this_chunk_size;
466  remaining_size -= this_chunk_size;
467  destination += this_chunk_size;
468  ++accessor->read_ntuples;
469 
470  /* Check if we need to read any overflow chunks. */
471  while (remaining_size > 0)
472  {
473  /* We are now positioned at the start of an overflow chunk. */
474  SharedTuplestoreChunk chunk_header;
475 
476  if (BufFileRead(accessor->read_file, &chunk_header, STS_CHUNK_HEADER_SIZE) !=
478  ereport(ERROR,
480  errmsg("could not read from shared tuplestore temporary file"),
481  errdetail_internal("Short read while reading overflow chunk header.")));
482  accessor->read_bytes = STS_CHUNK_HEADER_SIZE;
483  if (chunk_header.overflow == 0)
484  ereport(ERROR,
486  errmsg("unexpected chunk in shared tuplestore temporary file"),
487  errdetail_internal("Expected overflow chunk.")));
488  accessor->read_next_page += STS_CHUNK_PAGES;
489  this_chunk_size = Min(remaining_size,
490  BLCKSZ * STS_CHUNK_PAGES -
492  if (BufFileRead(accessor->read_file,
493  destination,
494  this_chunk_size) != this_chunk_size)
495  ereport(ERROR,
497  errmsg("could not read from shared tuplestore temporary file"),
498  errdetail_internal("Short read while reading tuple.")));
499  accessor->read_bytes += this_chunk_size;
500  remaining_size -= this_chunk_size;
501  destination += this_chunk_size;
502 
503  /*
504  * These will be used to count regular tuples following the oversized
505  * tuple that spilled into this overflow chunk.
506  */
507  accessor->read_ntuples = 0;
508  accessor->read_ntuples_available = chunk_header.ntuples;
509  }
510 
511  tuple = (MinimalTuple) accessor->read_buffer;
512  tuple->t_len = size;
513 
514  return tuple;
515 }
#define Min(x, y)
Definition: c.h:986
SharedTuplestore * sts
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1069
void pfree(void *pointer)
Definition: mcxt.c:1169
#define STS_CHUNK_HEADER_SIZE
#define ERROR
Definition: elog.h:46
MinimalTupleData * MinimalTuple
Definition: htup.h:27
int errcode_for_file_access(void)
Definition: elog.c:721
unsigned int uint32
Definition: c.h:441
#define ereport(elevel,...)
Definition: elog.h:157
#define Max(x, y)
Definition: c.h:980
#define STS_CHUNK_PAGES
int errmsg(const char *fmt,...)
Definition: elog.c:909
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
size_t BufFileRead(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:555

◆ sts_reinitialize()

void sts_reinitialize ( SharedTuplestoreAccessor accessor)

Definition at line 234 of file sharedtuplestore.c.

References i, SharedTuplestore::nparticipants, SharedTuplestore::participants, SharedTuplestoreParticipant::read_page, and SharedTuplestoreAccessor::sts.

235 {
236  int i;
237 
238  /*
239  * Reset the shared read head for all participants' files. Also set the
240  * initial chunk size to the minimum (any increases from that size will be
241  * recorded in chunk_expansion_log).
242  */
243  for (i = 0; i < accessor->sts->nparticipants; ++i)
244  {
245  accessor->sts->participants[i].read_page = 0;
246  }
247 }
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]
SharedTuplestore * sts
int i