dest.h 5.2 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * dest.h
4 5 6 7
 *	  support for communication destinations
 *
 * Whenever the backend executes a query, the results
 * have to go someplace.
8
 *
9
 *	  - stdout is the destination only when we are running a
10
 *		standalone backend (no postmaster) and are returning results
11
 *		back to an interactive user.
12
 *
13
 *	  - a remote process is the destination when we are
14 15
 *		running a backend with a frontend and the frontend executes
 *		PQexec() or PQfn().  In this case, the results are sent
16
 *		to the frontend via the functions in backend/libpq.
17
 *
18
 *	  - DestNone is the destination when the system executes
19
 *		a query internally.  The results are discarded.
20
 *
21 22
 * dest.c defines three functions that implement destination management:
 *
23
 * BeginCommand: initialize the destination at start of command.
24
 * CreateDestReceiver: return a pointer to a struct of destination-specific
25
 * receiver functions.
26 27 28 29
 * EndCommand: clean up the destination at end of command.
 *
 * BeginCommand/EndCommand are executed once per received SQL query.
 *
30 31
 * CreateDestReceiver returns a receiver object appropriate to the specified
 * destination.  The executor, as well as utility statements that can return
B
Bruce Momjian 已提交
32
 * tuples, are passed the resulting DestReceiver* pointer.	Each executor run
33
 * or utility execution calls the receiver's rStartup method, then the
34
 * receiveSlot method (zero or more times), then the rShutdown method.
35
 * The same receiver object may be re-used multiple times; eventually it is
36
 * destroyed by calling its rDestroy method.
37
 *
38 39
 * The DestReceiver object returned by CreateDestReceiver may be a statically
 * allocated object (for destination types that require no local state),
40
 * in which case rDestroy is a no-op.  Alternatively it can be a palloc'd
41 42 43
 * object that has DestReceiver as its first field and contains additional
 * fields (see printtup.c for an example).	These additional fields are then
 * accessible to the DestReceiver functions by casting the DestReceiver*
44
 * pointer passed to them.	The palloc'd object is pfree'd by the rDestroy
B
Bruce Momjian 已提交
45
 * method.	Note that the caller of CreateDestReceiver should take care to
46 47 48 49
 * do so in a memory context that is long-lived enough for the receiver
 * object not to disappear while still needed.
 *
 * Special provision: None_Receiver is a permanently available receiver
50
 * object for the DestNone destination.  This avoids useless creation/destroy
51
 * calls in portal and cursor manipulations.
52
 *
53
 *
P
 
PostgreSQL Daemon 已提交
54
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
55
 * Portions Copyright (c) 1994, Regents of the University of California
56
 *
57
 * $PostgreSQL: pgsql/src/include/tcop/dest.h,v 1.48 2005/11/03 17:11:40 alvherre Exp $
58 59 60 61 62 63
 *
 *-------------------------------------------------------------------------
 */
#ifndef DEST_H
#define DEST_H

64
#include "executor/tuptable.h"
65

66 67 68 69 70

/* buffer size to use for command completion tags */
#define COMPLETION_TAG_BUFSIZE	64


71
/* ----------------
72 73
 *		CommandDest is a simplistic means of identifying the desired
 *		destination.  Someday this will probably need to be improved.
74
 *
75 76
 * Note: only the values DestNone, DestDebug, DestRemote are legal for the
 * global variable whereToSendOutput.	The other values may be used
77
 * as the destination for individual commands.
78 79
 * ----------------
 */
80 81
typedef enum
{
82 83 84 85 86 87
	DestNone,				/* results are discarded */
	DestDebug,				/* results go to debugging output */
	DestRemote,				/* results sent to frontend process */
	DestRemoteExecute,		/* sent to frontend, in Execute command */
	DestSPI,				/* results sent to SPI manager */
	DestTuplestore			/* results sent to Tuplestore */
88
} CommandDest;
89

90 91 92 93
/* ----------------
 *		DestReceiver is a base type for destination-specific local state.
 *		In the simplest cases, there is no state info, just the function
 *		pointers that the executor must call.
94
 *
95 96
 * Note: the receiveSlot routine must be passed a slot containing a TupleDesc
 * identical to the one given to the rStartup routine.
97 98 99
 * ----------------
 */
typedef struct _DestReceiver DestReceiver;
100

B
Bruce Momjian 已提交
101 102
struct _DestReceiver
{
103
	/* Called for each tuple to be output: */
104
	void		(*receiveSlot) (TupleTableSlot *slot,
B
Bruce Momjian 已提交
105
											DestReceiver *self);
106
	/* Per-executor-run initialization and shutdown: */
107
	void		(*rStartup) (DestReceiver *self,
108 109
										 int operation,
										 TupleDesc typeinfo);
110
	void		(*rShutdown) (DestReceiver *self);
111
	/* Destroy the receiver object itself (if dynamically allocated) */
112
	void		(*rDestroy) (DestReceiver *self);
113
	/* CommandDest code for this receiver */
B
Bruce Momjian 已提交
114
	CommandDest mydest;
115 116
	/* Private fields might appear beyond this point... */
};
117

118
extern DestReceiver *None_Receiver;		/* permanent receiver for DestNone */
119

120 121 122 123
/* This is a forward reference to utils/portal.h */

typedef struct PortalData *Portal;

124
/* The primary destination management functions */
125

126
extern void BeginCommand(const char *commandTag, CommandDest dest);
127
extern DestReceiver *CreateDestReceiver(CommandDest dest, Portal portal);
128
extern void EndCommand(const char *commandTag, CommandDest dest);
129 130 131

/* Additional functions that go with destination management, more or less. */

132
extern void NullCommand(CommandDest dest);
B
Bruce Momjian 已提交
133
extern void ReadyForQuery(CommandDest dest);
134

135
#endif   /* DEST_H */