mirror of
				https://github.com/postgres/postgres.git
				synced 2025-10-31 10:30:33 +03:00 
			
		
		
		
	Upcoming work intends to allow pluggable ways to introduce new ways of storing table data. Accessing those table access methods from the executor requires TupleTableSlots to be carry tuples in the native format of such storage methods; otherwise there'll be a significant conversion overhead. Different access methods will require different data to store tuples efficiently (just like virtual, minimal, heap already require fields in TupleTableSlot). To allow that without requiring additional pointer indirections, we want to have different structs (embedding TupleTableSlot) for different types of slots. Thus different types of slots are needed, which requires adapting creators of slots. The slot that most efficiently can represent a type of tuple in an executor node will often depend on the type of slot a child node uses. Therefore we need to track the type of slot is returned by nodes, so parent slots can create slots based on that. Relatedly, JIT compilation of tuple deforming needs to know which type of slot a certain expression refers to, so it can create an appropriate deforming function for the type of tuple in the slot. But not all nodes will only return one type of slot, e.g. an append node will potentially return different types of slots for each of its subplans. Therefore add function that allows to query the type of a node's result slot, and whether it'll always be the same type (whether it's fixed). This can be queried using ExecGetResultSlotOps(). The scan, result, inner, outer type of slots are automatically inferred from ExecInitScanTupleSlot(), ExecInitResultSlot(), left/right subtrees respectively. If that's not correct for a node, that can be overwritten using new fields in PlanState. This commit does not introduce the actually abstracted implementation of different kind of TupleTableSlots, that will be left for a followup commit. The different types of slots introduced will, for now, still use the same backing implementation. While this already partially invalidates the big comment in tuptable.h, it seems to make more sense to update it later, when the different TupleTableSlot implementations actually exist. Author: Ashutosh Bapat and Andres Freund, with changes by Amit Khandekar Discussion: https://postgr.es/m/20181105210039.hh4vvi4vwoq5ba2q@alap3.anarazel.de
		
			
				
	
	
		
			202 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			202 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /*-------------------------------------------------------------------------
 | |
|  *
 | |
|  * nodeNamedtuplestorescan.c
 | |
|  *	  routines to handle NamedTuplestoreScan nodes.
 | |
|  *
 | |
|  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
 | |
|  * Portions Copyright (c) 1994, Regents of the University of California
 | |
|  *
 | |
|  *
 | |
|  * IDENTIFICATION
 | |
|  *	  src/backend/executor/nodeNamedtuplestorescan.c
 | |
|  *
 | |
|  *-------------------------------------------------------------------------
 | |
|  */
 | |
| 
 | |
| #include "postgres.h"
 | |
| 
 | |
| #include "executor/execdebug.h"
 | |
| #include "executor/nodeNamedtuplestorescan.h"
 | |
| #include "miscadmin.h"
 | |
| #include "utils/queryenvironment.h"
 | |
| 
 | |
| static TupleTableSlot *NamedTuplestoreScanNext(NamedTuplestoreScanState *node);
 | |
| 
 | |
| /* ----------------------------------------------------------------
 | |
|  *		NamedTuplestoreScanNext
 | |
|  *
 | |
|  *		This is a workhorse for ExecNamedTuplestoreScan
 | |
|  * ----------------------------------------------------------------
 | |
|  */
 | |
| static TupleTableSlot *
 | |
| NamedTuplestoreScanNext(NamedTuplestoreScanState *node)
 | |
| {
 | |
| 	TupleTableSlot *slot;
 | |
| 
 | |
| 	/* We intentionally do not support backward scan. */
 | |
| 	Assert(ScanDirectionIsForward(node->ss.ps.state->es_direction));
 | |
| 
 | |
| 	/*
 | |
| 	 * Get the next tuple from tuplestore. Return NULL if no more tuples.
 | |
| 	 */
 | |
| 	slot = node->ss.ss_ScanTupleSlot;
 | |
| 	tuplestore_select_read_pointer(node->relation, node->readptr);
 | |
| 	(void) tuplestore_gettupleslot(node->relation, true, false, slot);
 | |
| 	return slot;
 | |
| }
 | |
| 
 | |
| /*
 | |
|  * NamedTuplestoreScanRecheck -- access method routine to recheck a tuple in
 | |
|  * EvalPlanQual
 | |
|  */
 | |
| static bool
 | |
| NamedTuplestoreScanRecheck(NamedTuplestoreScanState *node, TupleTableSlot *slot)
 | |
| {
 | |
| 	/* nothing to check */
 | |
| 	return true;
 | |
| }
 | |
| 
 | |
| /* ----------------------------------------------------------------
 | |
|  *		ExecNamedTuplestoreScan(node)
 | |
|  *
 | |
|  *		Scans the CTE sequentially and returns the next qualifying tuple.
 | |
|  *		We call the ExecScan() routine and pass it the appropriate
 | |
|  *		access method functions.
 | |
|  * ----------------------------------------------------------------
 | |
|  */
 | |
| static TupleTableSlot *
 | |
| ExecNamedTuplestoreScan(PlanState *pstate)
 | |
| {
 | |
| 	NamedTuplestoreScanState *node = castNode(NamedTuplestoreScanState, pstate);
 | |
| 
 | |
| 	return ExecScan(&node->ss,
 | |
| 					(ExecScanAccessMtd) NamedTuplestoreScanNext,
 | |
| 					(ExecScanRecheckMtd) NamedTuplestoreScanRecheck);
 | |
| }
 | |
| 
 | |
| 
 | |
