nodeTee.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*-------------------------------------------------------------------------
 *
 * nodeTee.c--
 *    
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *   DESCRIPTION
 *      This code provides support for a tee node, which allows multiple
 *    parent in a megaplan. 
 *      
 *   INTERFACE ROUTINES
 *      ExecTee	
 *	ExecInitTee
 *	ExecEndTee
 *
 * IDENTIFICATION
18
 *    $Header: /cvsroot/pgsql/src/backend/executor/Attic/nodeTee.c,v 1.2 1996/10/31 10:12:24 scrappy Exp $
19 20 21 22 23
 *
 *-------------------------------------------------------------------------
 */

#include <sys/file.h>
24 25
#include "postgres.h"

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#include "utils/palloc.h"
#include "utils/relcache.h" 
#include "storage/bufmgr.h"  /* for IncrBufferRefCount */
#include "optimizer/internal.h"
#include "executor/executor.h"
#include "executor/nodeTee.h"
#include "catalog/catalog.h"
#include "tcop/pquery.h"

/* ------------------------------------------------------------------
 *	ExecInitTee
 *
 *	Create tee state
 *
 * ------------------------------------------------------------------
 */
bool
ExecInitTee(Tee* node, EState *currentEstate, Plan * parent)
{
    TeeState      *teeState;
    Plan          *outerPlan;
    int           len;
    Relation      bufferRel;
    TupleDesc     tupType;
    EState        *estate;
    
    /* it is possible that the Tee has already been initialized
       since it can be reached by multiple parents.
       If it is already initialized, simply return and do 
       not initialize the children nodes again
    */
    if (node->plan.state)
      return TRUE; 

    /* ----------------
     *  assign the node's execution state
     * ----------------
     */
    /* make a new executor state, because we have a different
       es_range_table */

/*     node->plan.state = estate;*/

    estate = CreateExecutorState();
    estate->es_direction = currentEstate->es_direction;
    estate->es_BaseId = currentEstate->es_BaseId;
    estate->es_BaseId = currentEstate->es_BaseId;
    estate->es_tupleTable = currentEstate->es_tupleTable;
    estate->es_refcount = currentEstate->es_refcount;
    estate->es_junkFilter = currentEstate->es_junkFilter;

    /* use the range table for Tee subplan since the range tables
       for the two parents may be different */
    if (node->rtentries)
	estate->es_range_table = node->rtentries; 
    else
	estate->es_range_table = currentEstate->es_range_table;

    node->plan.state = estate;


    /* ----------------
     * create teeState structure
     * ----------------
     */
    teeState = makeNode(TeeState);
    teeState->tee_leftPlace = 0;
    teeState->tee_rightPlace = 0;
    teeState->tee_lastPlace = 0;
    teeState->tee_bufferRel = NULL;
    teeState->tee_leftScanDesc = NULL;
    teeState->tee_rightScanDesc = NULL;


    node->teestate = teeState;
    
    /* ----------------
     *  Miscellanious initialization
     *
     *	     +	assign node's base_id
     *       +	assign debugging hooks and
     *       +	create expression context for node
     * ----------------
     */
    ExecAssignNodeBaseInfo(estate, &(teeState->cstate), parent);
    ExecAssignExprContext(estate, &(teeState->cstate));

#define TEE_NSLOTS 2
    /* ----------------
     *	initialize tuple slots
     * ----------------
     */
    ExecInitResultTupleSlot(estate, &(teeState->cstate));
    
    /* initialize child nodes */
    outerPlan = outerPlan((Plan*) node);
    ExecInitNode(outerPlan, estate, (Plan*) node);

    /* ----------------
     *  the tuple type info is from the outer plan of this node
     *  the result type is also the same as the outerplan  
     */
    ExecAssignResultTypeFromOuterPlan((Plan*) node, &(teeState->cstate));
    ExecAssignProjectionInfo((Plan*)node, &teeState->cstate);
    
    /* ---------------------------------------
       initialize temporary relation to buffer tuples 
    */
    tupType = ExecGetResultType(&(teeState->cstate));
    len =     ExecTargetListLength(((Plan*)node)->targetlist);

/*    bufferRel = ExecCreatR(len, tupType, _TEMP_RELATION_ID_);  */

    /* create a catalogued relation even though this is a temporary relation */
    /* cleanup of catalogued relations is easier to do */
    
    if (node->teeTableName[0] != '\0') {
	Relation r;

	teeState->tee_bufferRelname = pstrdup(node->teeTableName);

	/* we are given an tee table name, 
	   if a relation by that name exists, then we open it,
	   else we create it and then open it */
	r = RelationNameGetRelation(teeState->tee_bufferRelname);

	if (RelationIsValid(r))
	    bufferRel = heap_openr(teeState->tee_bufferRelname);
	else
	    bufferRel = heap_open(heap_create(teeState->tee_bufferRelname,
/*FIX */				      NULL,
				    'n',
				    DEFAULT_SMGR,
				    tupType));
    }
    else {
	sprintf(teeState->tee_bufferRelname,
		"ttemp_%d", /* 'ttemp' for 'tee' temporary*/
		newoid()); 
/*	bufferRel = ExecCreatR(len, tupType, _TEMP_RELATION_ID); */
	    bufferRel = heap_open(heap_create(teeState->tee_bufferRelname,
					      NULL, /*XXX */
					      'n',
					      DEFAULT_SMGR,
					      tupType));
    }

    teeState->tee_bufferRel = bufferRel;

    /*initialize a memory context for allocating thing like scan descriptors */
    /* we do this so that on cleanup of the tee, we can free things.
       if we didn't have our own memory context, we would be in the memory
       context of the portal that we happen to be using at the moment */

    teeState->tee_mcxt = (MemoryContext)CreateGlobalMemory(teeState->tee_bufferRelname);

    /* don't initialize the scan descriptors here
       because it's not good to initialize scan descriptors on empty
       rels. Wait until the scan descriptors are needed
       before initializing them. */
    
    teeState->tee_leftScanDesc = NULL;
    teeState->tee_rightScanDesc = NULL;
    
    return TRUE;
}

