home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / executor / n_scantemps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  7.4 KB  |  263 lines

  1. /* ----------------------------------------------------------------
  2.  *   FILE
  3.  *      scantemps.c
  4.  *
  5.  *   DESCRIPTION
  6.  *      This code provides support for scanning a list of temporary
  7.  *    relations
  8.  *      
  9.  *   INTERFACE ROUTINES
  10.  *      ExecScanTemps        sequentially scans a list of temporary rels.
  11.  *    ExecInitScanTemps    initializes a scantemps node.
  12.  *    ExecEndScanTemps    releases any storage allocated.
  13.  *
  14.  *   NOTES
  15.  *
  16.  *   IDENTIFICATION
  17.  *      $Header: /private/postgres/src/executor/RCS/n_scantemps.c,v 1.6 1992/08/04 17:38:01 mer Exp $
  18.  * ----------------------------------------------------------------
  19.  */
  20.  
  21. #include <sys/file.h>
  22. #include "utils/relcache.h"
  23. #include "tcop/slaves.h"
  24. #include "executor/executor.h"
  25.  
  26.  
  27. /* ----------------------------------------------------------------
  28.  *    ExecScanTemps(node)
  29.  *
  30.  *    Scans a list of temporary relations one by one.  It is
  31.  *    equivalent to an append node above a list of scan nodes,
  32.  *    but much more efficient, because it does not need to do
  33.  *    any system catalog lookup, the reldescs of the temporary
  34.  *    relations are already in the nodes.  It does not do
  35.  *    any projections either.  This node is specially for
  36.  *    collecting results from multiple parallel backends.
  37.  *
  38.  * ----------------------------------------------------------------
  39.  */
  40.  
  41. TupleTableSlot
  42. ExecScanTemps(node)
  43. ScanTemps node;
  44. {
  45.     ScanTempState     scantempState;
  46.     HeapScanDesc     currentScanDesc;
  47.     HeapTuple         heapTuple;
  48.     HeapTuple        retHeapTuple;
  49.     EState         estate;
  50.     ScanDirection     dir;
  51.     int         whichplan;
  52.     int         nplans;
  53.     Relation         tempreldesc;
  54.     TupleTableSlot    slot;
  55.     Buffer        buffer;
  56.     
  57.     estate = (EState) get_state((Plan)node);
  58.     scantempState = get_scantempState(node);
  59.     whichplan = get_st_whichplan(scantempState);
  60.     nplans = get_st_nplans(scantempState);
  61.     
  62. #if 0
  63.     elog(DEBUG, "ScanTemps: total %d plans, current plan %d", nplans,whichplan);
  64. #endif
  65.     
  66.     currentScanDesc = get_css_currentScanDesc((CommonScanState) scantempState);
  67.     heapTuple = amgetnext(currentScanDesc,
  68.               (dir == EXEC_BKWD),
  69.               NULL);
  70.     
  71.     while (heapTuple == NULL) {
  72.     if (++whichplan >= nplans)
  73.        return NULL;
  74.     
  75. #if 0
  76.     elog(DEBUG, "ScanTemps: changing to plan %d", whichplan);
  77. #endif
  78.     amendscan(currentScanDesc);
  79.     tempreldesc = get_css_currentRelation((CommonScanState) scantempState);
  80.     amclose(tempreldesc);
  81.     
  82.     /* 
  83.      * change to the next temporary relation
  84.      */
  85.         tempreldesc = (Relation)nth(whichplan, get_temprelDescs(node));
  86.     
  87.     /*
  88.      * reopen the file for the temp relations, because the file
  89.      * was opened in another backend, so the virtual file descriptor
  90.      * is invalid in the current process.
  91.      */
  92.         tempreldesc->rd_fd = FileNameOpenFile(
  93.                   (char *) relpath((char *)
  94.                       &(tempreldesc->rd_rel->relname)),
  95.                                   O_RDWR, 0666);
  96.     
  97.     RelationRegisterTempRel(tempreldesc);
  98.         dir = get_es_direction(estate);
  99.     
  100.         currentScanDesc = ambeginscan(tempreldesc,
  101.                                       (dir == EXEC_BKWD),
  102.                                       NowTimeQual,
  103.                                       0,
  104.                                       NULL);
  105.     
  106.     currentScanDesc->rs_parallel_ok = true;
  107.  
  108.         set_css_currentRelation((CommonScanState) scantempState, tempreldesc);
  109.         set_css_currentScanDesc((CommonScanState) scantempState,
  110.                 currentScanDesc);
  111.     set_st_whichplan(scantempState, whichplan);
  112.     
  113.         heapTuple = amgetnext(currentScanDesc,
  114.                               (dir == EXEC_BKWD),
  115.                               &buffer);
  116.       }
  117.  
  118.     
  119.     /* -----------------------------------------
  120.      *   make a copy of heapTuple so that later processing
  121.      *   will not modify the tuple on buffer page.
  122.      * -----------------------------------------
  123.      */
  124.     retHeapTuple = (HeapTuple) palloc(heapTuple->t_len);
  125.     bcopy(heapTuple, retHeapTuple, heapTuple->t_len);
  126.  
  127.     slot = (TupleTableSlot)
  128.     get_css_ScanTupleSlot((CommonScanState) scantempState);
  129.     
  130.     ExecStoreTuple((Pointer) heapTuple,    /* tuple */
  131.            (Pointer) slot,    /* destination slot */
  132.            buffer,            /* buffer for this tuple */
  133.            false);            /* don't use pfree on tuple */
  134.  
  135.     slot = (TupleTableSlot)
  136.     get_cs_ResultTupleSlot((CommonState) scantempState);
  137.     
  138.     return (TupleTableSlot)
  139.     ExecStoreTuple((Pointer) retHeapTuple, /* tuple to store */
  140.                (Pointer) slot,         /* slot to store in */
  141.                InvalidBuffer,          /* tuple has no buffer */
  142.                true);                /* free the return heap tuple */
  143. }
  144.  
  145. /* ------------------------------------------------------------------
  146.  *    ExecInitScanTemps
  147.  *
  148.  *    Create scantemp states and first scan descriptor for the
  149.  *    ScanTemps node.
  150.  *
  151.  * ------------------------------------------------------------------
  152.  */
  153.  
  154. List
  155. ExecInitScanTemps(node, estate, parent)
  156.     ScanTemps     node;
  157.     EState    estate;
  158.     Plan    parent;
  159. {
  160.     ScanTempState    scantempstate;
  161.     Relation        tempreldesc;
  162.     HeapScanDesc    currentScanDesc;
  163.     ScanDirection    dir;
  164.     List        tempRelDescs;
  165.  
  166.     set_state((Plan) node, (EStatePtr)estate);
  167.  
  168.     scantempstate = MakeScanTempState(0,0);
  169.     set_scantempState(node, scantempstate);
  170.  
  171. #define SCANTEMP_NSLOTS 2
  172.     /* ----------------
  173.      *    initialize tuple slots
  174.      * ----------------
  175.      */
  176.     ExecInitResultTupleSlot(estate, (CommonState) scantempstate);
  177.     ExecInitScanTupleSlot(estate, (CommonScanState) scantempstate);
  178.     
  179.     /* ----------------
  180.      *     initialize tuple type and projection info
  181.      * ----------------
  182.      */
  183.     ExecAssignResultTypeFromTL((Plan) node, (CommonState) scantempstate);
  184.     ExecAssignProjectionInfo((Plan) node, (CommonState) scantempstate);
  185.     
  186.     /* ----------------
  187.      *    XXX comment me
  188.      * ----------------
  189.      */
  190.     tempRelDescs = get_temprelDescs(node);
  191.     tempreldesc = (Relation)CAR(tempRelDescs);
  192.     tempreldesc->rd_fd =
  193.     FileNameOpenFile((char *) relpath(&(tempreldesc->rd_rel->relname)),
  194.              O_RDWR, 0666);
  195.     
  196.     RelationRegisterTempRel(tempreldesc);
  197.     
  198.     /* ----------------
  199.      *    XXX comment me
  200.      * ----------------
  201.      */
  202.     dir = get_es_direction(estate);
  203.     currentScanDesc = ambeginscan(tempreldesc,
  204.                   (dir == EXEC_BKWD),
  205.                   NowTimeQual,
  206.                   0,
  207.                   NULL);
  208.     
  209.     currentScanDesc->rs_parallel_ok = true;
  210.     
  211.     /* ----------------
  212.      *    XXX comment me
  213.      * ----------------
  214.      */
  215.     set_css_currentRelation((CommonScanState) scantempstate, tempreldesc);
  216.     set_css_currentScanDesc((CommonScanState) scantempstate, currentScanDesc);
  217.     set_st_whichplan(scantempstate, 0);
  218.     set_st_nplans(scantempstate, length(tempRelDescs));
  219.  
  220.     return LispTrue;
  221. }
  222.  
  223. int
  224. ExecCountSlotsScanTemps(node)
  225.     Plan node;
  226. {
  227.     return ExecCountSlotsNode(get_outerPlan(node)) +
  228.        ExecCountSlotsNode(get_innerPlan(node)) +
  229.        SCANTEMP_NSLOTS;
  230. }
  231.  
  232. /* ---------------------------------------------------------------------
  233.  *    ExecEndScanTemps
  234.  *
  235.  *    frees any storage allocated through C routines.
  236.  *    closes the last temp relation
  237.  *
  238.  * --------------------------------------------------------------------
  239.  */
  240.  
  241. void
  242. ExecEndScanTemps(node)
  243. ScanTemps node;
  244. {
  245.     Relation      tempreldesc;
  246.     List      tempRelDescs;
  247.     Relation      relation;
  248.     ScanTempState state;
  249.     HeapScanDesc  scanDesc;
  250.     LispValue      x;
  251.  
  252.     state = get_scantempState(node);
  253.     tempRelDescs = get_temprelDescs(node);
  254.     relation = get_css_currentRelation((CommonScanState) state);
  255.     scanDesc = get_css_currentScanDesc((CommonScanState) state);
  256.     
  257.     if (scanDesc != NULL)
  258.     amendscan(scanDesc);
  259.     
  260.     if (relation != NULL)
  261.     amclose(relation);
  262. }
  263.