Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Typedefs | Functions

tuplestore.h File Reference

#include "executor/tuptable.h"
Include dependency graph for tuplestore.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define tuplestore_donestoring(state)   ((void) 0)

Typedefs

typedef struct Tuplestorestate Tuplestorestate

Functions

Tuplestorestatetuplestore_begin_heap (bool randomAccess, bool interXact, int maxKBytes)
void tuplestore_set_eflags (Tuplestorestate *state, int eflags)
void tuplestore_puttupleslot (Tuplestorestate *state, TupleTableSlot *slot)
void tuplestore_puttuple (Tuplestorestate *state, HeapTuple tuple)
void tuplestore_putvalues (Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
int tuplestore_alloc_read_pointer (Tuplestorestate *state, int eflags)
void tuplestore_select_read_pointer (Tuplestorestate *state, int ptr)
void tuplestore_copy_read_pointer (Tuplestorestate *state, int srcptr, int destptr)
void tuplestore_trim (Tuplestorestate *state)
bool tuplestore_in_memory (Tuplestorestate *state)
bool tuplestore_gettupleslot (Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
bool tuplestore_advance (Tuplestorestate *state, bool forward)
bool tuplestore_ateof (Tuplestorestate *state)
void tuplestore_rescan (Tuplestorestate *state)
void tuplestore_clear (Tuplestorestate *state)
void tuplestore_end (Tuplestorestate *state)

Define Documentation

#define tuplestore_donestoring (   state  )     ((void) 0)

Typedef Documentation

Definition at line 40 of file tuplestore.h.


Function Documentation

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

{
    return (state->status == TSS_INMEM);
}

void tuplestore_puttuple ( Tuplestorestate state,
HeapTuple  tuple 
)
void tuplestore_puttupleslot ( Tuplestorestate state,
TupleTableSlot slot 
)
void tuplestore_putvalues ( Tuplestorestate state,
TupleDesc  tdesc,
Datum values,
bool isnull 
)
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().

{
    int         i;

    if (state->status != TSS_INMEM || state->memtupcount != 0)
        elog(ERROR, "too late to call tuplestore_set_eflags");

    state->readptrs[0].eflags = eflags;
    for (i = 1; i < state->readptrcount; i++)
        eflags |= state->readptrs[i].eflags;
    state->eflags = eflags;
}

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;
    }
}