tstoreReceiver.c 2.6 KB
Newer Older
B
Bruce Momjian 已提交
1 2 3 4 5 6 7 8 9 10 11
/*-------------------------------------------------------------------------
 *
 * tstore_receiver.c
 *	  an implementation of DestReceiver that stores the result tuples in
 *	  a Tuplestore
 *
 *
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
12
 *	  $Header: /cvsroot/pgsql/src/backend/executor/tstoreReceiver.c,v 1.4 2003/05/06 00:20:31 tgl Exp $
B
Bruce Momjian 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "executor/tstoreReceiver.h"
#include "utils/memutils.h"
#include "utils/portal.h"

typedef struct
{
	DestReceiver		pub;
	Tuplestorestate    *tstore;
	MemoryContext		cxt;
} TStoreState;

30

B
Bruce Momjian 已提交
31
/*
32
 * Prepare to receive tuples from executor.
B
Bruce Momjian 已提交
33 34 35 36 37 38 39 40 41 42
 *
 * XXX: As currently implemented, this routine is a hack: there should
 * be no tie between this code and the portal system. Instead, the
 * receiver function that is part of DestFunction should be passed a
 * QueryDesc, so that the call site of ExecutorRun can "sub-class"
 * QueryDesc and pass in any necessary addition information (in this
 * case, the Tuplestore to use).
 */
static void
tstoreSetupReceiver(DestReceiver *self, int operation,
43 44
					const char *portalname,
					TupleDesc typeinfo, List *targetlist)
B
Bruce Momjian 已提交
45 46 47
{
	TStoreState *myState = (TStoreState *) self;

48 49 50 51
	/* Should only be called within a suitably-prepped portal */
	if (CurrentPortal == NULL ||
		CurrentPortal->holdStore == NULL)
		elog(ERROR, "Tuplestore destination used in wrong context");
B
Bruce Momjian 已提交
52

53 54 55
	/* Debug check: make sure portal's result tuple desc is correct */
	Assert(CurrentPortal->tupDesc != NULL);
	Assert(equalTupleDescs(CurrentPortal->tupDesc, typeinfo));
B
Bruce Momjian 已提交
56

57 58
	myState->tstore = CurrentPortal->holdStore;
	myState->cxt = CurrentPortal->holdContext;
B
Bruce Momjian 已提交
59 60
}

61 62 63
/*
 * Receive a tuple from the executor and store it in the tuplestore.
 */
B
Bruce Momjian 已提交
64 65 66 67 68 69 70 71 72 73 74
static void
tstoreReceiveTuple(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
{
	TStoreState *myState = (TStoreState *) self;
	MemoryContext oldcxt = MemoryContextSwitchTo(myState->cxt);

	tuplestore_puttuple(myState->tstore, tuple);

	MemoryContextSwitchTo(oldcxt);
}

75 76 77
/*
 * Clean up
 */
B
Bruce Momjian 已提交
78 79 80
static void
tstoreCleanupReceiver(DestReceiver *self)
{
81
	/* do nothing */
B
Bruce Momjian 已提交
82 83
}

84 85 86
/*
 * Initially create a DestReceiver object.
 */
B
Bruce Momjian 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
DestReceiver *
tstoreReceiverCreateDR(void)
{
	TStoreState *self = (TStoreState *) palloc(sizeof(TStoreState));

	self->pub.receiveTuple = tstoreReceiveTuple;
	self->pub.setup = tstoreSetupReceiver;
	self->pub.cleanup = tstoreCleanupReceiver;

	self->tstore = NULL;
	self->cxt = NULL;

	return (DestReceiver *) self;
}