tstoreReceiver.c 2.0 KB
Newer Older
B
Bruce Momjian 已提交
1 2
/*-------------------------------------------------------------------------
 *
3
 * tstoreReceiver.c
B
Bruce Momjian 已提交
4 5 6 7 8 9 10 11
 *	  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.5 2003/05/06 20:26:27 tgl Exp $
B
Bruce Momjian 已提交
13 14 15 16 17 18 19
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "executor/tstoreReceiver.h"
20

B
Bruce Momjian 已提交
21 22 23 24 25 26 27 28

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

29

B
Bruce Momjian 已提交
30
/*
31
 * Prepare to receive tuples from executor.
B
Bruce Momjian 已提交
32 33
 */
static void
34 35 36
tstoreStartupReceiver(DestReceiver *self, int operation,
					  const char *portalname,
					  TupleDesc typeinfo, List *targetlist)
B
Bruce Momjian 已提交
37
{
38
	/* do nothing */
B
Bruce Momjian 已提交
39 40
}

41 42 43
/*
 * Receive a tuple from the executor and store it in the tuplestore.
 */
B
Bruce Momjian 已提交
44 45 46 47 48 49 50 51 52 53 54
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);
}

55
/*
56
 * Clean up at end of an executor run
57
 */
B
Bruce Momjian 已提交
58
static void
59
tstoreShutdownReceiver(DestReceiver *self)
B
Bruce Momjian 已提交
60
{
61
	/* do nothing */
B
Bruce Momjian 已提交
62 63
}

64 65 66 67 68 69 70 71 72
/*
 * Destroy receiver when done with it
 */
static void
tstoreDestroyReceiver(DestReceiver *self)
{
	pfree(self);
}

73 74 75
/*
 * Initially create a DestReceiver object.
 */
B
Bruce Momjian 已提交
76
DestReceiver *
77 78
CreateTuplestoreDestReceiver(Tuplestorestate *tStore,
							 MemoryContext tContext)
B
Bruce Momjian 已提交
79 80 81 82
{
	TStoreState *self = (TStoreState *) palloc(sizeof(TStoreState));

	self->pub.receiveTuple = tstoreReceiveTuple;
83 84 85 86
	self->pub.startup = tstoreStartupReceiver;
	self->pub.shutdown = tstoreShutdownReceiver;
	self->pub.destroy = tstoreDestroyReceiver;
	self->pub.mydest = Tuplestore;
B
Bruce Momjian 已提交
87

88 89
	self->tstore = tStore;
	self->cxt = tContext;
B
Bruce Momjian 已提交
90 91 92

	return (DestReceiver *) self;
}