tstoreReceiver.c 1.9 KB
Newer Older
B
Bruce Momjian 已提交
1 2
/*-------------------------------------------------------------------------
 *
3
 * tstoreReceiver.c
B
Bruce Momjian 已提交
4 5 6 7
 *	  an implementation of DestReceiver that stores the result tuples in
 *	  a Tuplestore
 *
 *
P
 
PostgreSQL Daemon 已提交
8
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
B
Bruce Momjian 已提交
9 10 11
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
12
 *	  $PostgreSQL: pgsql/src/backend/executor/tstoreReceiver.c,v 1.14 2005/03/16 21:38:08 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

typedef struct
{
B
Bruce Momjian 已提交
24 25 26
	DestReceiver pub;
	Tuplestorestate *tstore;
	MemoryContext cxt;
27
} TStoreState;
B
Bruce Momjian 已提交
28

29

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

39 40 41
/*
 * Receive a tuple from the executor and store it in the tuplestore.
 */
B
Bruce Momjian 已提交
42
static void
43
tstoreReceiveSlot(TupleTableSlot *slot, DestReceiver *self)
B
Bruce Momjian 已提交
44 45 46 47
{
	TStoreState *myState = (TStoreState *) self;
	MemoryContext oldcxt = MemoryContextSwitchTo(myState->cxt);

48
	tuplestore_puttuple(myState->tstore, ExecFetchSlotTuple(slot));
B
Bruce Momjian 已提交
49 50 51 52

	MemoryContextSwitchTo(oldcxt);
}

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

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

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

80
	self->pub.receiveSlot = tstoreReceiveSlot;
81 82 83
	self->pub.rStartup = tstoreStartupReceiver;
	self->pub.rShutdown = tstoreShutdownReceiver;
	self->pub.rDestroy = tstoreDestroyReceiver;
84
	self->pub.mydest = Tuplestore;
B
Bruce Momjian 已提交
85

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

	return (DestReceiver *) self;
}