aboutsummaryrefslogtreecommitdiff
path: root/src/backend/optimizer
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2006-07-31 20:09:10 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2006-07-31 20:09:10 +0000
commit09d3670df3e4593be1d2299a62d982829016b847 (patch)
tree7a62e91c1cf595d0334dd2c196d1c79835cc267b /src/backend/optimizer
parent4cd72b53b9975bab5f4ca0792cf4f54c84829404 (diff)
downloadpostgresql-09d3670df3e4593be1d2299a62d982829016b847.tar.gz
postgresql-09d3670df3e4593be1d2299a62d982829016b847.zip
Change the relation_open protocol so that we obtain lock on a relation
(table or index) before trying to open its relcache entry. This fixes race conditions in which someone else commits a change to the relation's catalog entries while we are in process of doing relcache load. Problems of that ilk have been reported sporadically for years, but it was not really practical to fix until recently --- for instance, the recent addition of WAL-log support for in-place updates helped. Along the way, remove pg_am.amconcurrent: all AMs are now expected to support concurrent update.
Diffstat (limited to 'src/backend/optimizer')
-rw-r--r--src/backend/optimizer/util/plancat.c27
-rw-r--r--src/backend/optimizer/util/relnode.c4
2 files changed, 20 insertions, 11 deletions
diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index ff453336a16..bafe1b66731 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.121 2006/07/14 14:52:21 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/util/plancat.c,v 1.122 2006/07/31 20:09:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -64,7 +64,7 @@ static List *get_relation_constraints(Oid relationObjectId, RelOptInfo *rel);
* widths here, and we may as well cache the results for costsize.c.
*/
void
-get_relation_info(Oid relationObjectId, RelOptInfo *rel)
+get_relation_info(PlannerInfo *root, Oid relationObjectId, RelOptInfo *rel)
{
Index varno = rel->relid;
Relation relation;
@@ -105,9 +105,23 @@ get_relation_info(Oid relationObjectId, RelOptInfo *rel)
{
List *indexoidlist;
ListCell *l;
+ LOCKMODE lmode;
indexoidlist = RelationGetIndexList(relation);
+ /*
+ * For each index, we get the same type of lock that the executor will
+ * need, and do not release it. This saves a couple of trips to the
+ * shared lock manager while not creating any real loss of
+ * concurrency, because no schema changes could be happening on the
+ * index while we hold lock on the parent rel, and neither lock type
+ * blocks any other kind of index operation.
+ */
+ if (rel->relid == root->parse->resultRelation)
+ lmode = RowExclusiveLock;
+ else
+ lmode = AccessShareLock;
+
foreach(l, indexoidlist)
{
Oid indexoid = lfirst_oid(l);
@@ -120,13 +134,8 @@ get_relation_info(Oid relationObjectId, RelOptInfo *rel)
/*
* Extract info from the relation descriptor for the index.
- *
- * Note that we take no lock on the index; we assume our lock on
- * the parent table will protect the index's schema information.
- * When and if the executor actually uses the index, it will take
- * a lock as needed to protect the access to the index contents.
*/
- indexRelation = index_open(indexoid);
+ indexRelation = index_open(indexoid, lmode);
index = indexRelation->rd_index;
info = makeNode(IndexOptInfo);
@@ -203,7 +212,7 @@ get_relation_info(Oid relationObjectId, RelOptInfo *rel)
info->tuples = rel->tuples;
}
- index_close(indexRelation);
+ index_close(indexRelation, NoLock);
indexinfos = lcons(info, indexinfos);
}
diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index d20a8154612..8d06254a9f4 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.79 2006/07/14 14:52:21 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/util/relnode.c,v 1.80 2006/07/31 20:09:04 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -92,7 +92,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind)
{
case RTE_RELATION:
/* Table --- retrieve statistics from the system catalogs */
- get_relation_info(rte->relid, rel);
+ get_relation_info(root, rte->relid, rel);
break;
case RTE_SUBQUERY:
case RTE_FUNCTION: