1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* pquery.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* POSTGRES process query command code
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2001-03-22 07:16:21 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/tcop/pquery.c,v 1.44 2001/03/22 06:16:17 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-11-08 07:02:30 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-06-05 00:08:53 +02:00
|
|
|
#include "commands/command.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "executor/execdefs.h"
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "tcop/pquery.h"
|
2000-07-17 05:05:41 +02:00
|
|
|
#include "utils/memutils.h"
|
1998-08-25 23:24:12 +02:00
|
|
|
#include "utils/ps_status.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-07-17 05:05:41 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *CreateOperationTag(int operationType);
|
1997-08-19 23:40:56 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* CreateQueryDesc
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
QueryDesc *
|
1997-09-08 23:56:23 +02:00
|
|
|
CreateQueryDesc(Query *parsetree,
|
|
|
|
Plan *plantree,
|
1997-09-07 07:04:48 +02:00
|
|
|
CommandDest dest)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
qd->operation = parsetree->commandType; /* operation */
|
|
|
|
qd->parsetree = parsetree; /* parse tree */
|
|
|
|
qd->plantree = plantree; /* plan */
|
|
|
|
qd->dest = dest; /* output dest */
|
|
|
|
return qd;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* CreateExecutorState
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Note: this may someday take parameters -cim 9/18/89
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1998-02-26 05:46:47 +01:00
|
|
|
EState *
|
1996-11-10 04:06:38 +01:00
|
|
|
CreateExecutorState(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
EState *state;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* create a new executor state
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
state = makeNode(EState);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* initialize the Executor State structure
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
state->es_direction = ForwardScanDirection;
|
|
|
|
state->es_range_table = NIL;
|
|
|
|
|
2000-11-12 01:37:02 +01:00
|
|
|
state->es_result_relations = NULL;
|
|
|
|
state->es_num_result_relations = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
state->es_result_relation_info = NULL;
|
2000-11-12 01:37:02 +01:00
|
|
|
|
|
|
|
state->es_junkFilter = NULL;
|
|
|
|
|
2000-08-22 06:06:22 +02:00
|
|
|
state->es_into_relation_descriptor = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
state->es_param_list_info = NULL;
|
1998-02-13 04:42:32 +01:00
|
|
|
state->es_param_exec_vals = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
state->es_tupleTable = NULL;
|
|
|
|
|
2000-08-22 06:06:22 +02:00
|
|
|
state->es_query_cxt = CurrentMemoryContext;
|
|
|
|
|
|
|
|
state->es_per_tuple_exprcontext = NULL;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* return the executor state structure
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
return state;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* CreateOperationTag
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* utility to get a string representation of the
|
|
|
|
* query operation.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
static char *
|
1996-07-09 08:22:35 +02:00
|
|
|
CreateOperationTag(int operationType)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *tag;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (operationType)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case CMD_SELECT:
|
|
|
|
tag = "SELECT";
|
|
|
|
break;
|
|
|
|
case CMD_INSERT:
|
|
|
|
tag = "INSERT";
|
|
|
|
break;
|
|
|
|
case CMD_DELETE:
|
|
|
|
tag = "DELETE";
|
|
|
|
break;
|
|
|
|
case CMD_UPDATE:
|
|
|
|
tag = "UPDATE";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(DEBUG, "CreateOperationTag: unknown operation type %d",
|
|
|
|
operationType);
|
2000-06-28 05:33:33 +02:00
|
|
|
tag = "???";
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
2000-06-28 05:33:33 +02:00
|
|
|
* PreparePortal
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-06-28 05:33:33 +02:00
|
|
|
Portal
|
|
|
|
PreparePortal(char *portalName)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Portal portal;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Check for already-in-use portal name.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-06-05 00:08:53 +02:00
|
|
|
portal = GetPortalByName(portalName);
|
|
|
|
if (PortalIsValid(portal))
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Should we raise an error rather than closing the old
|
|
|
|
* portal?
|
|
|
|
*/
|
2000-06-05 00:08:53 +02:00
|
|
|
elog(NOTICE, "Closing pre-existing portal \"%s\"",
|
1997-09-07 07:04:48 +02:00
|
|
|
portalName);
|
2000-06-05 00:08:53 +02:00
|
|
|
PortalDrop(&portal);
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Create the new portal.
|
2000-06-05 00:08:53 +02:00
|
|
|
*/
|
2000-06-28 05:33:33 +02:00
|
|
|
portal = CreatePortal(portalName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-28 05:33:33 +02:00
|
|
|
return portal;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
2000-06-28 05:33:33 +02:00
|
|
|
* ProcessQuery
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-06-28 05:33:33 +02:00
|
|
|
* Execute a plan, the non-parallel version
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
2000-06-28 05:33:33 +02:00
|
|
|
void
|
|
|
|
ProcessQuery(Query *parsetree,
|
|
|
|
Plan *plan,
|
|
|
|
CommandDest dest)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-28 05:33:33 +02:00
|
|
|
int operation = parsetree->commandType;
|
|
|
|
char *tag;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isRetrieveIntoPortal;
|
|
|
|
bool isRetrieveIntoRelation;
|
|
|
|
char *intoName = NULL;
|
2000-08-22 06:06:22 +02:00
|
|
|
Portal portal = NULL;
|
|
|
|
MemoryContext oldContext = NULL;
|
2000-06-28 05:33:33 +02:00
|
|
|
QueryDesc *queryDesc;
|
|
|
|
EState *state;
|
|
|
|
TupleDesc attinfo;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-04 03:44:38 +02:00
|
|
|
set_ps_display(tag = CreateOperationTag(operation));
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* initialize portal/into relation status
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
isRetrieveIntoPortal = false;
|
|
|
|
isRetrieveIntoRelation = false;
|
|
|
|
|
|
|
|
if (operation == CMD_SELECT)
|
|
|
|
{
|
2000-06-28 05:33:33 +02:00
|
|
|
if (parsetree->isPortal)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
isRetrieveIntoPortal = true;
|
2000-06-28 05:33:33 +02:00
|
|
|
intoName = parsetree->into;
|
|
|
|
if (parsetree->isBinary)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For internal format portals, we change Remote
|
|
|
|
* (externalized form) to RemoteInternal (internalized
|
|
|
|
* form)
|
|
|
|
*/
|
2000-06-28 05:33:33 +02:00
|
|
|
dest = RemoteInternal;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
2000-06-28 05:33:33 +02:00
|
|
|
else if (parsetree->into != NULL)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
/* select into table */
|
|
|
|
isRetrieveIntoRelation = true;
|
|
|
|
}
|
2000-06-28 05:33:33 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* If retrieving into a portal, set up the portal and copy the
|
|
|
|
* parsetree and plan into its memory context.
|
2000-06-28 05:33:33 +02:00
|
|
|
*/
|
|
|
|
if (isRetrieveIntoPortal)
|
|
|
|
{
|
|
|
|
portal = PreparePortal(intoName);
|
2000-08-22 06:06:22 +02:00
|
|
|
oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
|
2000-06-28 05:33:33 +02:00
|
|
|
parsetree = copyObject(parsetree);
|
|
|
|
plan = copyObject(plan);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-08-22 06:06:22 +02:00
|
|
|
/*
|
|
|
|
* We stay in portal's memory context for now, so that query desc,
|
|
|
|
* EState, and plan startup info are also allocated in the portal
|
|
|
|
* context.
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Now we can create the QueryDesc object.
|
2000-06-28 05:33:33 +02:00
|
|
|
*/
|
|
|
|
queryDesc = CreateQueryDesc(parsetree, plan, dest);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* When performing a retrieve into, we override the normal
|
|
|
|
* communication destination during the processing of the the query.
|
|
|
|
* This only affects the tuple-output function - the correct
|
|
|
|
* destination will still see BeginCommand() and EndCommand()
|
|
|
|
* messages.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (isRetrieveIntoRelation)
|
2001-02-27 23:07:34 +01:00
|
|
|
queryDesc->dest = None;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* create a default executor state.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
state = CreateExecutorState();
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* call ExecStart to prepare the plan for execution
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
attinfo = ExecutorStart(queryDesc, state);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* report the query's result type information back to the front end or
|
|
|
|
* to whatever destination we're dealing with.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
BeginCommand(NULL,
|
|
|
|
operation,
|
|
|
|
attinfo,
|
|
|
|
isRetrieveIntoRelation,
|
|
|
|
isRetrieveIntoPortal,
|
|
|
|
tag,
|
|
|
|
dest);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* If retrieve into portal, stop now; we do not run the plan until a
|
|
|
|
* FETCH command is received.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
if (isRetrieveIntoPortal)
|
|
|
|
{
|
2000-06-28 05:33:33 +02:00
|
|
|
PortalSetQuery(portal,
|
|
|
|
queryDesc,
|
|
|
|
attinfo,
|
|
|
|
state,
|
|
|
|
PortalCleanup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-08-22 06:06:22 +02:00
|
|
|
/* Now we can return to caller's memory context. */
|
|
|
|
MemoryContextSwitchTo(oldContext);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
EndCommand(tag, dest);
|
2000-08-22 06:06:22 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Now we get to the important call to ExecutorRun() where we actually
|
|
|
|
* run the plan..
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-10-26 23:38:24 +02:00
|
|
|
ExecutorRun(queryDesc, state, EXEC_RUN, 0L);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* save infos for EndCommand */
|
|
|
|
UpdateCommandInfo(operation, state->es_lastoid, state->es_processed);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Now, we close down all the scans and free allocated resources.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
ExecutorEnd(queryDesc, state);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Notify the destination of end of processing.
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
EndCommand(tag, dest);
|
|
|
|
}
|