|
| 1 | +/* ------------------------------------------------------------------------ |
| 2 | + * |
| 3 | + * nodeCustom.c |
| 4 | + * Routines to handle execution of custom scan node |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * ------------------------------------------------------------------------ |
| 10 | + */ |
| 11 | +#include "postgres.h" |
| 12 | + |
| 13 | +#include "executor/executor.h" |
| 14 | +#include "executor/nodeCustom.h" |
| 15 | +#include "nodes/execnodes.h" |
| 16 | +#include "nodes/plannodes.h" |
| 17 | +#include "parser/parsetree.h" |
| 18 | +#include "utils/hsearch.h" |
| 19 | +#include "utils/memutils.h" |
| 20 | +#include "utils/rel.h" |
| 21 | + |
| 22 | +CustomScanState * |
| 23 | +ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags) |
| 24 | +{ |
| 25 | + CustomScanState *css; |
| 26 | + Relation scan_rel; |
| 27 | + |
| 28 | + /* populate a CustomScanState according to the CustomScan */ |
| 29 | + css = (CustomScanState *) cscan->methods->CreateCustomScanState(cscan); |
| 30 | + Assert(IsA(css, CustomScanState)); |
| 31 | + |
| 32 | + /* fill up fields of ScanState */ |
| 33 | + css->ss.ps.plan = &cscan->scan.plan; |
| 34 | + css->ss.ps.state = estate; |
| 35 | + |
| 36 | + /* create expression context for node */ |
| 37 | + ExecAssignExprContext(estate, &css->ss.ps); |
| 38 | + |
| 39 | + /* initialize child expressions */ |
| 40 | + css->ss.ps.targetlist = (List *) |
| 41 | + ExecInitExpr((Expr *) cscan->scan.plan.targetlist, |
| 42 | + (PlanState *) css); |
| 43 | + css->ss.ps.qual = (List *) |
| 44 | + ExecInitExpr((Expr *) cscan->scan.plan.qual, |
| 45 | + (PlanState *) css); |
| 46 | + |
| 47 | + /* tuple table initialization */ |
| 48 | + ExecInitScanTupleSlot(estate, &css->ss); |
| 49 | + ExecInitResultTupleSlot(estate, &css->ss.ps); |
| 50 | + |
| 51 | + /* initialize scan relation */ |
| 52 | + scan_rel = ExecOpenScanRelation(estate, cscan->scan.scanrelid, eflags); |
| 53 | + css->ss.ss_currentRelation = scan_rel; |
| 54 | + css->ss.ss_currentScanDesc = NULL; /* set by provider */ |
| 55 | + ExecAssignScanType(&css->ss, RelationGetDescr(scan_rel)); |
| 56 | + |
| 57 | + css->ss.ps.ps_TupFromTlist = false; |
| 58 | + |
| 59 | + /* |
| 60 | + * Initialize result tuple type and projection info. |
| 61 | + */ |
| 62 | + ExecAssignResultTypeFromTL(&css->ss.ps); |
| 63 | + ExecAssignScanProjectionInfo(&css->ss); |
| 64 | + |
| 65 | + /* |
| 66 | + * The callback of custom-scan provider applies the final initialization |
| 67 | + * of the custom-scan-state node according to its logic. |
| 68 | + */ |
| 69 | + css->methods->BeginCustomScan(css, estate, eflags); |
| 70 | + |
| 71 | + return css; |
| 72 | +} |
| 73 | + |
| 74 | +TupleTableSlot * |
| 75 | +ExecCustomScan(CustomScanState *node) |
| 76 | +{ |
| 77 | + Assert(node->methods->ExecCustomScan != NULL); |
| 78 | + return node->methods->ExecCustomScan(node); |
| 79 | +} |
| 80 | + |
| 81 | +void |
| 82 | +ExecEndCustomScan(CustomScanState *node) |
| 83 | +{ |
| 84 | + Assert(node->methods->EndCustomScan != NULL); |
| 85 | + node->methods->EndCustomScan(node); |
| 86 | + |
| 87 | + /* Free the exprcontext */ |
| 88 | + ExecFreeExprContext(&node->ss.ps); |
| 89 | + |
| 90 | + /* Clean out the tuple table */ |
| 91 | + ExecClearTuple(node->ss.ps.ps_ResultTupleSlot); |
| 92 | + if (node->ss.ss_ScanTupleSlot) |
| 93 | + ExecClearTuple(node->ss.ss_ScanTupleSlot); |
| 94 | + |
| 95 | + /* Close the heap relation */ |
| 96 | + ExecCloseScanRelation(node->ss.ss_currentRelation); |
| 97 | +} |
| 98 | + |
| 99 | +void |
| 100 | +ExecReScanCustomScan(CustomScanState *node) |
| 101 | +{ |
| 102 | + Assert(node->methods->ReScanCustomScan != NULL); |
| 103 | + node->methods->ReScanCustomScan(node); |
| 104 | +} |
| 105 | + |
| 106 | +void |
| 107 | +ExecCustomMarkPos(CustomScanState *node) |
| 108 | +{ |
| 109 | + if (!node->methods->MarkPosCustomScan) |
| 110 | + ereport(ERROR, |
| 111 | + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 112 | + errmsg("custom-scan \"%s\" does not support MarkPos", |
| 113 | + node->methods->CustomName))); |
| 114 | + node->methods->MarkPosCustomScan(node); |
| 115 | +} |
| 116 | + |
| 117 | +void |
| 118 | +ExecCustomRestrPos(CustomScanState *node) |
| 119 | +{ |
| 120 | + if (!node->methods->RestrPosCustomScan) |
| 121 | + ereport(ERROR, |
| 122 | + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 123 | + errmsg("custom-scan \"%s\" does not support MarkPos", |
| 124 | + node->methods->CustomName))); |
| 125 | + node->methods->RestrPosCustomScan(node); |
| 126 | +} |
0 commit comments