1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* dest.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* support for various communication destinations - see lib/H/tcop/dest.h
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-09-01 06:40:42 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/tcop/dest.c,v 1.23 1998/09/01 04:32:10 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* INTERFACE ROUTINES
|
|
|
|
* BeginCommand - prepare destination for tuples of the given type
|
|
|
|
* EndCommand - tell destination that no more tuples will arrive
|
1998-05-07 01:51:16 +02:00
|
|
|
* NullCommand - tell dest that an empty query string was recognized
|
|
|
|
* ReadyForQuery - tell dest that we are ready for a new query
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* These routines do the appropriate work before and after
|
|
|
|
* tuples are returned by a query to keep the backend and the
|
|
|
|
* "destination" portals synchronized.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <stdio.h> /* for sprintf() */
|
1997-01-10 21:19:49 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/htup.h"
|
1998-01-26 02:42:53 +01:00
|
|
|
#include "libpq/libpq.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "access/printtup.h"
|
|
|
|
#include "utils/portal.h"
|
|
|
|
#include "utils/palloc.h"
|
|
|
|
|
|
|
|
#include "executor/executor.h"
|
|
|
|
|
|
|
|
#include "tcop/dest.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "utils/mcxt.h"
|
|
|
|
|
|
|
|
#include "commands/async.h"
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static char CommandInfo[32] = {0};
|
1997-08-19 23:40:56 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* output functions
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1997-01-08 09:39:10 +01:00
|
|
|
static void
|
|
|
|
donothing(HeapTuple tuple, TupleDesc attrdesc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void spi_printtup(HeapTuple tuple, TupleDesc tupdesc);
|
1997-08-29 11:06:27 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
void (*
|
|
|
|
DestToFunction(CommandDest dest)) (HeapTuple, TupleDesc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (dest)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
case RemoteInternal:
|
1997-09-08 04:41:22 +02:00
|
|
|
return printtup_internal;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case Remote:
|
|
|
|
return printtup;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case Local:
|
|
|
|
return be_printtup;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case Debug:
|
|
|
|
return debugtup;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case SPI:
|
|
|
|
return spi_printtup;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case None:
|
|
|
|
default:
|
|
|
|
return donothing;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* never gets here, but DECstation lint appears to be stupid...
|
|
|
|
*/
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
return donothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* EndCommand - tell destination that no more tuples will arrive
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
EndCommand(char *commandTag, CommandDest dest)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char buf[64];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (dest)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case RemoteInternal:
|
|
|
|
case Remote:
|
|
|
|
/* ----------------
|
|
|
|
* tell the fe that the query is over
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
pq_putnchar("C", 1);
|
|
|
|
sprintf(buf, "%s%s", commandTag, CommandInfo);
|
|
|
|
CommandInfo[0] = 0;
|
|
|
|
pq_putstr(buf);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Local:
|
|
|
|
case Debug:
|
|
|
|
case None:
|
|
|
|
default:
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are necessary to sync communications between fe/be processes doing
|
|
|
|
* COPY rel TO stdout
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
|
|
|
* or
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* COPY rel FROM stdin
|
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* NOTE: the message code letters are changed at protocol version 2.0
|
|
|
|
* to eliminate possible confusion with data tuple messages.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
SendCopyBegin(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
|
|
|
|
pq_putnchar("H", 1); /* new way */
|
|
|
|
else
|
|
|
|
pq_putnchar("B", 1); /* old way */
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-11-10 04:06:38 +01:00
|
|
|
ReceiveCopyBegin(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
|
|
|
|
pq_putnchar("G", 1); /* new way */
|
|
|
|
else
|
|
|
|
pq_putnchar("D", 1); /* old way */
|
|
|
|
/* We *must* flush here to ensure FE knows it can send. */
|
1997-09-07 07:04:48 +02:00
|
|
|
pq_flush();
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1998-05-07 01:51:16 +02:00
|
|
|
* NullCommand - tell dest that an empty query string was recognized
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1998-05-07 01:51:16 +02:00
|
|
|
* In FE/BE protocol version 1.0, this hack is necessary to support
|
|
|
|
* libpq's crufty way of determining whether a multiple-command
|
|
|
|
* query string is done. In protocol 2.0 it's probably not really
|
|
|
|
* necessary to distinguish empty queries anymore, but we still do it
|
|
|
|
* for backwards compatibility with 1.0.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
NullCommand(CommandDest dest)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (dest)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
case RemoteInternal:
|
|
|
|
case Remote:
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
/* ----------------
|
1998-05-07 01:51:16 +02:00
|
|
|
* tell the fe that we saw an empty query string
|
1997-09-08 04:41:22 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
pq_putstr("I");
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Local:
|
|
|
|
case Debug:
|
|
|
|
case None:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* ReadyForQuery - tell dest that we are ready for a new query
|
|
|
|
*
|
|
|
|
* The ReadyForQuery message is sent in protocol versions 2.0 and up
|
|
|
|
* so that the FE can tell when we are done processing a query string.
|
|
|
|
*
|
|
|
|
* Note that by flushing the stdio buffer here, we can avoid doing it
|
|
|
|
* most other places and thus reduce the number of separate packets sent.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ReadyForQuery(CommandDest dest)
|
|
|
|
{
|
|
|
|
switch (dest)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
case RemoteInternal:
|
|
|
|
case Remote:
|
1998-05-07 01:51:16 +02:00
|
|
|
{
|
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
|
|
|
|
pq_putnchar("Z", 1);
|
|
|
|
/* Flush output at end of cycle in any case. */
|
1997-09-08 04:41:22 +02:00
|
|
|
pq_flush();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Local:
|
|
|
|
case Debug:
|
|
|
|
case None:
|
|
|
|
default:
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* BeginCommand - prepare destination for tuples of the given type
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BeginCommand(char *pname,
|
1997-09-07 07:04:48 +02:00
|
|
|
int operation,
|
|
|
|
TupleDesc tupdesc,
|
|
|
|
bool isIntoRel,
|
|
|
|
bool isIntoPortal,
|
|
|
|
char *tag,
|
|
|
|
CommandDest dest)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
PortalEntry *entry;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_attribute *attrs = tupdesc->attrs;
|
1997-09-08 04:41:22 +02:00
|
|
|
int natts = tupdesc->natts;
|
|
|
|
int i;
|
|
|
|
char *p;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (dest)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case RemoteInternal:
|
|
|
|
case Remote:
|
|
|
|
/* ----------------
|
|
|
|
* if this is a "retrieve portal" query, just return
|
|
|
|
* because nothing needs to be sent to the fe.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
CommandInfo[0] = 0;
|
|
|
|
if (isIntoPortal)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if portal name not specified for remote query,
|
|
|
|
* use the "blank" portal.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (pname == NULL)
|
|
|
|
pname = "blank";
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* send fe info on tuples we're about to send
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
pq_putnchar("P", 1);/* new portal.. */
|
|
|
|
pq_putstr(pname); /* portal name */
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if this is a retrieve, then we send back the tuple
|
|
|
|
* descriptor of the tuples. "retrieve into" is an
|
|
|
|
* exception because no tuples are returned in that case.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (operation == CMD_SELECT && !isIntoRel)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
pq_putnchar("T", 1); /* type info to follow.. */
|
|
|
|
pq_putint(natts, 2); /* number of attributes in tuples */
|
|
|
|
|
|
|
|
for (i = 0; i < natts; ++i)
|
|
|
|
{
|
1998-07-20 18:57:18 +02:00
|
|
|
pq_putstr(attrs[i]->attname.data);
|
1997-12-06 23:57:36 +01:00
|
|
|
pq_putint((int) attrs[i]->atttypid, sizeof(attrs[i]->atttypid));
|
|
|
|
pq_putint(attrs[i]->attlen, sizeof(attrs[i]->attlen));
|
1998-05-19 20:05:58 +02:00
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
|
|
|
|
pq_putint(attrs[i]->atttypmod, sizeof(attrs[i]->atttypmod));
|
1997-09-08 04:41:22 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Local:
|
|
|
|
/* ----------------
|
|
|
|
* prepare local portal buffer for query results
|
|
|
|
* and setup result for PQexec()
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
entry = be_currentportal();
|
|
|
|
if (pname != NULL)
|
|
|
|
pbuf_setportalinfo(entry, pname);
|
|
|
|
|
|
|
|
if (operation == CMD_SELECT && !isIntoRel)
|
|
|
|
{
|
|
|
|
be_typeinit(entry, tupdesc, natts);
|
|
|
|
p = (char *) palloc(strlen(entry->name) + 2);
|
|
|
|
p[0] = 'P';
|
|
|
|
strcpy(p + 1, entry->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = (char *) palloc(strlen(tag) + 2);
|
|
|
|
p[0] = 'C';
|
|
|
|
strcpy(p + 1, tag);
|
|
|
|
}
|
|
|
|
entry->result = p;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Debug:
|
|
|
|
/* ----------------
|
|
|
|
* show the return type of the tuples
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (pname == NULL)
|
|
|
|
pname = "blank";
|
|
|
|
|
|
|
|
showatts(pname, tupdesc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case None:
|
|
|
|
default:
|
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-09-07 07:04:48 +02:00
|
|
|
UpdateCommandInfo(int operation, Oid lastoid, uint32 tuples)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (operation)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case CMD_INSERT:
|
|
|
|
if (tuples > 1)
|
|
|
|
lastoid = InvalidOid;
|
|
|
|
sprintf(CommandInfo, " %u %u", lastoid, tuples);
|
|
|
|
break;
|
|
|
|
case CMD_DELETE:
|
|
|
|
case CMD_UPDATE:
|
|
|
|
sprintf(CommandInfo, " %u", tuples);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CommandInfo[0] = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
return;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|