PostgreSQL Source Code git master
sharedtuplestore.h File Reference
#include "access/htup.h"
#include "storage/fd.h"
#include "storage/sharedfileset.h"
Include dependency graph for sharedtuplestore.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SHARED_TUPLESTORE_SINGLE_PASS   0x01
 

Typedefs

typedef struct SharedTuplestore SharedTuplestore
 
typedef struct SharedTuplestoreAccessor SharedTuplestoreAccessor
 

Functions

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)
 
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)
 
MinimalTuple sts_parallel_scan_next (SharedTuplestoreAccessor *accessor, void *meta_data)
 

Macro Definition Documentation

◆ SHARED_TUPLESTORE_SINGLE_PASS

#define SHARED_TUPLESTORE_SINGLE_PASS   0x01

Definition at line 30 of file sharedtuplestore.h.

Typedef Documentation

◆ SharedTuplestore

Definition at line 21 of file sharedtuplestore.h.

◆ SharedTuplestoreAccessor

Definition at line 24 of file sharedtuplestore.h.

Function Documentation

◆ sts_attach()

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

Definition at line 178 of file sharedtuplestore.c.

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}
#define Assert(condition)
Definition: c.h:815
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
SharedTuplestore * sts

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

Referenced by ExecParallelHashEnsureBatchAccessors(), and ExecParallelHashRepartitionRest().

◆ sts_begin_parallel_scan()

void sts_begin_parallel_scan ( SharedTuplestoreAccessor accessor)

Definition at line 253 of file sharedtuplestore.c.

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}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:204
int i
Definition: isn.c:72
void sts_end_parallel_scan(SharedTuplestoreAccessor *accessor)
SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER]

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

◆ sts_end_parallel_scan()

void sts_end_parallel_scan ( SharedTuplestoreAccessor accessor)

Definition at line 281 of file sharedtuplestore.c.

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:412

References BufFileClose(), and SharedTuplestoreAccessor::read_file.

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

◆ sts_end_write()

void sts_end_write ( SharedTuplestoreAccessor accessor)

Definition at line 213 of file sharedtuplestore.c.

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}
void pfree(void *pointer)
Definition: mcxt.c:1521
static void sts_flush_chunk(SharedTuplestoreAccessor *accessor)
SharedTuplestoreChunk * write_chunk

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

◆ sts_estimate()

size_t sts_estimate ( int  participants)

Definition at line 104 of file sharedtuplestore.c.

105{
106 return offsetof(SharedTuplestore, participants) +
107 sizeof(SharedTuplestoreParticipant) * participants;
108}
struct SharedTuplestoreParticipant SharedTuplestoreParticipant

◆ 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 126 of file sharedtuplestore.c.