int 
ExecCountSlotsTee(Tee *node)
{
  /* Tee nodes can't have innerPlans */
    return ExecCountSlotsNode(outerPlan(node)) + TEE_NSLOTS;
}

/* ----------------------------------------------------------------
   initTeeScanDescs
      initializes the left and right scandescs on the temporary
      relation of a Tee node

      must open two separate scan descriptors,
      because the left and right scans may be at different points
* ----------------------------------------------------------------
*/
void 
initTeeScanDescs(Tee* node)
{
  TeeState *teeState;
  Relation bufferRel;
  ScanDirection dir;
  MemoryContext       orig;

  teeState = node->teestate;
  if (teeState->tee_leftScanDesc && teeState->tee_rightScanDesc)  
    return;

  orig = CurrentMemoryContext;
  MemoryContextSwitchTo(teeState->tee_mcxt);

  bufferRel = teeState->tee_bufferRel;
  dir = ((Plan*)node)->state->es_direction; /* backwards not handled yet XXX */

  if (teeState->tee_leftScanDesc == NULL)
    {
      teeState->tee_leftScanDesc = heap_beginscan(bufferRel,
						  ScanDirectionIsBackward(dir),
						  NowTimeQual, /* time qual */
						  0,       /* num scan keys */
						  NULL    /* scan keys */
						  );
    }
  if (teeState->tee_rightScanDesc == NULL)
    {
      teeState->tee_rightScanDesc = heap_beginscan(bufferRel,
						  ScanDirectionIsBackward(dir),
						  NowTimeQual, /* time qual */
						  0,     /* num scan keys */
						  NULL  /* scan keys */
						  );
    }

    MemoryContextSwitchTo(orig);
}

