From bedb78d386a47fd66b6cda2040e0a5fb545ee371 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 28 Apr 2005 21:47:18 +0000 Subject: Implement sharable row-level locks, and use them for foreign key references to eliminate unnecessary deadlocks. This commit adds SELECT ... FOR SHARE paralleling SELECT ... FOR UPDATE. The implementation uses a new SLRU data structure (managed much like pg_subtrans) to represent multiple- transaction-ID sets. When more than one transaction is holding a shared lock on a particular row, we create a MultiXactId representing that set of transactions and store its ID in the row's XMAX. This scheme allows an effectively unlimited number of row locks, just as we did before, while not costing any extra overhead except when a shared lock actually has to be shared. Still TODO: use the regular lock manager to control the grant order when multiple backends are waiting for a row lock. Alvaro Herrera and Tom Lane. --- src/backend/utils/adt/ri_triggers.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src/backend/utils/adt/ri_triggers.c') diff --git a/src/backend/utils/adt/ri_triggers.c b/src/backend/utils/adt/ri_triggers.c index 72ae3b8dde..78a85b7edc 100644 --- a/src/backend/utils/adt/ri_triggers.c +++ b/src/backend/utils/adt/ri_triggers.c @@ -17,7 +17,7 @@ * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.76 2004/12/31 22:01:22 pgsql Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.77 2005/04/28 21:47:15 tgl Exp $ * * ---------- */ @@ -206,7 +206,7 @@ RI_FKey_check(PG_FUNCTION_ARGS) * tuple. * * pk_rel is opened in RowShareLock mode since that's what our eventual - * SELECT FOR UPDATE will get on it. + * SELECT FOR SHARE will get on it. */ pk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock); fk_rel = trigdata->tg_relation; @@ -267,7 +267,7 @@ RI_FKey_check(PG_FUNCTION_ARGS) * ---------- */ quoteRelationName(pkrelname, pk_rel); - snprintf(querystr, sizeof(querystr), "SELECT 1 FROM ONLY %s x FOR UPDATE OF x", + snprintf(querystr, sizeof(querystr), "SELECT 1 FROM ONLY %s x FOR SHARE OF x", pkrelname); /* Prepare and save the plan */ @@ -428,7 +428,7 @@ RI_FKey_check(PG_FUNCTION_ARGS) queryoids[i] = SPI_gettypeid(fk_rel->rd_att, qkey.keypair[i][RI_KEYPAIR_FK_IDX]); } - strcat(querystr, " FOR UPDATE OF x"); + strcat(querystr, " FOR SHARE OF x"); /* Prepare and save the plan */ qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids, @@ -590,7 +590,7 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel, queryoids[i] = SPI_gettypeid(pk_rel->rd_att, qkey.keypair[i][RI_KEYPAIR_PK_IDX]); } - strcat(querystr, " FOR UPDATE OF x"); + strcat(querystr, " FOR SHARE OF x"); /* Prepare and save the plan */ qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids, @@ -655,7 +655,7 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS) * tuple. * * fk_rel is opened in RowShareLock mode since that's what our eventual - * SELECT FOR UPDATE will get on it. + * SELECT FOR SHARE will get on it. */ fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock); pk_rel = trigdata->tg_relation; @@ -748,7 +748,7 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS) queryoids[i] = SPI_gettypeid(pk_rel->rd_att, qkey.keypair[i][RI_KEYPAIR_PK_IDX]); } - strcat(querystr, " FOR UPDATE OF x"); + strcat(querystr, " FOR SHARE OF x"); /* Prepare and save the plan */ qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids, @@ -834,7 +834,7 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS) * and old tuple. * * fk_rel is opened in RowShareLock mode since that's what our eventual - * SELECT FOR UPDATE will get on it. + * SELECT FOR SHARE will get on it. */ fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock); pk_rel = trigdata->tg_relation; @@ -939,7 +939,7 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS) queryoids[i] = SPI_gettypeid(pk_rel->rd_att, qkey.keypair[i][RI_KEYPAIR_PK_IDX]); } - strcat(querystr, " FOR UPDATE OF x"); + strcat(querystr, " FOR SHARE OF x"); /* Prepare and save the plan */ qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids, @@ -1373,7 +1373,7 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS) * tuple. * * fk_rel is opened in RowShareLock mode since that's what our eventual - * SELECT FOR UPDATE will get on it. + * SELECT FOR SHARE will get on it. */ fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock); pk_rel = trigdata->tg_relation; @@ -1453,7 +1453,7 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS) queryoids[i] = SPI_gettypeid(pk_rel->rd_att, qkey.keypair[i][RI_KEYPAIR_PK_IDX]); } - strcat(querystr, " FOR UPDATE OF x"); + strcat(querystr, " FOR SHARE OF x"); /* Prepare and save the plan */ qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids, @@ -1543,7 +1543,7 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS) * and old tuple. * * fk_rel is opened in RowShareLock mode since that's what our eventual - * SELECT FOR UPDATE will get on it. + * SELECT FOR SHARE will get on it. */ fk_rel = heap_open(trigdata->tg_trigger->tgconstrrelid, RowShareLock); pk_rel = trigdata->tg_relation; @@ -1634,7 +1634,7 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS) queryoids[i] = SPI_gettypeid(pk_rel->rd_att, qkey.keypair[i][RI_KEYPAIR_PK_IDX]); } - strcat(querystr, " FOR UPDATE OF x"); + strcat(querystr, " FOR SHARE OF x"); /* Prepare and save the plan */ qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids, -- cgit v1.2.1