Header And Logo

PostgreSQL
| The world's most advanced open source database.

dest.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * dest.h
00004  *    support for communication destinations
00005  *
00006  * Whenever the backend executes a query that returns tuples, the results
00007  * have to go someplace.  For example:
00008  *
00009  *    - stdout is the destination only when we are running a
00010  *      standalone backend (no postmaster) and are returning results
00011  *      back to an interactive user.
00012  *
00013  *    - a remote process is the destination when we are
00014  *      running a backend with a frontend and the frontend executes
00015  *      PQexec() or PQfn().  In this case, the results are sent
00016  *      to the frontend via the functions in backend/libpq.
00017  *
00018  *    - DestNone is the destination when the system executes
00019  *      a query internally.  The results are discarded.
00020  *
00021  * dest.c defines three functions that implement destination management:
00022  *
00023  * BeginCommand: initialize the destination at start of command.
00024  * CreateDestReceiver: return a pointer to a struct of destination-specific
00025  * receiver functions.
00026  * EndCommand: clean up the destination at end of command.
00027  *
00028  * BeginCommand/EndCommand are executed once per received SQL query.
00029  *
00030  * CreateDestReceiver returns a receiver object appropriate to the specified
00031  * destination.  The executor, as well as utility statements that can return
00032  * tuples, are passed the resulting DestReceiver* pointer.  Each executor run
00033  * or utility execution calls the receiver's rStartup method, then the
00034  * receiveSlot method (zero or more times), then the rShutdown method.
00035  * The same receiver object may be re-used multiple times; eventually it is
00036  * destroyed by calling its rDestroy method.
00037  *
00038  * In some cases, receiver objects require additional parameters that must
00039  * be passed to them after calling CreateDestReceiver.  Since the set of
00040  * parameters varies for different receiver types, this is not handled by
00041  * this module, but by direct calls from the calling code to receiver type
00042  * specific functions.
00043  *
00044  * The DestReceiver object returned by CreateDestReceiver may be a statically
00045  * allocated object (for destination types that require no local state),
00046  * in which case rDestroy is a no-op.  Alternatively it can be a palloc'd
00047  * object that has DestReceiver as its first field and contains additional
00048  * fields (see printtup.c for an example).  These additional fields are then
00049  * accessible to the DestReceiver functions by casting the DestReceiver*
00050  * pointer passed to them.  The palloc'd object is pfree'd by the rDestroy
00051  * method.  Note that the caller of CreateDestReceiver should take care to
00052  * do so in a memory context that is long-lived enough for the receiver
00053  * object not to disappear while still needed.
00054  *
00055  * Special provision: None_Receiver is a permanently available receiver
00056  * object for the DestNone destination.  This avoids useless creation/destroy
00057  * calls in portal and cursor manipulations.
00058  *
00059  *
00060  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00061  * Portions Copyright (c) 1994, Regents of the University of California
00062  *
00063  * src/include/tcop/dest.h
00064  *
00065  *-------------------------------------------------------------------------
00066  */
00067 #ifndef DEST_H
00068 #define DEST_H
00069 
00070 #include "executor/tuptable.h"
00071 
00072 
00073 /* buffer size to use for command completion tags */
00074 #define COMPLETION_TAG_BUFSIZE  64
00075 
00076 
00077 /* ----------------
00078  *      CommandDest is a simplistic means of identifying the desired
00079  *      destination.  Someday this will probably need to be improved.
00080  *
00081  * Note: only the values DestNone, DestDebug, DestRemote are legal for the
00082  * global variable whereToSendOutput.   The other values may be used
00083  * as the destination for individual commands.
00084  * ----------------
00085  */
00086 typedef enum
00087 {
00088     DestNone,                   /* results are discarded */
00089     DestDebug,                  /* results go to debugging output */
00090     DestRemote,                 /* results sent to frontend process */
00091     DestRemoteExecute,          /* sent to frontend, in Execute command */
00092     DestSPI,                    /* results sent to SPI manager */
00093     DestTuplestore,             /* results sent to Tuplestore */
00094     DestIntoRel,                /* results sent to relation (SELECT INTO) */
00095     DestCopyOut,                /* results sent to COPY TO code */
00096     DestSQLFunction,            /* results sent to SQL-language func mgr */
00097     DestTransientRel            /* results sent to transient relation */
00098 } CommandDest;
00099 
00100 /* ----------------
00101  *      DestReceiver is a base type for destination-specific local state.
00102  *      In the simplest cases, there is no state info, just the function
00103  *      pointers that the executor must call.
00104  *
00105  * Note: the receiveSlot routine must be passed a slot containing a TupleDesc
00106  * identical to the one given to the rStartup routine.
00107  * ----------------
00108  */
00109 typedef struct _DestReceiver DestReceiver;
00110 
00111 struct _DestReceiver
00112 {
00113     /* Called for each tuple to be output: */
00114     void        (*receiveSlot) (TupleTableSlot *slot,
00115                                             DestReceiver *self);
00116     /* Per-executor-run initialization and shutdown: */
00117     void        (*rStartup) (DestReceiver *self,
00118                                          int operation,
00119                                          TupleDesc typeinfo);
00120     void        (*rShutdown) (DestReceiver *self);
00121     /* Destroy the receiver object itself (if dynamically allocated) */
00122     void        (*rDestroy) (DestReceiver *self);
00123     /* CommandDest code for this receiver */
00124     CommandDest mydest;
00125     /* Private fields might appear beyond this point... */
00126 };
00127 
00128 extern DestReceiver *None_Receiver;     /* permanent receiver for DestNone */
00129 
00130 /* The primary destination management functions */
00131 
00132 extern void BeginCommand(const char *commandTag, CommandDest dest);
00133 extern DestReceiver *CreateDestReceiver(CommandDest dest);
00134 extern void EndCommand(const char *commandTag, CommandDest dest);
00135 
00136 /* Additional functions that go with destination management, more or less. */
00137 
00138 extern void NullCommand(CommandDest dest);
00139 extern void ReadyForQuery(CommandDest dest);
00140 
00141 #endif   /* DEST_H */