/* ----------------------------------------------------------------
 *	ExecTee(node)
 *
 *
 *      A Tee serves to connect a subplan to multiple parents.
 *      the subplan is always the outplan of the Tee node.
 *      
 *      The Tee gets requests from either leftParent or rightParent,
 *      fetches the result tuple from the child, and then
 *      stored the result into a temporary relation (serving as a queue).
 *      leftPlace and rightPlace keep track of where the left and rightParents
 *      are.
 *      If a parent requests a tuple and that parent is not at the end 
 *      of the temporary relation, then the request is satisfied from
 *      the queue instead of by executing the child plan
 *
 * ----------------------------------------------------------------
 */

TupleTableSlot*
ExecTee(Tee *node, Plan *parent)
{
    EState 		*estate;
    TeeState            *teeState;
    int                 leftPlace, rightPlace, lastPlace;
    int                 branch;
    TupleTableSlot*     result;
    TupleTableSlot*     slot;
    Plan                *childNode;
    ScanDirection       dir;
    HeapTuple           heapTuple;
    Relation            bufferRel;
    HeapScanDesc        scanDesc;
    Buffer              buffer;

    estate = ((Plan*)node)->state;
    teeState = node->teestate;
    leftPlace = teeState->tee_leftPlace;
    rightPlace = teeState->tee_rightPlace;
    lastPlace = teeState->tee_lastPlace;
    bufferRel = teeState->tee_bufferRel;

    childNode = outerPlan(node);

    dir = estate->es_direction;

    /* XXX doesn't handle backwards direction yet */

    if (parent == node->leftParent) {
	branch = leftPlace;
      }
    else
      if ( (parent == node->rightParent) || (parent == (Plan*) node)) 
	  /* the tee node could be the root node of the plan,
	     in which case, we treat it like a right-parent pull*/
	{
	branch = rightPlace;
      }
    else
      {
	elog(WARN,"A Tee node can only be executed from its left or right parent\n");
	return NULL;
      }

    if (branch == lastPlace)
      { /* we're at the end of the queue already,
	   - get a new tuple from the child plan,
	   - store it in the queue,
	   - increment lastPlace,
	   - increment leftPlace or rightPlace as appropriate,
	   - and return result
	   */
	slot = ExecProcNode(childNode, (Plan*)node);
	if (!TupIsNull(slot)) 
	  {
	    heapTuple = slot->val;
	    
	    /* insert into temporary relation */
	    heap_insert(bufferRel, heapTuple);
	    
	    /* once there is data in the temporary relation,
	       ensure that the left and right scandescs are initialized */
	    initTeeScanDescs(node);

	    scanDesc = (parent == node->leftParent) ?
	      teeState->tee_leftScanDesc : teeState->tee_rightScanDesc;

	    {
      /* move the scandesc forward so we don't re-read this tuple later */
	      HeapTuple throwAway;
	      /* Buffer buffer;*/
	      throwAway = heap_getnext(scanDesc,
				       ScanDirectionIsBackward(dir),
	             		   /*  &buffer */
				       (Buffer*)NULL);
	    }

	    /* set the shouldFree field of the child's slot so that
	       when the child's slot is free'd, this tuple isn't free'd also */
	    /* does this mean this tuple has to be garbage collected later??*/
	    slot->ttc_shouldFree = false;

	    teeState->tee_lastPlace = lastPlace + 1;
	  }
	result = slot;
      }	
    else 
      {/* the desired data already exists in the temporary relation */ 
	scanDesc = (parent == node->leftParent) ?
	  teeState->tee_leftScanDesc : teeState->tee_rightScanDesc;

	heapTuple = heap_getnext(scanDesc,
				 ScanDirectionIsBackward(dir),
				 &buffer);

	/* Increase the pin count on the buffer page, because the
	   tuple stored in the slot also points to it (as well as
	   the scan descriptor). If we don't, ExecStoreTuple will
	   decrease the pin count on the next iteration. */
    
	if (buffer != InvalidBuffer) 
	  IncrBufferRefCount(buffer);
    
	slot = teeState->cstate.cs_ResultTupleSlot;
	slot->ttc_tupleDescriptor = RelationGetTupleDescriptor(bufferRel);

	result =   ExecStoreTuple(heapTuple,/* tuple to store */
				  slot,  /* slot to store in */
				  buffer,/* this tuple's buffer */
				  false); /* don't free stuff from heap_getnext */
				 
      }

    if (parent == node->leftParent)
      {
	teeState->tee_leftPlace = leftPlace+1;
      }
    else
      {
	teeState->tee_rightPlace = rightPlace+1;
      }

    return result;
}

