9
9
*
10
10
*
11
11
* IDENTIFICATION
12
- * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.107 2005/05/22 22:30:20 tgl Exp $
12
+ * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.108 2005/05/23 03:01:14 tgl Exp $
13
13
*
14
14
*-------------------------------------------------------------------------
15
15
*/
@@ -67,7 +67,25 @@ get_relation_info(Oid relationObjectId, RelOptInfo *rel)
67
67
bool hasindex ;
68
68
List * indexinfos = NIL ;
69
69
70
- relation = heap_open (relationObjectId , AccessShareLock );
70
+ /*
71
+ * Normally, we can assume the rewriter already acquired at least
72
+ * AccessShareLock on each relation used in the query. However this
73
+ * will not be the case for relations added to the query because they
74
+ * are inheritance children of some relation mentioned explicitly.
75
+ * For them, this is the first access during the parse/rewrite/plan
76
+ * pipeline, and so we need to obtain and keep a suitable lock.
77
+ *
78
+ * XXX really, a suitable lock is RowShareLock if the relation is
79
+ * an UPDATE/DELETE target, and AccessShareLock otherwise. However
80
+ * we cannot easily tell here which to get, so for the moment just
81
+ * get AccessShareLock always. The executor will get the right lock
82
+ * when it runs, which means there is a very small chance of deadlock
83
+ * trying to upgrade our lock.
84
+ */
85
+ if (rel -> reloptkind == RELOPT_BASEREL )
86
+ relation = heap_open (relationObjectId , NoLock );
87
+ else
88
+ relation = heap_open (relationObjectId , AccessShareLock );
71
89
72
90
rel -> min_attr = FirstLowInvalidHeapAttributeNumber + 1 ;
73
91
rel -> max_attr = RelationGetNumberOfAttributes (relation );
@@ -205,8 +223,8 @@ get_relation_info(Oid relationObjectId, RelOptInfo *rel)
205
223
206
224
rel -> indexlist = indexinfos ;
207
225
208
- /* XXX keep the lock here? */
209
- heap_close (relation , AccessShareLock );
226
+ /* close rel, but keep lock if any */
227
+ heap_close (relation , NoLock );
210
228
}
211
229
212
230
/*
@@ -374,7 +392,8 @@ build_physical_tlist(Query *root, RelOptInfo *rel)
374
392
switch (rte -> rtekind )
375
393
{
376
394
case RTE_RELATION :
377
- relation = heap_open (rte -> relid , AccessShareLock );
395
+ /* Assume we already have adequate lock */
396
+ relation = heap_open (rte -> relid , NoLock );
378
397
379
398
numattrs = RelationGetNumberOfAttributes (relation );
380
399
for (attrno = 1 ; attrno <= numattrs ; attrno ++ )
@@ -401,7 +420,7 @@ build_physical_tlist(Query *root, RelOptInfo *rel)
401
420
false));
402
421
}
403
422
404
- heap_close (relation , AccessShareLock );
423
+ heap_close (relation , NoLock );
405
424
break ;
406
425
407
426
case RTE_SUBQUERY :
0 commit comments