PostgreSQL Source Code git master
Loading...
Searching...
No Matches
sharedtuplestore.c File Reference
#include "postgres.h"
#include "access/htup.h"
#include "access/htup_details.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 39 of file sharedtuplestore.c.

◆ STS_CHUNK_HEADER_SIZE

#define STS_CHUNK_HEADER_SIZE   offsetof(SharedTuplestoreChunk, data)

Definition at line 38 of file sharedtuplestore.c.

◆ STS_CHUNK_PAGES

#define STS_CHUNK_PAGES   4

Definition at line 37 of file sharedtuplestore.c.

Typedef Documentation

◆ SharedTuplestoreChunk

◆ SharedTuplestoreParticipant

Function Documentation

◆ sts_attach()

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

Definition at line 177 of file sharedtuplestore.c.

180{
182
184
186 accessor->participant = my_participant_number;
187 accessor->sts = sts;
188 accessor->fileset = fileset;
190
191 return accessor;
192}
#define Assert(condition)
Definition c.h:873
#define palloc0_object(type)
Definition fe_memutils.h:75
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
static int fb(int x)

References Assert, CurrentMemoryContext, fb(), and palloc0_object.

Referenced by ExecParallelHashEnsureBatchAccessors(), and ExecParallelHashRepartitionRest().

◆ sts_begin_parallel_scan()

void sts_begin_parallel_scan ( SharedTuplestoreAccessor accessor)

Definition at line 252 of file sharedtuplestore.c.

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

References Assert, fb(), i, PG_USED_FOR_ASSERTS_ONLY, and sts_end_parallel_scan().

Referenced by ExecParallelHashJoinNewBatch(), and ExecParallelHashRepartitionRest().

◆ sts_end_parallel_scan()

void sts_end_parallel_scan ( SharedTuplestoreAccessor accessor)

Definition at line 280 of file sharedtuplestore.c.

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

References BufFileClose(), and fb().

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

213{
214 if (accessor->write_file != NULL)
215 {
217 BufFileClose(accessor->write_file);
218 pfree(accessor->write_chunk);
219 accessor->write_chunk = NULL;
220 accessor->write_file = NULL;
221 accessor->sts->participants[accessor->participant].writing = false;
222 }
223}
void pfree(void *pointer)
Definition mcxt.c:1616
static void sts_flush_chunk(SharedTuplestoreAccessor *accessor)

References BufFileClose(), fb(), pfree(), and sts_flush_chunk().

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

◆ sts_estimate()

size_t sts_estimate ( int  participants)

Definition at line 103 of file sharedtuplestore.c.

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

References fb().

◆ sts_filename()

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

Definition at line 598 of file sharedtuplestore.c.

599{
600 snprintf(name, MAXPGPATH, "%s.p%d", accessor->sts->name, participant);
601}
#define MAXPGPATH
#define snprintf
Definition port.h:260
const char * name

References fb(), MAXPGPATH, name, and snprintf.

Referenced by sts_parallel_scan_next(), and sts_puttuple().

◆ sts_flush_chunk()

static void sts_flush_chunk ( SharedTuplestoreAccessor accessor)
static

Definition at line 195 of file sharedtuplestore.c.

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

References BufFileWrite(), fb(), and STS_CHUNK_PAGES.

Referenced by sts_end_write(), and sts_puttuple().

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

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

References Assert, CurrentMemoryContext, elog, ERROR, fb(), SharedTuplestore::flags, i, SharedTuplestoreParticipant::lock, LWLockInitialize(), SharedTuplestore::meta_data_size, name, SharedTuplestore::name, SharedTuplestoreParticipant::npages, SharedTuplestore::nparticipants, palloc0_object, SharedTuplestore::participants, SharedTuplestoreParticipant::read_page, 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)
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 {
528
529 /* Make sure we have the file open. */
530 if (accessor->read_file == NULL)
531 {
532 char name[MAXPGPATH];
534
535 sts_filename(name, accessor, accessor->read_participant);
536
538 accessor->read_file =
540 false);
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)));
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;
565 accessor->read_bytes = STS_CHUNK_HEADER_SIZE;
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:850
void BufFileReadExact(BufFile *file, void *ptr, size_t size)
Definition buffile.c:654
int errcode_for_file_access(void)
Definition elog.c:886
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ereport(elevel,...)
Definition elog.h:150
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_EXCLUSIVE
Definition lwlock.h:112
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
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)

References BufFileClose(), BufFileOpenFileSet(), BufFileReadExact(), BufFileSeekBlock(), ereport, errcode_for_file_access(), errmsg(), ERROR, fb(), SharedTuplestoreParticipant::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, MemoryContextSwitchTo(), name, SharedTuplestoreParticipant::npages, SharedTuplestoreParticipant::read_page, 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 299 of file sharedtuplestore.c.