/* ----------------------------------------------------------------
 *   	ExecTeeReScan(node)
 *   
 *   	Rescans the relation.
 * ----------------------------------------------------------------
 */
void
ExecTeeReScan(Tee *node, ExprContext *exprCtxt, Plan *parent)
{

    EState 		*estate;
    TeeState            *teeState;
    ScanDirection       dir;

    estate = ((Plan*)node)->state;
    teeState = node->teestate;

    dir = estate->es_direction;
    
    /* XXX doesn't handle backwards direction yet */

    if (parent == node->leftParent) {
      if (teeState->tee_leftScanDesc)
	{
	  heap_rescan(teeState->tee_leftScanDesc,
		      ScanDirectionIsBackward(dir),
		      NULL);
	  teeState->tee_leftPlace = 0;
	}
    }
    else
      {
	if (teeState->tee_rightScanDesc)
	  {
	  heap_rescan(teeState->tee_leftScanDesc,
		      ScanDirectionIsBackward(dir),
		      NULL);
	  teeState->tee_rightPlace = 0;
	  }
      }
}


/* ---------------------------------------------------------------------
 *	ExecEndTee
 *
 *   End the Tee node, and free up any storage
 * since a Tee node can be downstream of multiple parent nodes,
 * only free when both parents are done
 * --------------------------------------------------------------------
 */

void 
ExecEndTee(Tee* node, Plan* parent)
{
    EState 		*estate;
    TeeState            *teeState;
    int                 leftPlace, rightPlace, lastPlace;
    Relation            bufferRel;
    MemoryContext       orig;

    estate = ((Plan*)node)->state;
    teeState = node->teestate;
    leftPlace = teeState->tee_leftPlace;
    rightPlace = teeState->tee_rightPlace;
    lastPlace = teeState->tee_lastPlace;

    if (!node->leftParent || parent == node->leftParent)
	leftPlace = -1;

    if (!node->rightParent || parent == node->rightParent)
	rightPlace = -1;

    if (parent == (Plan*)node) 
      rightPlace = leftPlace = -1;

    teeState->tee_leftPlace = leftPlace;
    teeState->tee_rightPlace = rightPlace;
    if ( (leftPlace == -1) && (rightPlace == -1) )
      {
	/* remove the temporary relations */
	/* and close the scan descriptors */

	bufferRel = teeState->tee_bufferRel;
        if (bufferRel) {
	    heap_destroyr(bufferRel);
	  teeState->tee_bufferRel = NULL;
	  if (teeState->tee_mcxt) {
	    orig = CurrentMemoryContext;
	    MemoryContextSwitchTo(teeState->tee_mcxt);
	  }

	  if (teeState->tee_leftScanDesc)
	    {
	    heap_endscan(teeState->tee_leftScanDesc);
	    teeState->tee_leftScanDesc = NULL;
	  }
	  if (teeState->tee_rightScanDesc)
	    {
	    heap_endscan(teeState->tee_rightScanDesc);
	    teeState->tee_rightScanDesc = NULL;
	  }

	  if (teeState->tee_mcxt) {
	    MemoryContextSwitchTo(orig);
	    teeState->tee_mcxt = NULL;
	  }
	}
     }
    
}