132{
133 SharedTuplestoreAccessor *accessor;
134 int i;
135
136 Assert(my_participant_number < participants);
137
138 sts->nparticipants = participants;
139 sts->meta_data_size = meta_data_size;
140 sts->flags = flags;
141
142 if (strlen(name) > sizeof(sts->name) - 1)
143 elog(ERROR, "SharedTuplestore name too long");
144 strcpy(sts->name, name);
145
146 /*
147 * Limit meta-data so it + tuple size always fits into a single chunk.
148 * sts_puttuple() and sts_read_tuple() could be made to support scenarios
149 * where that's not the case, but it's not currently required. If so,
150 * meta-data size probably should be made variable, too.
151 */
152 if (meta_data_size + sizeof(uint32) >= STS_CHUNK_DATA_SIZE)
153 elog(ERROR, "meta-data too long");
154
155 for (i = 0; i < participants; ++i)
156 {
159 sts->participants[i].read_page = 0;
160 sts->participants[i].npages = 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}
uint32_t uint32
Definition: c.h:488
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
@ LWTRANCHE_SHARED_TUPLESTORE
Definition: lwlock.h:199
#define STS_CHUNK_DATA_SIZE
char name[NAMEDATALEN]
const char * name

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

Referenced by ExecParallelHashJoinSetUpBatches().

◆ sts_parallel_scan_next()

MinimalTuple sts_parallel_scan_next ( SharedTuplestoreAccessor accessor,
void *  meta_data 
)

Definition at line 495 of file sharedtuplestore.c.

496{
498 BlockNumber read_page;
499 bool eof;
500
501 for (;;)
502 {
503 /* Can we read more tuples from the current chunk? */
504 if (accessor->read_ntuples < accessor->read_ntuples_available)
505 return sts_read_tuple(accessor, meta_data);
506
507 /* Find the location of a new chunk to read. */
508 p = &accessor->sts->participants[accessor->read_participant];
509
511 /* We can skip directly past overflow pages we know about. */
512 if (p->read_page < accessor->read_next_page)
513 p->read_page = accessor->read_next_page;
514 eof = p->read_page >= p->npages;
515 if (!eof)
516 {
517 /* Claim the next chunk. */
518 read_page = p->read_page;
519 /* Advance the read head for the next reader. */
521 accessor->read_next_page = p->read_page;
522 }
523 LWLockRelease(&p->lock);
524
525 if (!eof)
526 {
527 SharedTuplestoreChunk chunk_header;
528
529 /* Make sure we have the file open. */
530 if (accessor->read_file == NULL)
531 {
532 char name[MAXPGPATH];
533 MemoryContext oldcxt;
534
535 sts_filename(name, accessor, accessor->read_participant);
536
537 oldcxt = MemoryContextSwitchTo(accessor->context);
538 accessor->read_file =
539 BufFileOpenFileSet(&accessor->fileset->fs, name, O_RDONLY,
540 false);
541 MemoryContextSwitchTo(oldcxt);
542 }
543
544 /* Seek and load the chunk header. */
545 if (BufFileSeekBlock(accessor->read_file, read_page) != 0)
548 errmsg("could not seek to block %u in shared tuplestore temporary file",
549 read_page)));
550 BufFileReadExact(accessor->read_file, &chunk_header, STS_CHUNK_HEADER_SIZE);
551
552 /*
553 * If this is an overflow chunk, we skip it and any following
554 * overflow chunks all at once.
555 */
556 if (chunk_header.overflow > 0)
557 {
558 accessor->read_next_page = read_page +
559 chunk_header.overflow * STS_CHUNK_PAGES;
560 continue;
561 }
562
563 accessor->read_ntuples = 0;
564 accessor->read_ntuples_available = chunk_header.ntuples;
566
567 /* Go around again, so we can get a tuple from this chunk. */
568 }
569 else
570 {
571 if (accessor->read_file != NULL)
572 {
573 BufFileClose(accessor->read_file);
574 accessor->read_file = NULL;
575 }
576
577 /*
578 * Try the next participant's file. If we've gone full circle,
579 * we're done.
580 */
581 accessor->read_participant = (accessor->read_participant + 1) %
582 accessor->sts->nparticipants;
583 if (accessor->read_participant == accessor->participant)
584 break;
585 accessor->read_next_page = 0;
586
587 /* Go around again, so we can get a chunk from this file. */
588 }
589 }
590
591 return NULL;
592}
uint32 BlockNumber
Definition: block.h:31
BufFile * BufFileOpenFileSet(FileSet *fileset, const char *name, int mode, bool missing_ok)
Definition: buffile.c:291
int BufFileSeekBlock(BufFile *file, int64 blknum)
Definition: buffile.c:851
void BufFileReadExact(BufFile *file, void *ptr, size_t size)
Definition: buffile.c:654
int errcode_for_file_access(void)
Definition: elog.c:876
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define MAXPGPATH
static MinimalTuple sts_read_tuple(SharedTuplestoreAccessor *accessor, void *meta_data)
#define STS_CHUNK_HEADER_SIZE
static void sts_filename(char *name, SharedTuplestoreAccessor *accessor, int participant)
#define STS_CHUNK_PAGES

References BufFileClose(), BufFileOpenFileSet(), BufFileReadExact(), BufFileSeekBlock(), SharedTuplestoreAccessor::context, ereport, errcode_for_file_access(), errmsg(), ERROR, SharedTuplestoreAccessor::fileset, SharedFileSet::fs, if(), SharedTuplestoreParticipant::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, MemoryContextSwitchTo(), 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().

◆ sts_puttuple()

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

Definition at line 300 of file sharedtuplestore.c.

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

References Assert, BufFileCreateFileSet(), SharedTuplestoreAccessor::context, SharedTuplestoreChunk::data, SharedTuplestoreAccessor::fileset, SharedFileSet::fs, MAXPGPATH, MemoryContextAllocZero(), MemoryContextSwitchTo(), SharedTuplestore::meta_data_size, Min, name, SharedTuplestoreChunk::ntuples, SharedTuplestoreChunk::overflow, SharedTuplestoreAccessor::participant, SharedTuplestore::participants, size, 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().

◆ sts_reinitialize()

void sts_reinitialize ( SharedTuplestoreAccessor accessor)

Definition at line 234 of file sharedtuplestore.c.

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}

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