1 /*-------------------------------------------------------------------------
2  *
3  * execCurrent.c
4  *	  executor support for WHERE CURRENT OF cursor
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *	src/backend/executor/execCurrent.c
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14 
15 #include "access/relscan.h"
16 #include "access/sysattr.h"
17 #include "catalog/pg_type.h"
18 #include "executor/executor.h"
19 #include "utils/builtins.h"
20 #include "utils/lsyscache.h"
21 #include "utils/portal.h"
22 #include "utils/rel.h"
23 
24 
25 static char *fetch_cursor_param_value(ExprContext *econtext, int paramId);
26 static ScanState *search_plan_tree(PlanState *node, Oid table_oid,
27 				 bool *pending_rescan);
28 
29 
30 /*
31  * execCurrentOf
32  *
33  * Given a CURRENT OF expression and the OID of a table, determine which row
34  * of the table is currently being scanned by the cursor named by CURRENT OF,
35  * and return the row's TID into *current_tid.
36  *
37  * Returns TRUE if a row was identified.  Returns FALSE if the cursor is valid
38  * for the table but is not currently scanning a row of the table (this is a
39  * legal situation in inheritance cases).  Raises error if cursor is not a
40  * valid updatable scan of the specified table.
41  */
42 bool
execCurrentOf(CurrentOfExpr * cexpr,ExprContext * econtext,Oid table_oid,ItemPointer current_tid)43 execCurrentOf(CurrentOfExpr *cexpr,
44 			  ExprContext *econtext,
45 			  Oid table_oid,
46 			  ItemPointer current_tid)
47 {
48 	char	   *cursor_name;
49 	char	   *table_name;
50 	Portal		portal;
51 	QueryDesc  *queryDesc;
52 
53 	/* Get the cursor name --- may have to look up a parameter reference */
54 	if (cexpr->cursor_name)
55 		cursor_name = cexpr->cursor_name;
56 	else
57 		cursor_name = fetch_cursor_param_value(econtext, cexpr->cursor_param);
58 
59 	/* Fetch table name for possible use in error messages */
60 	table_name = get_rel_name(table_oid);
61 	if (table_name == NULL)
62 		elog(ERROR, "cache lookup failed for relation %u", table_oid);
63 
64 	/* Find the cursor's portal */
65 	portal = GetPortalByName(cursor_name);
66 	if (!PortalIsValid(portal))
67 		ereport(ERROR,
68 				(errcode(ERRCODE_UNDEFINED_CURSOR),
69 				 errmsg("cursor \"%s\" does not exist", cursor_name)));
70 
71 	/*
72 	 * We have to watch out for non-SELECT queries as well as held cursors,
73 	 * both of which may have null queryDesc.
74 	 */
75 	if (portal->strategy != PORTAL_ONE_SELECT)
76 		ereport(ERROR,
77 				(errcode(ERRCODE_INVALID_CURSOR_STATE),
78 				 errmsg("cursor \"%s\" is not a SELECT query",
79 						cursor_name)));
80 	queryDesc = PortalGetQueryDesc(portal);
81 	if (queryDesc == NULL || queryDesc->estate == NULL)
82 		ereport(ERROR,
83 				(errcode(ERRCODE_INVALID_CURSOR_STATE),
84 				 errmsg("cursor \"%s\" is held from a previous transaction",
85 						cursor_name)));
86 
87 	/*
88 	 * We have two different strategies depending on whether the cursor uses
89 	 * FOR UPDATE/SHARE or not.  The reason for supporting both is that the
90 	 * FOR UPDATE code is able to identify a target table in many cases where
91 	 * the other code can't, while the non-FOR-UPDATE case allows use of WHERE
92 	 * CURRENT OF with an insensitive cursor.
93 	 */
94 	if (queryDesc->estate->es_rowMarks)
95 	{
96 		ExecRowMark *erm;
97 		ListCell   *lc;
98 
99 		/*
100 		 * Here, the query must have exactly one FOR UPDATE/SHARE reference to
101 		 * the target table, and we dig the ctid info out of that.
102 		 */
103 		erm = NULL;
104 		foreach(lc, queryDesc->estate->es_rowMarks)
105 		{
106 			ExecRowMark *thiserm = (ExecRowMark *) lfirst(lc);
107 
108 			if (!RowMarkRequiresRowShareLock(thiserm->markType))
109 				continue;		/* ignore non-FOR UPDATE/SHARE items */
110 
111 			if (thiserm->relid == table_oid)
112 			{
113 				if (erm)
114 					ereport(ERROR,
115 							(errcode(ERRCODE_INVALID_CURSOR_STATE),
116 							 errmsg("cursor \"%s\" has multiple FOR UPDATE/SHARE references to table \"%s\"",
117 									cursor_name, table_name)));
118 				erm = thiserm;
119 			}
120 		}
121 
122 		if (erm == NULL)
123 			ereport(ERROR,
124 					(errcode(ERRCODE_INVALID_CURSOR_STATE),
125 					 errmsg("cursor \"%s\" does not have a FOR UPDATE/SHARE reference to table \"%s\"",
126 							cursor_name, table_name)));
127 
128 		/*
129 		 * The cursor must have a current result row: per the SQL spec, it's
130 		 * an error if not.
131 		 */
132 		if (portal->atStart || portal->atEnd)
133 			ereport(ERROR,
134 					(errcode(ERRCODE_INVALID_CURSOR_STATE),
135 					 errmsg("cursor \"%s\" is not positioned on a row",
136 							cursor_name)));
137 
138 		/* Return the currently scanned TID, if there is one */
139 		if (ItemPointerIsValid(&(erm->curCtid)))
140 		{
141 			*current_tid = erm->curCtid;
142 			return true;
143 		}
144 
145 		/*
146 		 * This table didn't produce the cursor's current row; some other
147 		 * inheritance child of the same parent must have.  Signal caller to
148 		 * do nothing on this table.
149 		 */
150 		return false;
151 	}
152 	else
153 	{
154 		/*
155 		 * Without FOR UPDATE, we dig through the cursor's plan to find the
156 		 * scan node.  Fail if it's not there or buried underneath
157 		 * aggregation.
158 		 */
159 		ScanState  *scanstate;
160 		bool		pending_rescan = false;
161 
162 		scanstate = search_plan_tree(queryDesc->planstate, table_oid,
163 									 &pending_rescan);
164 		if (!scanstate)
165 			ereport(ERROR,
166 					(errcode(ERRCODE_INVALID_CURSOR_STATE),
167 					 errmsg("cursor \"%s\" is not a simply updatable scan of table \"%s\"",
168 							cursor_name, table_name)));
169 
170 		/*
171 		 * The cursor must have a current result row: per the SQL spec, it's
172 		 * an error if not.  We test this at the top level, rather than at the
173 		 * scan node level, because in inheritance cases any one table scan
174 		 * could easily not be on a row. We want to return false, not raise
175 		 * error, if the passed-in table OID is for one of the inactive scans.
176 		 */
177 		if (portal->atStart || portal->atEnd)
178 			ereport(ERROR,
179 					(errcode(ERRCODE_INVALID_CURSOR_STATE),
180 					 errmsg("cursor \"%s\" is not positioned on a row",
181 							cursor_name)));
182 
183 		/*
184 		 * Now OK to return false if we found an inactive scan.  It is
185 		 * inactive either if it's not positioned on a row, or there's a
186 		 * rescan pending for it.
187 		 */
188 		if (TupIsNull(scanstate->ss_ScanTupleSlot) || pending_rescan)
189 			return false;
190 
191 		/*
192 		 * Extract TID of the scan's current row.  The mechanism for this is
193 		 * in principle scan-type-dependent, but for most scan types, we can
194 		 * just dig the TID out of the physical scan tuple.
195 		 */
196 		if (IsA(scanstate, IndexOnlyScanState))
197 		{
198 			/*
199 			 * For IndexOnlyScan, the tuple stored in ss_ScanTupleSlot may be
200 			 * a virtual tuple that does not have the ctid column, so we have
201 			 * to get the TID from xs_ctup.t_self.
202 			 */
203 			IndexScanDesc scan = ((IndexOnlyScanState *) scanstate)->ioss_ScanDesc;
204 
205 			*current_tid = scan->xs_ctup.t_self;
206 		}
207 		else
208 		{
209 			/*
210 			 * Default case: try to fetch TID from the scan node's current
211 			 * tuple.  As an extra cross-check, verify tableoid in the current
212 			 * tuple.  If the scan hasn't provided a physical tuple, we have
213 			 * to fail.
214 			 */
215 			Datum		ldatum;
216 			bool		lisnull;
217 			ItemPointer tuple_tid;
218 
219 #ifdef USE_ASSERT_CHECKING
220 			if (!slot_getsysattr(scanstate->ss_ScanTupleSlot,
221 								 TableOidAttributeNumber,
222 								 &ldatum,
223 								 &lisnull))
224 				ereport(ERROR,
225 						(errcode(ERRCODE_INVALID_CURSOR_STATE),
226 						 errmsg("cursor \"%s\" is not a simply updatable scan of table \"%s\"",
227 								cursor_name, table_name)));
228 			Assert(!lisnull);
229 			Assert(DatumGetObjectId(ldatum) == table_oid);
230 #endif
231 
232 			if (!slot_getsysattr(scanstate->ss_ScanTupleSlot,
233 								 SelfItemPointerAttributeNumber,
234 								 &ldatum,
235 								 &lisnull))
236 				ereport(ERROR,
237 						(errcode(ERRCODE_INVALID_CURSOR_STATE),
238 						 errmsg("cursor \"%s\" is not a simply updatable scan of table \"%s\"",
239 								cursor_name, table_name)));
240 			Assert(!lisnull);
241 			tuple_tid = (ItemPointer) DatumGetPointer(ldatum);
242 
243 			*current_tid = *tuple_tid;
244 		}
245 
246 		Assert(ItemPointerIsValid(current_tid));
247 
248 		return true;
249 	}
250 }
251 
252 /*
253  * fetch_cursor_param_value
254  *
255  * Fetch the string value of a param, verifying it is of type REFCURSOR.
256  */
257 static char *
fetch_cursor_param_value(ExprContext * econtext,int paramId)258 fetch_cursor_param_value(ExprContext *econtext, int paramId)
259 {
260 	ParamListInfo paramInfo = econtext->ecxt_param_list_info;
261 
262 	if (paramInfo &&
263 		paramId > 0 && paramId <= paramInfo->numParams)
264 	{
265 		ParamExternData *prm = &paramInfo->params[paramId - 1];
266 
267 		/* give hook a chance in case parameter is dynamic */
268 		if (!OidIsValid(prm->ptype) && paramInfo->paramFetch != NULL)
269 			(*paramInfo->paramFetch) (paramInfo, paramId);
270 
271 		if (OidIsValid(prm->ptype) && !prm->isnull)
272 		{
273 			/* safety check in case hook did something unexpected */
274 			if (prm->ptype != REFCURSOROID)
275 				ereport(ERROR,
276 						(errcode(ERRCODE_DATATYPE_MISMATCH),
277 						 errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
278 								paramId,
279 								format_type_be(prm->ptype),
280 								format_type_be(REFCURSOROID))));
281 
282 			/* We know that refcursor uses text's I/O routines */
283 			return TextDatumGetCString(prm->value);
284 		}
285 	}
286 
287 	ereport(ERROR,
288 			(errcode(ERRCODE_UNDEFINED_OBJECT),
289 			 errmsg("no value found for parameter %d", paramId)));
290 	return NULL;
291 }
292 
293 /*
294  * search_plan_tree
295  *
296  * Search through a PlanState tree for a scan node on the specified table.
297  * Return NULL if not found or multiple candidates.
298  *
299  * CAUTION: this function is not charged simply with finding some candidate
300  * scan, but with ensuring that that scan returned the plan tree's current
301  * output row.  That's why we must reject multiple-match cases.
302  *
303  * If a candidate is found, set *pending_rescan to true if that candidate
304  * or any node above it has a pending rescan action, i.e. chgParam != NULL.
305  * That indicates that we shouldn't consider the node to be positioned on a
306  * valid tuple, even if its own state would indicate that it is.  (Caller
307  * must initialize *pending_rescan to false, and should not trust its state
308  * if multiple candidates are found.)
309  */
310 static ScanState *
search_plan_tree(PlanState * node,Oid table_oid,bool * pending_rescan)311 search_plan_tree(PlanState *node, Oid table_oid,
312 				 bool *pending_rescan)
313 {
314 	ScanState  *result = NULL;
315 
316 	if (node == NULL)
317 		return NULL;
318 	switch (nodeTag(node))
319 	{
320 			/*
321 			 * Relation scan nodes can all be treated alike: check to see if
322 			 * they are scanning the specified table.
323 			 *
324 			 * ForeignScan and CustomScan might not have a currentRelation, in
325 			 * which case we just ignore them.  (We dare not descend to any
326 			 * child plan nodes they might have, since we do not know the
327 			 * relationship of such a node's current output tuple to the
328 			 * children's current outputs.)
329 			 */
330 		case T_SeqScanState:
331 		case T_SampleScanState:
332 		case T_IndexScanState:
333 		case T_IndexOnlyScanState:
334 		case T_BitmapHeapScanState:
335 		case T_TidScanState:
336 		case T_ForeignScanState:
337 		case T_CustomScanState:
338 			{
339 				ScanState  *sstate = (ScanState *) node;
340 
341 				if (sstate->ss_currentRelation &&
342 					RelationGetRelid(sstate->ss_currentRelation) == table_oid)
343 					result = sstate;
344 				break;
345 			}
346 
347 			/*
348 			 * For Append, we can check each input node.  It is safe to
349 			 * descend to the inputs because only the input that resulted in
350 			 * the Append's current output node could be positioned on a tuple
351 			 * at all; the other inputs are either at EOF or not yet started.
352 			 * Hence, if the desired table is scanned by some
353 			 * currently-inactive input node, we will find that node but then
354 			 * our caller will realize that it didn't emit the tuple of
355 			 * interest.
356 			 *
357 			 * We do need to watch out for multiple matches (possible if
358 			 * Append was from UNION ALL rather than an inheritance tree).
359 			 *
360 			 * Note: we can NOT descend through MergeAppend similarly, since
361 			 * its inputs are likely all active, and we don't know which one
362 			 * returned the current output tuple.  (Perhaps that could be
363 			 * fixed if we were to let this code know more about MergeAppend's
364 			 * internal state, but it does not seem worth the trouble.  Users
365 			 * should not expect plans for ORDER BY queries to be considered
366 			 * simply-updatable, since they won't be if the sorting is
367 			 * implemented by a Sort node.)
368 			 */
369 		case T_AppendState:
370 			{
371 				AppendState *astate = (AppendState *) node;
372 				int			i;
373 
374 				for (i = 0; i < astate->as_nplans; i++)
375 				{
376 					ScanState  *elem = search_plan_tree(astate->appendplans[i],
377 														table_oid,
378 														pending_rescan);
379 
380 					if (!elem)
381 						continue;
382 					if (result)
383 						return NULL;	/* multiple matches */
384 					result = elem;
385 				}
386 				break;
387 			}
388 
389 			/*
390 			 * Result and Limit can be descended through (these are safe
391 			 * because they always return their input's current row)
392 			 */
393 		case T_ResultState:
394 		case T_LimitState:
395 			result = search_plan_tree(node->lefttree,
396 									  table_oid,
397 									  pending_rescan);
398 			break;
399 
400 			/*
401 			 * SubqueryScan too, but it keeps the child in a different place
402 			 */
403 		case T_SubqueryScanState:
404 			result = search_plan_tree(((SubqueryScanState *) node)->subplan,
405 									  table_oid,
406 									  pending_rescan);
407 			break;
408 
409 		default:
410 			/* Otherwise, assume we can't descend through it */
411 			break;
412 	}
413 
414 	/*
415 	 * If we found a candidate at or below this node, then this node's
416 	 * chgParam indicates a pending rescan that will affect the candidate.
417 	 */
418 	if (result && node->chgParam != NULL)
419 		*pending_rescan = true;
420 
421 	return result;
422 }
423