#include "executor/tuptable.h"
Go to the source code of this file.
#define tuplestore_donestoring | ( | state | ) | ((void) 0) |
Definition at line 60 of file tuplestore.h.
Referenced by dblink_get_notify(), deflist_to_tuplestore(), get_crosstab_tuplestore(), materializeResult(), pg_available_extension_versions(), pg_available_extensions(), pg_cursor(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_prepared_statement(), pg_stat_get_wal_senders(), pg_stat_statements(), and xpath_table().
typedef struct Tuplestorestate Tuplestorestate |
Definition at line 40 of file tuplestore.h.
bool tuplestore_advance | ( | Tuplestorestate * | state, | |
bool | forward | |||
) |
Definition at line 1063 of file tuplestore.c.
References pfree(), and tuplestore_gettuple().
Referenced by CteScanNext(), ExecMaterial(), PersistHoldablePortal(), window_gettupleslot(), and WinSetMarkPosition().
{ void *tuple; bool should_free; tuple = tuplestore_gettuple(state, forward, &should_free); if (tuple) { if (should_free) pfree(tuple); return true; } else { return false; } }
int tuplestore_alloc_read_pointer | ( | Tuplestorestate * | state, | |
int | eflags | |||
) |
Definition at line 371 of file tuplestore.c.
References TSReadPointer::eflags, Tuplestorestate::eflags, elog, ERROR, Tuplestorestate::memtupcount, Tuplestorestate::readptrcount, Tuplestorestate::readptrs, Tuplestorestate::readptrsize, repalloc(), Tuplestorestate::status, and TSS_INMEM.
Referenced by begin_partition(), ExecInitCteScan(), and ExecMaterial().
{ /* Check for possible increase of requirements */ if (state->status != TSS_INMEM || state->memtupcount != 0) { if ((state->eflags | eflags) != state->eflags) elog(ERROR, "too late to require new tuplestore eflags"); } /* Make room for another read pointer if needed */ if (state->readptrcount >= state->readptrsize) { int newcnt = state->readptrsize * 2; state->readptrs = (TSReadPointer *) repalloc(state->readptrs, newcnt * sizeof(TSReadPointer)); state->readptrsize = newcnt; } /* And set it up */ state->readptrs[state->readptrcount] = state->readptrs[0]; state->readptrs[state->readptrcount].eflags = eflags; state->eflags |= eflags; return state->readptrcount++; }
bool tuplestore_ateof | ( | Tuplestorestate * | state | ) |
Definition at line 528 of file tuplestore.c.
References Tuplestorestate::activeptr, TSReadPointer::eof_reached, and Tuplestorestate::readptrs.
Referenced by CteScanNext(), and ExecMaterial().
{ return state->readptrs[state->activeptr].eof_reached; }
Tuplestorestate* tuplestore_begin_heap | ( | bool | randomAccess, | |
bool | interXact, | |||
int | maxKBytes | |||
) |
Definition at line 306 of file tuplestore.c.
References Tuplestorestate::copytup, EXEC_FLAG_BACKWARD, EXEC_FLAG_REWIND, Tuplestorestate::readtup, tuplestore_begin_common(), and Tuplestorestate::writetup.
Referenced by begin_partition(), connectby(), crosstab(), dblink_get_notify(), deflist_to_tuplestore(), each_worker(), exec_init_tuple_store(), ExecInitCteScan(), ExecInitRecursiveUnion(), ExecMakeTableFunctionResult(), ExecMaterial(), ExecRecursiveUnion(), fmgr_sql(), get_crosstab_tuplestore(), json_array_elements(), json_populate_recordset(), materializeQueryResult(), materializeResult(), pg_available_extension_versions(), pg_available_extensions(), pg_cursor(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_prepared_statement(), pg_stat_get_wal_senders(), pg_stat_statements(), plperl_return_next(), PortalCreateHoldStore(), storeRow(), and xpath_table().
{ Tuplestorestate *state; int eflags; /* * This interpretation of the meaning of randomAccess is compatible with * the pre-8.3 behavior of tuplestores. */ eflags = randomAccess ? (EXEC_FLAG_BACKWARD | EXEC_FLAG_REWIND) : (EXEC_FLAG_REWIND); state = tuplestore_begin_common(eflags, interXact, maxKBytes); state->copytup = copytup_heap; state->writetup = writetup_heap; state->readtup = readtup_heap; return state; }
void tuplestore_clear | ( | Tuplestorestate * | state | ) |
Definition at line 406 of file tuplestore.c.
References BufFileClose(), TSReadPointer::current, TSReadPointer::eof_reached, FREEMEM, GetMemoryChunkSpace(), i, Tuplestorestate::memtupcount, Tuplestorestate::memtupdeleted, Tuplestorestate::memtuples, Tuplestorestate::myfile, pfree(), Tuplestorestate::readptrcount, Tuplestorestate::readptrs, Tuplestorestate::status, and Tuplestorestate::truncated.
Referenced by ExecReScanCteScan(), ExecReScanRecursiveUnion(), and fmgr_sql().
{ int i; TSReadPointer *readptr; if (state->myfile) BufFileClose(state->myfile); state->myfile = NULL; if (state->memtuples) { for (i = state->memtupdeleted; i < state->memtupcount; i++) { FREEMEM(state, GetMemoryChunkSpace(state->memtuples[i])); pfree(state->memtuples[i]); } } state->status = TSS_INMEM; state->truncated = false; state->memtupdeleted = 0; state->memtupcount = 0; readptr = state->readptrs; for (i = 0; i < state->readptrcount; readptr++, i++) { readptr->eof_reached = false; readptr->current = 0; } }
void tuplestore_copy_read_pointer | ( | Tuplestorestate * | state, | |
int | srcptr, | |||
int | destptr | |||
) |
Definition at line 1150 of file tuplestore.c.
References Tuplestorestate::activeptr, Assert, BufFileSeek(), BufFileTell(), Tuplestorestate::eflags, TSReadPointer::eflags, elog, TSReadPointer::eof_reached, ERROR, TSReadPointer::file, i, Tuplestorestate::myfile, TSReadPointer::offset, Tuplestorestate::readptrcount, Tuplestorestate::readptrs, Tuplestorestate::status, TSS_INMEM, TSS_READFILE, TSS_WRITEFILE, Tuplestorestate::writepos_file, and Tuplestorestate::writepos_offset.
Referenced by ExecMaterialMarkPos(), and ExecMaterialRestrPos().
{ TSReadPointer *sptr = &state->readptrs[srcptr]; TSReadPointer *dptr = &state->readptrs[destptr]; Assert(srcptr >= 0 && srcptr < state->readptrcount); Assert(destptr >= 0 && destptr < state->readptrcount); /* Assigning to self is a no-op */ if (srcptr == destptr) return; if (dptr->eflags != sptr->eflags) { /* Possible change of overall eflags, so copy and then recompute */ int eflags; int i; *dptr = *sptr; eflags = state->readptrs[0].eflags; for (i = 1; i < state->readptrcount; i++) eflags |= state->readptrs[i].eflags; state->eflags = eflags; } else *dptr = *sptr; switch (state->status) { case TSS_INMEM: case TSS_WRITEFILE: /* no work */ break; case TSS_READFILE: /* * This case is a bit tricky since the active read pointer's * position corresponds to the seek point, not what is in its * variables. Assigning to the active requires a seek, and * assigning from the active requires a tell, except when * eof_reached. */ if (destptr == state->activeptr) { if (dptr->eof_reached) { if (BufFileSeek(state->myfile, state->writepos_file, state->writepos_offset, SEEK_SET) != 0) elog(ERROR, "tuplestore seek failed"); } else { if (BufFileSeek(state->myfile, dptr->file, dptr->offset, SEEK_SET) != 0) elog(ERROR, "tuplestore seek failed"); } } else if (srcptr == state->activeptr) { if (!dptr->eof_reached) BufFileTell(state->myfile, &dptr->file, &dptr->offset); } break; default: elog(ERROR, "invalid tuplestore state"); break; } }
void tuplestore_end | ( | Tuplestorestate * | state | ) |
Definition at line 440 of file tuplestore.c.
References BufFileClose(), i, Tuplestorestate::memtupdeleted, Tuplestorestate::memtuples, Tuplestorestate::myfile, pfree(), and Tuplestorestate::readptrs.
Referenced by ExecEndCteScan(), ExecEndFunctionScan(), ExecEndMaterial(), ExecEndRecursiveUnion(), ExecMakeFunctionResult(), ExecRecursiveUnion(), ExecReScanFunctionScan(), ExecReScanMaterial(), PortalDrop(), release_partition(), ShutdownFuncExpr(), ShutdownSQLFunction(), and storeRow().
bool tuplestore_gettupleslot | ( | Tuplestorestate * | state, | |
bool | forward, | |||
bool | copy, | |||
TupleTableSlot * | slot | |||
) |
Definition at line 1030 of file tuplestore.c.
References ExecClearTuple(), ExecStoreMinimalTuple(), heap_copy_minimal_tuple(), and tuplestore_gettuple().
Referenced by CteScanNext(), ExecMakeFunctionResult(), ExecMaterial(), ExecWindowAgg(), fmgr_sql(), FunctionNext(), RunFromStore(), window_gettupleslot(), and WorkTableScanNext().
{ MinimalTuple tuple; bool should_free; tuple = (MinimalTuple) tuplestore_gettuple(state, forward, &should_free); if (tuple) { if (copy && !should_free) { tuple = heap_copy_minimal_tuple(tuple); should_free = true; } ExecStoreMinimalTuple(tuple, slot, should_free); return true; } else { ExecClearTuple(slot); return false; } }
bool tuplestore_in_memory | ( | Tuplestorestate * | state | ) |
Definition at line 1333 of file tuplestore.c.
References Tuplestorestate::status, and TSS_INMEM.
Referenced by spool_tuples().
void tuplestore_puttuple | ( | Tuplestorestate * | state, | |
HeapTuple | tuple | |||
) |
Definition at line 692 of file tuplestore.c.
References Tuplestorestate::context, COPYTUP, MemoryContextSwitchTo(), and tuplestore_puttuple_common().
Referenced by build_tuplestore_recursively(), crosstab(), each_object_field_end(), elements_array_element_end(), exec_stmt_return_next(), exec_stmt_return_query(), ExecMakeTableFunctionResult(), get_crosstab_tuplestore(), materializeQueryResult(), materializeResult(), plperl_return_next(), populate_recordset_object_end(), storeRow(), and xpath_table().
{ MemoryContext oldcxt = MemoryContextSwitchTo(state->context); /* * Copy the tuple. (Must do this even in WRITEFILE case. Note that * COPYTUP includes USEMEM, so we needn't do that here.) */ tuple = COPYTUP(state, tuple); tuplestore_puttuple_common(state, (void *) tuple); MemoryContextSwitchTo(oldcxt); }
void tuplestore_puttupleslot | ( | Tuplestorestate * | state, | |
TupleTableSlot * | slot | |||
) |
Definition at line 670 of file tuplestore.c.
References Tuplestorestate::context, ExecCopySlotMinimalTuple(), GetMemoryChunkSpace(), MemoryContextSwitchTo(), tuplestore_puttuple_common(), and USEMEM.
Referenced by begin_partition(), CteScanNext(), ExecMaterial(), ExecRecursiveUnion(), spool_tuples(), sqlfunction_receive(), and tstoreReceiveSlot_notoast().
{ MinimalTuple tuple; MemoryContext oldcxt = MemoryContextSwitchTo(state->context); /* * Form a MinimalTuple in working memory */ tuple = ExecCopySlotMinimalTuple(slot); USEMEM(state, GetMemoryChunkSpace(tuple)); tuplestore_puttuple_common(state, (void *) tuple); MemoryContextSwitchTo(oldcxt); }
void tuplestore_putvalues | ( | Tuplestorestate * | state, | |
TupleDesc | tdesc, | |||
Datum * | values, | |||
bool * | isnull | |||
) |
Definition at line 712 of file tuplestore.c.
References Tuplestorestate::context, GetMemoryChunkSpace(), heap_form_minimal_tuple(), MemoryContextSwitchTo(), tuplestore_puttuple_common(), and USEMEM.
Referenced by dblink_get_notify(), deflist_to_tuplestore(), exec_stmt_return_next(), ExecMakeTableFunctionResult(), get_available_versions_for_extension(), pg_available_extensions(), pg_cursor(), pg_event_trigger_dropped_objects(), pg_extension_update_paths(), pg_prepared_statement(), pg_stat_get_wal_senders(), pg_stat_statements(), plperl_return_next(), and tstoreReceiveSlot_detoast().
{ MinimalTuple tuple; MemoryContext oldcxt = MemoryContextSwitchTo(state->context); tuple = heap_form_minimal_tuple(tdesc, values, isnull); USEMEM(state, GetMemoryChunkSpace(tuple)); tuplestore_puttuple_common(state, (void *) tuple); MemoryContextSwitchTo(oldcxt); }
void tuplestore_rescan | ( | Tuplestorestate * | state | ) |
Definition at line 1117 of file tuplestore.c.
References Tuplestorestate::activeptr, Assert, BufFileSeek(), TSReadPointer::current, TSReadPointer::eflags, elog, TSReadPointer::eof_reached, ERROR, EXEC_FLAG_REWIND, TSReadPointer::file, Tuplestorestate::myfile, TSReadPointer::offset, Tuplestorestate::readptrs, Tuplestorestate::status, Tuplestorestate::truncated, TSS_INMEM, TSS_READFILE, and TSS_WRITEFILE.
Referenced by DoPortalRewind(), ExecReScanCteScan(), ExecReScanFunctionScan(), ExecReScanMaterial(), ExecReScanWorkTableScan(), and PersistHoldablePortal().
{ TSReadPointer *readptr = &state->readptrs[state->activeptr]; Assert(readptr->eflags & EXEC_FLAG_REWIND); Assert(!state->truncated); switch (state->status) { case TSS_INMEM: readptr->eof_reached = false; readptr->current = 0; break; case TSS_WRITEFILE: readptr->eof_reached = false; readptr->file = 0; readptr->offset = 0L; break; case TSS_READFILE: readptr->eof_reached = false; if (BufFileSeek(state->myfile, 0, 0L, SEEK_SET) != 0) elog(ERROR, "tuplestore seek to start failed"); break; default: elog(ERROR, "invalid tuplestore state"); break; } }
void tuplestore_select_read_pointer | ( | Tuplestorestate * | state, | |
int | ptr | |||
) |
Definition at line 460 of file tuplestore.c.
References Tuplestorestate::activeptr, Assert, BufFileSeek(), BufFileTell(), elog, TSReadPointer::eof_reached, ERROR, TSReadPointer::file, Tuplestorestate::myfile, TSReadPointer::offset, Tuplestorestate::readptrs, Tuplestorestate::status, TSS_INMEM, TSS_READFILE, TSS_WRITEFILE, Tuplestorestate::writepos_file, and Tuplestorestate::writepos_offset.
Referenced by CteScanNext(), ExecReScanCteScan(), ExecWindowAgg(), window_gettupleslot(), and WinSetMarkPosition().
{ TSReadPointer *readptr; TSReadPointer *oldptr; Assert(ptr >= 0 && ptr < state->readptrcount); /* No work if already active */ if (ptr == state->activeptr) return; readptr = &state->readptrs[ptr]; oldptr = &state->readptrs[state->activeptr]; switch (state->status) { case TSS_INMEM: case TSS_WRITEFILE: /* no work */ break; case TSS_READFILE: /* * First, save the current read position in the pointer about to * become inactive. */ if (!oldptr->eof_reached) BufFileTell(state->myfile, &oldptr->file, &oldptr->offset); /* * We have to make the temp file's seek position equal to the * logical position of the new read pointer. In eof_reached * state, that's the EOF, which we have available from the saved * write position. */ if (readptr->eof_reached) { if (BufFileSeek(state->myfile, state->writepos_file, state->writepos_offset, SEEK_SET) != 0) elog(ERROR, "tuplestore seek failed"); } else { if (BufFileSeek(state->myfile, readptr->file, readptr->offset, SEEK_SET) != 0) elog(ERROR, "tuplestore seek failed"); } break; default: elog(ERROR, "invalid tuplestore state"); break; } state->activeptr = ptr; }
void tuplestore_set_eflags | ( | Tuplestorestate * | state, | |
int | eflags | |||
) |
Definition at line 347 of file tuplestore.c.
References Tuplestorestate::eflags, TSReadPointer::eflags, elog, ERROR, i, Tuplestorestate::memtupcount, Tuplestorestate::readptrcount, Tuplestorestate::readptrs, Tuplestorestate::status, and TSS_INMEM.
Referenced by begin_partition(), ExecInitCteScan(), and ExecMaterial().
void tuplestore_trim | ( | Tuplestorestate * | state | ) |
Definition at line 1238 of file tuplestore.c.
References Assert, TSReadPointer::current, Tuplestorestate::eflags, TSReadPointer::eof_reached, EXEC_FLAG_REWIND, FREEMEM, GetMemoryChunkSpace(), i, memmove, Tuplestorestate::memtupcount, Tuplestorestate::memtupdeleted, Tuplestorestate::memtuples, Min, pfree(), Tuplestorestate::readptrcount, Tuplestorestate::readptrs, Tuplestorestate::status, Tuplestorestate::truncated, and TSS_INMEM.
Referenced by ExecMaterialMarkPos(), and ExecWindowAgg().
{ int oldest; int nremove; int i; /* * Truncation is disallowed if any read pointer requires rewind * capability. */ if (state->eflags & EXEC_FLAG_REWIND) return; /* * We don't bother trimming temp files since it usually would mean more * work than just letting them sit in kernel buffers until they age out. */ if (state->status != TSS_INMEM) return; /* Find the oldest read pointer */ oldest = state->memtupcount; for (i = 0; i < state->readptrcount; i++) { if (!state->readptrs[i].eof_reached) oldest = Min(oldest, state->readptrs[i].current); } /* * Note: you might think we could remove all the tuples before the oldest * "current", since that one is the next to be returned. However, since * tuplestore_gettuple returns a direct pointer to our internal copy of * the tuple, it's likely that the caller has still got the tuple just * before "current" referenced in a slot. So we keep one extra tuple * before the oldest "current". (Strictly speaking, we could require such * callers to use the "copy" flag to tuplestore_gettupleslot, but for * efficiency we allow this one case to not use "copy".) */ nremove = oldest - 1; if (nremove <= 0) return; /* nothing to do */ Assert(nremove >= state->memtupdeleted); Assert(nremove <= state->memtupcount); /* Release no-longer-needed tuples */ for (i = state->memtupdeleted; i < nremove; i++) { FREEMEM(state, GetMemoryChunkSpace(state->memtuples[i])); pfree(state->memtuples[i]); state->memtuples[i] = NULL; } state->memtupdeleted = nremove; /* mark tuplestore as truncated (used for Assert crosschecks only) */ state->truncated = true; /* * If nremove is less than 1/8th memtupcount, just stop here, leaving the * "deleted" slots as NULL. This prevents us from expending O(N^2) time * repeatedly memmove-ing a large pointer array. The worst case space * wastage is pretty small, since it's just pointers and not whole tuples. */ if (nremove < state->memtupcount / 8) return; /* * Slide the array down and readjust pointers. * * In mergejoin's current usage, it's demonstrable that there will always * be exactly one non-removed tuple; so optimize that case. */ if (nremove + 1 == state->memtupcount) state->memtuples[0] = state->memtuples[nremove]; else memmove(state->memtuples, state->memtuples + nremove, (state->memtupcount - nremove) * sizeof(void *)); state->memtupdeleted = 0; state->memtupcount -= nremove; for (i = 0; i < state->readptrcount; i++) { if (!state->readptrs[i].eof_reached) state->readptrs[i].current -= nremove; } }