301{
302 size_t size;
303
304 /* Do we have our own file yet? */
305 if (accessor->write_file == NULL)
306 {
307 SharedTuplestoreParticipant *participant;
308 char name[MAXPGPATH];
310
311 /* Create one. Only this backend will write into it. */
312 sts_filename(name, accessor, accessor->participant);
313
315 accessor->write_file =
316 BufFileCreateFileSet(&accessor->fileset->fs, name);
318
319 /* Set up the shared state for this backend's file. */
320 participant = &accessor->sts->participants[accessor->participant];
321 participant->writing = true; /* for assertions only */
322 }
323
324 /* Do we have space? */
325 size = accessor->sts->meta_data_size + tuple->t_len;
326 if (accessor->write_pointer == NULL ||
327 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 *)
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. */
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
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) /
393 Min(accessor->write_end - accessor->write_pointer, size);
394 memcpy(accessor->write_pointer, (char *) tuple + written,
396 accessor->write_pointer += written_this_chunk;
397 size -= 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:997
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266

References Assert, BufFileCreateFileSet(), fb(), MAXPGPATH, MemoryContextAllocZero(), MemoryContextSwitchTo(), Min, name, STS_CHUNK_DATA_SIZE, STS_CHUNK_PAGES, sts_filename(), sts_flush_chunk(), MinimalTupleData::t_len, and SharedTuplestoreParticipant::writing.

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

◆ sts_read_tuple()

static MinimalTuple sts_read_tuple ( SharedTuplestoreAccessor accessor,
void meta_data 
)
static

Definition at line 415 of file sharedtuplestore.c.

416{
417 MinimalTuple tuple;
418 uint32 size;
419 size_t remaining_size;
420 size_t this_chunk_size;
421 char *destination;
422
423 /*
424 * We'll keep track of bytes read from this chunk so that we can detect an
425 * overflowing tuple and switch to reading overflow pages.
426 */
427 if (accessor->sts->meta_data_size > 0)
428 {
429 BufFileReadExact(accessor->read_file, meta_data, accessor->sts->meta_data_size);
430 accessor->read_bytes += accessor->sts->meta_data_size;
431 }
432 BufFileReadExact(accessor->read_file, &size, sizeof(size));
433 accessor->read_bytes += sizeof(size);
434 if (size > accessor->read_buffer_size)
435 {
437
438 if (accessor->read_buffer != NULL)
439 pfree(accessor->read_buffer);
440 new_read_buffer_size = Max(size, accessor->read_buffer_size * 2);
441 accessor->read_buffer =
443 accessor->read_buffer_size = new_read_buffer_size;
444 }
445 remaining_size = size - sizeof(uint32);
447 BLCKSZ * STS_CHUNK_PAGES - accessor->read_bytes);
448 destination = accessor->read_buffer + sizeof(uint32);
450 accessor->read_bytes += this_chunk_size;
453 ++accessor->read_ntuples;
454
455 /* Check if we need to read any overflow chunks. */
456 while (remaining_size > 0)
457 {
458 /* We are now positioned at the start of an overflow chunk. */
460
462 accessor->read_bytes = STS_CHUNK_HEADER_SIZE;
463 if (chunk_header.overflow == 0)
466 errmsg("unexpected chunk in shared tuplestore temporary file"),
467 errdetail_internal("Expected overflow chunk.")));
468 accessor->read_next_page += STS_CHUNK_PAGES;
473 accessor->read_bytes += this_chunk_size;
476
477 /*
478 * These will be used to count regular tuples following the oversized
479 * tuple that spilled into this overflow chunk.
480 */
481 accessor->read_ntuples = 0;
482 accessor->read_ntuples_available = chunk_header.ntuples;
483 }
484
485 tuple = (MinimalTuple) accessor->read_buffer;
486 tuple->t_len = size;
487
488 return tuple;
489}
#define Max(x, y)
Definition c.h:991
int errdetail_internal(const char *fmt,...)
Definition elog.c:1243
MinimalTupleData * MinimalTuple
Definition htup.h:27
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232

References BufFileReadExact(), ereport, errcode_for_file_access(), errdetail_internal(), errmsg(), ERROR, fb(), Max, MemoryContextAlloc(), Min, pfree(), STS_CHUNK_HEADER_SIZE, STS_CHUNK_PAGES, and MinimalTupleData::t_len.

Referenced by sts_parallel_scan_next().

◆ sts_reinitialize()

void sts_reinitialize ( SharedTuplestoreAccessor accessor)

Definition at line 233 of file sharedtuplestore.c.

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

References fb(), and i.