1 /*-------------------------------------------------------------------------
4 * an implementation of DestReceiver that stores the result tuples in
8 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/executor/tstoreReceiver.c,v 1.3 2003/05/02 20:54:34 tgl Exp $
14 *-------------------------------------------------------------------------
19 #include "executor/tstoreReceiver.h"
20 #include "utils/memutils.h"
21 #include "utils/portal.h"
26 Tuplestorestate *tstore;
32 * Prepare to receive tuples from executor.
34 * XXX: As currently implemented, this routine is a hack: there should
35 * be no tie between this code and the portal system. Instead, the
36 * receiver function that is part of DestFunction should be passed a
37 * QueryDesc, so that the call site of ExecutorRun can "sub-class"
38 * QueryDesc and pass in any necessary addition information (in this
39 * case, the Tuplestore to use).
42 tstoreSetupReceiver(DestReceiver *self, int operation,
43 const char *portalname, TupleDesc typeinfo)
45 TStoreState *myState = (TStoreState *) self;
47 /* Should only be called within a suitably-prepped portal */
48 if (CurrentPortal == NULL ||
49 CurrentPortal->holdStore == NULL)
50 elog(ERROR, "Tuplestore destination used in wrong context");
52 /* Debug check: make sure portal's result tuple desc is correct */
53 Assert(CurrentPortal->tupDesc != NULL);
54 Assert(equalTupleDescs(CurrentPortal->tupDesc, typeinfo));
56 myState->tstore = CurrentPortal->holdStore;
57 myState->cxt = CurrentPortal->holdContext;
61 * Receive a tuple from the executor and store it in the tuplestore.
64 tstoreReceiveTuple(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
66 TStoreState *myState = (TStoreState *) self;
67 MemoryContext oldcxt = MemoryContextSwitchTo(myState->cxt);
69 tuplestore_puttuple(myState->tstore, tuple);
71 MemoryContextSwitchTo(oldcxt);
78 tstoreCleanupReceiver(DestReceiver *self)
84 * Initially create a DestReceiver object.
87 tstoreReceiverCreateDR(void)
89 TStoreState *self = (TStoreState *) palloc(sizeof(TStoreState));
91 self->pub.receiveTuple = tstoreReceiveTuple;
92 self->pub.setup = tstoreSetupReceiver;
93 self->pub.cleanup = tstoreCleanupReceiver;
98 return (DestReceiver *) self;