| /* ----------------------------------------------------------------
 | |
|  *		ExecInitNamedTuplestoreScan
 | |
|  * ----------------------------------------------------------------
 | |
|  */
 | |
| NamedTuplestoreScanState *
 | |
| ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflags)
 | |
| {
 | |
| 	NamedTuplestoreScanState *scanstate;
 | |
| 	EphemeralNamedRelation enr;
 | |
| 
 | |
| 	/* check for unsupported flags */
 | |
| 	Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
 | |
| 
 | |
| 	/*
 | |
| 	 * NamedTuplestoreScan should not have any children.
 | |
| 	 */
 | |
| 	Assert(outerPlan(node) == NULL);
 | |
| 	Assert(innerPlan(node) == NULL);
 | |
| 
 | |
| 	/*
 | |
| 	 * create new NamedTuplestoreScanState for node
 | |
| 	 */
 | |
| 	scanstate = makeNode(NamedTuplestoreScanState);
 | |
| 	scanstate->ss.ps.plan = (Plan *) node;
 | |
| 	scanstate->ss.ps.state = estate;
 | |
| 	scanstate->ss.ps.ExecProcNode = ExecNamedTuplestoreScan;
 | |
| 
 | |
| 	enr = get_ENR(estate->es_queryEnv, node->enrname);
 | |
| 	if (!enr)
 | |
| 		elog(ERROR, "executor could not find named tuplestore \"%s\"",
 | |
| 			 node->enrname);
 | |
| 
 | |
| 	Assert(enr->reldata);
 | |
| 	scanstate->relation = (Tuplestorestate *) enr->reldata;
 | |
| 	scanstate->tupdesc = ENRMetadataGetTupDesc(&(enr->md));
 | |
| 	scanstate->readptr =
 | |
| 		tuplestore_alloc_read_pointer(scanstate->relation, EXEC_FLAG_REWIND);
 | |
| 
 | |
| 	/*
 | |
| 	 * The new read pointer copies its position from read pointer 0, which
 | |
| 	 * could be anywhere, so explicitly rewind it.
 | |
| 	 */
 | |
| 	tuplestore_select_read_pointer(scanstate->relation, scanstate->readptr);
 | |
| 	tuplestore_rescan(scanstate->relation);
 | |
| 
 | |
| 	/*
 | |
| 	 * XXX: Should we add a function to free that read pointer when done?
 | |
| 	 *
 | |
| 	 * This was attempted, but it did not improve performance or memory usage
 | |
| 	 * in any tested cases.
 | |
| 	 */
 | |
| 
 | |
| 	/*
 | |
| 	 * Miscellaneous initialization
 | |
| 	 *
 | |
| 	 * create expression context for node
 | |
| 	 */
 | |
| 	ExecAssignExprContext(estate, &scanstate->ss.ps);
 | |
| 
 | |
| 	/*
 | |
| 	 * The scan tuple type is specified for the tuplestore.
 | |
| 	 */
 | |
| 	ExecInitScanTupleSlot(estate, &scanstate->ss, scanstate->tupdesc,
 | |
| 						  &TTSOpsMinimalTuple);
 | |
| 
 | |
| 	/*
 | |
| 	 * Initialize result type and projection.
 | |
| 	 */
 | |
| 	ExecInitResultTypeTL(&scanstate->ss.ps);
 | |
| 	ExecAssignScanProjectionInfo(&scanstate->ss);
 | |
| 
 | |
| 	/*
 | |
| 	 * initialize child expressions
 | |
| 	 */
 | |
| 	scanstate->ss.ps.qual =
 | |
| 		ExecInitQual(node->scan.plan.qual, (PlanState *) scanstate);
 | |
| 
 | |
| 	return scanstate;
 | |
| }
 | |
| 
 | |
| /* ----------------------------------------------------------------
 | |
|  *		ExecEndNamedTuplestoreScan
 | |
|  *
 | |
|  *		frees any storage allocated through C routines.
 | |
|  * ----------------------------------------------------------------
 | |
|  */
 | |
| void
 | |
| ExecEndNamedTuplestoreScan(NamedTuplestoreScanState *node)
 | |
| {
 | |
| 	/*
 | |
| 	 * Free exprcontext
 | |
| 	 */
 | |
| 	ExecFreeExprContext(&node->ss.ps);
 | |
| 
 | |
| 	/*
 | |
| 	 * clean out the tuple table
 | |
| 	 */
 | |
| 	if (node->ss.ps.ps_ResultTupleSlot)
 | |
| 		ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
 | |
| 	ExecClearTuple(node->ss.ss_ScanTupleSlot);
 | |
| }
 | |
| 
 | |
| /* ----------------------------------------------------------------
 | |
|  *		ExecReScanNamedTuplestoreScan
 | |
|  *
 | |
|  *		Rescans the relation.
 | |
|  * ----------------------------------------------------------------
 | |
|  */
 | |
| void
 | |
| ExecReScanNamedTuplestoreScan(NamedTuplestoreScanState *node)
 | |
| {
 | |
| 	Tuplestorestate *tuplestorestate = node->relation;
 | |
| 
 | |
| 	if (node->ss.ps.ps_ResultTupleSlot)
 | |
| 		ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
 | |
| 
 | |
| 	ExecScanReScan(&node->ss);
 | |
| 
 | |
| 	/*
 | |
| 	 * Rewind my own pointer.
 | |
| 	 */
 | |
| 	tuplestore_select_read_pointer(tuplestorestate, node->readptr);
 | |
| 	tuplestore_rescan(tuplestorestate);
 | |
| }
 |