PostgreSQL Source Code  git master
nodeLockRows.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * nodeLockRows.c
4  * Routines to handle FOR UPDATE/FOR SHARE row locking
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/executor/nodeLockRows.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
17  * ExecLockRows - fetch locked rows
18  * ExecInitLockRows - initialize node and subnodes..
19  * ExecEndLockRows - shutdown node and subnodes
20  */
21 
22 #include "postgres.h"
23 
24 #include "access/htup_details.h"
25 #include "access/xact.h"
26 #include "executor/executor.h"
27 #include "executor/nodeLockRows.h"
28 #include "foreign/fdwapi.h"
29 #include "miscadmin.h"
30 #include "storage/bufmgr.h"
31 #include "utils/rel.h"
32 #include "utils/tqual.h"
33 
34 
35 /* ----------------------------------------------------------------
36  * ExecLockRows
37  * ----------------------------------------------------------------
38  */
39 static TupleTableSlot * /* return: a tuple or NULL */
41 {
42  LockRowsState *node = castNode(LockRowsState, pstate);
43  TupleTableSlot *slot;
44  EState *estate;
46  bool epq_needed;
47  ListCell *lc;
48 
50 
51  /*
52  * get information from the node
53  */
54  estate = node->ps.state;
55  outerPlan = outerPlanState(node);
56 
57  /*
58  * Get next tuple from subplan, if any.
59  */
60 lnext:
61  slot = ExecProcNode(outerPlan);
62 
63  if (TupIsNull(slot))
64  return NULL;
65 
66  /* We don't need EvalPlanQual unless we get updated tuple version(s) */
67  epq_needed = false;
68 
69  /*
70  * Attempt to lock the source tuple(s). (Note we only have locking
71  * rowmarks in lr_arowMarks.)
72  */
73  foreach(lc, node->lr_arowMarks)
74  {
75  ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(lc);
76  ExecRowMark *erm = aerm->rowmark;
77  HeapTuple *testTuple;
78  Datum datum;
79  bool isNull;
80  HeapTupleData tuple;
81  Buffer buffer;
83  LockTupleMode lockmode;
85  HeapTuple copyTuple;
86 
87  /* clear any leftover test tuple for this rel */
88  testTuple = &(node->lr_curtuples[erm->rti - 1]);
89  if (*testTuple != NULL)
90  heap_freetuple(*testTuple);
91  *testTuple = NULL;
92 
93  /* if child rel, must check whether it produced this row */
94  if (erm->rti != erm->prti)
95  {
96  Oid tableoid;
97 
98  datum = ExecGetJunkAttribute(slot,
99  aerm->toidAttNo,
100  &isNull);
101  /* shouldn't ever get a null result... */
102  if (isNull)
103  elog(ERROR, "tableoid is NULL");
104  tableoid = DatumGetObjectId(datum);
105 
106  Assert(OidIsValid(erm->relid));
107  if (tableoid != erm->relid)
108  {
109  /* this child is inactive right now */
110  erm->ermActive = false;
112  continue;
113  }
114  }
115  erm->ermActive = true;
116 
117  /* fetch the tuple's ctid */
118  datum = ExecGetJunkAttribute(slot,
119  aerm->ctidAttNo,
120  &isNull);
121  /* shouldn't ever get a null result... */
122  if (isNull)
123  elog(ERROR, "ctid is NULL");
124 
125  /* requests for foreign tables must be passed to their FDW */
126  if (erm->relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
127  {
128  FdwRoutine *fdwroutine;
129  bool updated = false;
130 
131  fdwroutine = GetFdwRoutineForRelation(erm->relation, false);
132  /* this should have been checked already, but let's be safe */
133  if (fdwroutine->RefetchForeignRow == NULL)
134  ereport(ERROR,
135  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
136  errmsg("cannot lock rows in foreign table \"%s\"",
138  copyTuple = fdwroutine->RefetchForeignRow(estate,
139  erm,
140  datum,
141  &updated);
142  if (copyTuple == NULL)
143  {
144  /* couldn't get the lock, so skip this row */
145  goto lnext;
146  }
147 
148  /* save locked tuple for possible EvalPlanQual testing below */
149  *testTuple = copyTuple;
150 
151  /*
152  * if FDW says tuple was updated before getting locked, we need to
153  * perform EPQ testing to see if quals are still satisfied
154  */
155  if (updated)
156  epq_needed = true;
157 
158  continue;
159  }
160 
161  /* okay, try to lock the tuple */
162  tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
163  switch (erm->markType)
164  {
165  case ROW_MARK_EXCLUSIVE:
166  lockmode = LockTupleExclusive;
167  break;
169  lockmode = LockTupleNoKeyExclusive;
170  break;
171  case ROW_MARK_SHARE:
172  lockmode = LockTupleShare;
173  break;
174  case ROW_MARK_KEYSHARE:
175  lockmode = LockTupleKeyShare;
176  break;
177  default:
178  elog(ERROR, "unsupported rowmark type");
179  lockmode = LockTupleNoKeyExclusive; /* keep compiler quiet */
180  break;
181  }
182 
183  test = heap_lock_tuple(erm->relation, &tuple,
184  estate->es_output_cid,
185  lockmode, erm->waitPolicy, true,
186  &buffer, &hufd);
187  ReleaseBuffer(buffer);
188  switch (test)
189  {
190  case HeapTupleWouldBlock:
191  /* couldn't lock tuple in SKIP LOCKED mode */
192  goto lnext;
193 
195 
196  /*
197  * The target tuple was already updated or deleted by the
198  * current command, or by a later command in the current
199  * transaction. We *must* ignore the tuple in the former
200  * case, so as to avoid the "Halloween problem" of repeated
201  * update attempts. In the latter case it might be sensible
202  * to fetch the updated tuple instead, but doing so would
203  * require changing heap_update and heap_delete to not
204  * complain about updating "invisible" tuples, which seems
205  * pretty scary (heap_lock_tuple will not complain, but few
206  * callers expect HeapTupleInvisible, and we're not one of
207  * them). So for now, treat the tuple as deleted and do not
208  * process.
209  */
210  goto lnext;
211 
213  /* got the lock successfully */
214  break;
215 
216  case HeapTupleUpdated:
218  ereport(ERROR,
219  (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
220  errmsg("could not serialize access due to concurrent update")));
221  if (ItemPointerEquals(&hufd.ctid, &tuple.t_self))
222  {
223  /* Tuple was deleted, so don't return it */
224  goto lnext;
225  }
226 
227  /* updated, so fetch and lock the updated version */
228  copyTuple = EvalPlanQualFetch(estate, erm->relation,
229  lockmode, erm->waitPolicy,
230  &hufd.ctid, hufd.xmax);
231 
232  if (copyTuple == NULL)
233  {
234  /*
235  * Tuple was deleted; or it's locked and we're under SKIP
236  * LOCKED policy, so don't return it
237  */
238  goto lnext;
239  }
240  /* remember the actually locked tuple's TID */
241  tuple.t_self = copyTuple->t_self;
242 
243  /* Save locked tuple for EvalPlanQual testing below */
244  *testTuple = copyTuple;
245 
246  /* Remember we need to do EPQ testing */
247  epq_needed = true;
248 
249  /* Continue loop until we have all target tuples */
250  break;
251 
252  case HeapTupleInvisible:
253  elog(ERROR, "attempted to lock invisible tuple");
254 
255  default:
256  elog(ERROR, "unrecognized heap_lock_tuple status: %u",
257  test);
258  }
259 
260  /* Remember locked tuple's TID for EPQ testing and WHERE CURRENT OF */
261  erm->curCtid = tuple.t_self;
262  }
263 
264  /*
265  * If we need to do EvalPlanQual testing, do so.
266  */
267  if (epq_needed)
268  {
269  /* Initialize EPQ machinery */
270  EvalPlanQualBegin(&node->lr_epqstate, estate);
271 
272  /*
273  * Transfer any already-fetched tuples into the EPQ state, and fetch a
274  * copy of any rows that were successfully locked without any update
275  * having occurred. (We do this in a separate pass so as to avoid
276  * overhead in the common case where there are no concurrent updates.)
277  * Make sure any inactive child rels have NULL test tuples in EPQ.
278  */
279  foreach(lc, node->lr_arowMarks)
280  {
281  ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(lc);
282  ExecRowMark *erm = aerm->rowmark;
283  HeapTupleData tuple;
284  Buffer buffer;
285 
286  /* skip non-active child tables, but clear their test tuples */
287  if (!erm->ermActive)
288  {
289  Assert(erm->rti != erm->prti); /* check it's child table */
290  EvalPlanQualSetTuple(&node->lr_epqstate, erm->rti, NULL);
291  continue;
292  }
293 
294  /* was tuple updated and fetched above? */
295  if (node->lr_curtuples[erm->rti - 1] != NULL)
296  {
297  /* yes, so set it as the EPQ test tuple for this rel */
299  erm->rti,
300  node->lr_curtuples[erm->rti - 1]);
301  /* freeing this tuple is now the responsibility of EPQ */
302  node->lr_curtuples[erm->rti - 1] = NULL;
303  continue;
304  }
305 
306  /* foreign tables should have been fetched above */
307  Assert(erm->relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE);
309 
310  /* okay, fetch the tuple */
311  tuple.t_self = erm->curCtid;
312  if (!heap_fetch(erm->relation, SnapshotAny, &tuple, &buffer,
313  false, NULL))
314  elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
315 
316  /* successful, copy and store tuple */
317  EvalPlanQualSetTuple(&node->lr_epqstate, erm->rti,
318  heap_copytuple(&tuple));
319  ReleaseBuffer(buffer);
320  }
321 
322  /*
323  * Now fetch any non-locked source rows --- the EPQ logic knows how to
324  * do that.
325  */
326  EvalPlanQualSetSlot(&node->lr_epqstate, slot);
328 
329  /*
330  * And finally we can re-evaluate the tuple.
331  */
332  slot = EvalPlanQualNext(&node->lr_epqstate);
333  if (TupIsNull(slot))
334  {
335  /* Updated tuple fails qual, so ignore it and go on */
336  goto lnext;
337  }
338  }
339 
340  /* Got all locks, so return the current tuple */
341  return slot;
342 }
343 
344 /* ----------------------------------------------------------------
345  * ExecInitLockRows
346  *
347  * This initializes the LockRows node state structures and
348  * the node's subplan.
349  * ----------------------------------------------------------------
350  */
352 ExecInitLockRows(LockRows *node, EState *estate, int eflags)
353 {
354  LockRowsState *lrstate;
355  Plan *outerPlan = outerPlan(node);
356  List *epq_arowmarks;
357  ListCell *lc;
358 
359  /* check for unsupported flags */
360  Assert(!(eflags & EXEC_FLAG_MARK));
361 
362  /*
363  * create state structure
364  */
365  lrstate = makeNode(LockRowsState);
366  lrstate->ps.plan = (Plan *) node;
367  lrstate->ps.state = estate;
368  lrstate->ps.ExecProcNode = ExecLockRows;
369 
370  /*
371  * Miscellaneous initialization
372  *
373  * LockRows nodes never call ExecQual or ExecProject.
374  */
375 
376  /*
377  * Tuple table initialization (XXX not actually used...)
378  */
379  ExecInitResultTupleSlot(estate, &lrstate->ps);
380 
381  /*
382  * then initialize outer plan
383  */
384  outerPlanState(lrstate) = ExecInitNode(outerPlan, estate, eflags);
385 
386  /*
387  * LockRows nodes do no projections, so initialize projection info for
388  * this node appropriately
389  */
390  ExecAssignResultTypeFromTL(&lrstate->ps);
391  lrstate->ps.ps_ProjInfo = NULL;
392 
393  /*
394  * Create workspace in which we can remember per-RTE locked tuples
395  */
396  lrstate->lr_ntables = list_length(estate->es_range_table);
397  lrstate->lr_curtuples = (HeapTuple *)
398  palloc0(lrstate->lr_ntables * sizeof(HeapTuple));
399 
400  /*
401  * Locate the ExecRowMark(s) that this node is responsible for, and
402  * construct ExecAuxRowMarks for them. (InitPlan should already have
403  * built the global list of ExecRowMarks.)
404  */
405  lrstate->lr_arowMarks = NIL;
406  epq_arowmarks = NIL;
407  foreach(lc, node->rowMarks)
408  {
410  ExecRowMark *erm;
411  ExecAuxRowMark *aerm;
412 
413  /* ignore "parent" rowmarks; they are irrelevant at runtime */
414  if (rc->isParent)
415  continue;
416 
417  /* safety check on size of lr_curtuples array */
418  Assert(rc->rti > 0 && rc->rti <= lrstate->lr_ntables);
419 
420  /* find ExecRowMark and build ExecAuxRowMark */
421  erm = ExecFindRowMark(estate, rc->rti, false);
422  aerm = ExecBuildAuxRowMark(erm, outerPlan->targetlist);
423 
424  /*
425  * Only locking rowmarks go into our own list. Non-locking marks are
426  * passed off to the EvalPlanQual machinery. This is because we don't
427  * want to bother fetching non-locked rows unless we actually have to
428  * do an EPQ recheck.
429  */
431  lrstate->lr_arowMarks = lappend(lrstate->lr_arowMarks, aerm);
432  else
433  epq_arowmarks = lappend(epq_arowmarks, aerm);
434  }
435 
436  /* Now we have the info needed to set up EPQ state */
437  EvalPlanQualInit(&lrstate->lr_epqstate, estate,
438  outerPlan, epq_arowmarks, node->epqParam);
439 
440  return lrstate;
441 }
442 
443 /* ----------------------------------------------------------------
444  * ExecEndLockRows
445  *
446  * This shuts down the subplan and frees resources allocated
447  * to this node.
448  * ----------------------------------------------------------------
449  */
450 void
452 {
455 }
456 
457 
458 void
460 {
461  /*
462  * if chgParam of subnode is not null then plan will be re-scanned by
463  * first ExecProcNode.
464  */
465  if (node->ps.lefttree->chgParam == NULL)
466  ExecReScan(node->ps.lefttree);
467 }
#define ItemPointerIsValid(pointer)
Definition: itemptr.h:60
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:611
#define NIL
Definition: pg_list.h:69
HeapTuple * lr_curtuples
Definition: execnodes.h:2036
CommandId es_output_cid
Definition: execnodes.h:438
static void test(void)
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:884
EPQState lr_epqstate
Definition: execnodes.h:2035
HTSU_Result heap_lock_tuple(Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, bool follow_updates, Buffer *buffer, HeapUpdateFailureData *hufd)
Definition: heapam.c:4560
#define castNode(_type_, nodeptr)
Definition: nodes.h:579
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
void EvalPlanQualSetTuple(EPQState *epqstate, Index rti, HeapTuple tuple)
Definition: execMain.c:2829
Relation relation
Definition: execnodes.h:536
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf, bool keep_buf, Relation stats_relation)
Definition: heapam.c:1876
LockRowsState * ExecInitLockRows(LockRows *node, EState *estate, int eflags)
Definition: nodeLockRows.c:352
#define DatumGetObjectId(X)
Definition: postgres.h:506
HeapTuple EvalPlanQualFetch(EState *estate, Relation relation, int lockmode, LockWaitPolicy wait_policy, ItemPointer tid, TransactionId priorXmax)
Definition: execMain.c:2565
void ExecReScanLockRows(LockRowsState *node)
Definition: nodeLockRows.c:459
void EvalPlanQualFetchRowMarks(EPQState *epqstate)
Definition: execMain.c:2864
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
void ExecEndLockRows(LockRowsState *node)
Definition: nodeLockRows.c:451
#define IsolationUsesXactSnapshot()
Definition: xact.h:43
static TupleTableSlot * ExecLockRows(PlanState *pstate)
Definition: nodeLockRows.c:40
int errcode(int sqlerrcode)
Definition: elog.c:575
bool ermActive
Definition: execnodes.h:544
int epqParam
Definition: plannodes.h:915
LockWaitPolicy waitPolicy
Definition: execnodes.h:543
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
EState * state
Definition: execnodes.h:851
List * es_range_table
Definition: execnodes.h:431
Form_pg_class rd_rel
Definition: rel.h:114
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1373
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
#define RowMarkRequiresRowShareLock(marktype)
Definition: plannodes.h:973
void EvalPlanQualBegin(EPQState *epqstate, EState *parentestate)
Definition: execMain.c:3015
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:447
TupleTableSlot * EvalPlanQualNext(EPQState *epqstate)
Definition: execMain.c:2999
struct PlanState * lefttree
Definition: execnodes.h:868
void EvalPlanQualEnd(EPQState *epqstate)
Definition: execMain.c:3208
ItemPointerData * ItemPointer
Definition: itemptr.h:49
ExecRowMark * rowmark
Definition: execnodes.h:563
ItemPointerData curCtid
Definition: execnodes.h:545
LockTupleMode
Definition: heapam.h:38
#define ERROR
Definition: elog.h:43
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
ItemPointerData t_self
Definition: htup.h:65
#define lfirst_node(type, lc)
Definition: pg_list.h:109
#define outerPlanState(node)
Definition: execnodes.h:895
PlanState ps
Definition: execnodes.h:2033
Index rti
Definition: execnodes.h:538
Index prti
Definition: execnodes.h:539
HTSU_Result
Definition: snapshot.h:121
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define TupIsNull(slot)
Definition: tuptable.h:138
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
Bitmapset * chgParam
Definition: execnodes.h:877
#define outerPlan(node)
Definition: plannodes.h:174
List * lappend(List *list, void *datum)
Definition: list.c:128
TransactionId xmax
Definition: heapam.h:71
void * palloc0(Size size)
Definition: mcxt.c:877
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:855
uintptr_t Datum
Definition: postgres.h:372
#define SnapshotAny
Definition: tqual.h:28
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:236
void EvalPlanQualInit(EPQState *epqstate, EState *estate, Plan *subplan, List *auxrowmarks, int epqParam)
Definition: execMain.c:2794
Plan * plan
Definition: execnodes.h:849
#define makeNode(_type_)
Definition: nodes.h:558
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
#define EXEC_FLAG_MARK
Definition: executor.h:61
List * rowMarks
Definition: plannodes.h:914
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
RowMarkType markType
Definition: execnodes.h:541
static int list_length(const List *l)
Definition: pg_list.h:89
AttrNumber toidAttNo
Definition: execnodes.h:565
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
List * targetlist
Definition: plannodes.h:144
#define DatumGetPointer(X)
Definition: postgres.h:555
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
int errmsg(const char *fmt,...)
Definition: elog.c:797
FdwRoutine * GetFdwRoutineForRelation(Relation relation, bool makecopy)
Definition: foreign.c:395
Datum ExecGetJunkAttribute(TupleTableSlot *slot, AttrNumber attno, bool *isNull)
Definition: execJunk.c:248
List * lr_arowMarks
Definition: execnodes.h:2034
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
bool isParent
Definition: plannodes.h:1024
ItemPointerData ctid
Definition: heapam.h:70
#define elog
Definition: elog.h:219
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
Definition: pg_list.h:45
int Buffer
Definition: buf.h:23
AttrNumber ctidAttNo
Definition: execnodes.h:564
ExecAuxRowMark * ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
Definition: execMain.c:2407
#define EvalPlanQualSetSlot(epqstate, slot)
Definition: executor.h:212
RefetchForeignRow_function RefetchForeignRow
Definition: fdwapi.h:215
ExecRowMark * ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
Definition: execMain.c:2383