dest.h 5.3 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
 *
 *	  - None 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 34
 * or utility execution calls the receiver's rStartup method, then the
 * receiveTuple 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 50 51
 * 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
 * object for the None destination.  This avoids useless creation/destroy
 * calls in portal and cursor manipulations.
52
 *
53
 *
B
Bruce Momjian 已提交
54
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
55
 * Portions Copyright (c) 1994, Regents of the University of California
56
 *
B
Bruce Momjian 已提交
57
 * $PostgreSQL: pgsql/src/include/tcop/dest.h,v 1.44 2004/08/29 04:13:10 momjian Exp $
58 59 60 61 62 63
 *
 *-------------------------------------------------------------------------
 */
#ifndef DEST_H
#define DEST_H

64
#include "access/htup.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
 *
 * Note: only the values None, Debug, Remote are legal for the global
B
Bruce Momjian 已提交
76
 * variable whereToSendOutput.	The other values may be used
77
 * as the destination for individual commands.
78 79
 * ----------------
 */
80 81
typedef enum
{
82 83 84
	None,						/* results are discarded */
	Debug,						/* results go to debugging output */
	Remote,						/* results sent to frontend process */
85
	RemoteExecute,				/* sent to frontend, in Execute command */
86 87
	SPI,						/* results sent to SPI manager */
	Tuplestore					/* 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
 *
 * Note: the receiveTuple routine must be passed a TupleDesc identical to the
96
 * one given to the rStartup routine.  The reason for passing it again is just
97 98
 * that some destinations would otherwise need dynamic state merely to
 * remember the tupledesc pointer.
99 100 101
 * ----------------
 */
typedef struct _DestReceiver DestReceiver;
102

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

B
Bruce Momjian 已提交
121
extern DestReceiver *None_Receiver;		/* permanent receiver for None */
122

123 124 125 126
/* This is a forward reference to utils/portal.h */

typedef struct PortalData *Portal;

127
/* The primary destination management functions */
128

129
extern void BeginCommand(const char *commandTag, CommandDest dest);
130
extern DestReceiver *CreateDestReceiver(CommandDest dest, Portal portal);
131
extern void EndCommand(const char *commandTag, CommandDest dest);
132 133 134

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

135
extern void NullCommand(CommandDest dest);
B
Bruce Momjian 已提交
136
extern void ReadyForQuery(CommandDest dest);
137

138
#endif   /* DEST_H */