pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #9371: pg_dump acquiring ROW EXCLUSIVE locks on tables
3+ messages / 3 participants
[nested] [flat]
* BUG #9371: pg_dump acquiring ROW EXCLUSIVE locks on tables
@ 2014-02-27 11:54 dean.a.rasheed@gmail.com
0 siblings, 1 reply; 3+ messages in thread
From: dean.a.rasheed@gmail.com @ 2014-02-27 11:54 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 9371
Logged by: Dean Rasheed
Email address: dean.a.rasheed@gmail.com
PostgreSQL version: 8.4.20
Operating system: Linux
Description:
[Spotted in 8.4, but also tested in 9.4devel]
On one of our production systems we are having a problem that some client
processes are being blocked by pg_dump, and therefore having to wait a long
time before they can run. The clients in question are attempting to obtain
an explicit SHARE lock on a table to prevent concurrent data changes, and it
turns out that pg_dump is obtaining a ROW EXCLUSIVE lock on those tables for
the duration of the dump.
Further digging revealed that pg_dump obtains a ROW EXCLUSIVE lock on any
table mentioned in an INSERT, UPDATE or DELETE RULE. Wondering how that can
happen, I tried the following test case, describing a relation:
CREATE TABLE t (a int);
CREATE VIEW v AS SELECT * FROM t;
CREATE RULE r AS ON INSERT TO v DO INSTEAD INSERT INTO t VALUES(NEW.a);
BEGIN;
\d+ v
SELECT mode FROM pg_locks where relation = 't'::regclass;
COMMIT;
Result:
mode
------------------
AccessShareLock
RowExclusiveLock
(2 rows)
whereas I would expect at most an ACCESS SHARE lock.
The reason is that in ruleutils.c, pg_get_ruledef()/make_ruledef() and
get_query_def() are calling AcquireRewriteLocks() on the rule query, which
acquires a ROW EXCLUSIVE lock on its target table, even though the query is
not about to be run in this context. The calls to AcquireRewriteLocks() do
seem to be necessary to handle dropped columns, but perhaps
AcquireRewriteLocks() could be told that the query is not about to be run in
this case, and that it should only acquire ACCESS SHARE locks on the
tables.
Regards,
Dean
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #9371: pg_dump acquiring ROW EXCLUSIVE locks on tables
@ 2014-03-03 15:21 Dean Rasheed <dean.a.rasheed@gmail.com>
parent: dean.a.rasheed@gmail.com
0 siblings, 1 reply; 3+ messages in thread
From: Dean Rasheed @ 2014-03-03 15:21 UTC (permalink / raw)
To: pgsql-bugs
On 27 February 2014 11:54, <dean.a.rasheed@gmail.com> wrote:
> [snip]
> perhaps
> AcquireRewriteLocks() could be told that the query is not about to be run in
> this case, and that it should only acquire ACCESS SHARE locks on the
> tables.
>
Here's a patch for HEAD along those lines.
I've tested it on our production data and confirmed that with this
patch pg_dump no longer acquires exclusive locks. I think this should
be back-patched, since we do promise that pg_dump does not block other
readers or writers.
Regards,
Dean
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
pg_dump-locking.patch (text/x-diff, 12.5K) download
diff --git a/src/backend/commands/matview.c b/src/backend/commands/matview.c
new file mode 100644
index a331f3e..d64f165
*** a/src/backend/commands/matview.c
--- b/src/backend/commands/matview.c
*************** refresh_matview_datafill(DestReceiver *d
*** 303,309 ****
/* Lock and rewrite, using a copy to preserve the original query. */
copied_query = copyObject(query);
! AcquireRewriteLocks(copied_query, false);
rewritten = QueryRewrite(copied_query);
/* SELECT should never rewrite to more or less than one SELECT query */
--- 303,309 ----
/* Lock and rewrite, using a copy to preserve the original query. */
copied_query = copyObject(query);
! AcquireRewriteLocks(copied_query, true, false);
rewritten = QueryRewrite(copied_query);
/* SELECT should never rewrite to more or less than one SELECT query */
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
new file mode 100644
index 0b13645..a3ede56
*** a/src/backend/rewrite/rewriteHandler.c
--- b/src/backend/rewrite/rewriteHandler.c
*************** typedef struct rewrite_event
*** 37,43 ****
CmdType event; /* type of rule being fired */
} rewrite_event;
! static bool acquireLocksOnSubLinks(Node *node, void *context);
static Query *rewriteRuleAction(Query *parsetree,
Query *rule_action,
Node *rule_qual,
--- 37,48 ----
CmdType event; /* type of rule being fired */
} rewrite_event;
! typedef struct
! {
! bool for_execute;
! } acquireLocksOnSubLinks_context;
!
! static bool acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context);
static Query *rewriteRuleAction(Query *parsetree,
Query *rule_action,
Node *rule_qual,
*************** static Bitmapset *adjust_view_column_set
*** 71,79 ****
* These locks will ensure that the relation schemas don't change under us
* while we are rewriting and planning the query.
*
* forUpdatePushedDown indicates that a pushed-down FOR [KEY] UPDATE/SHARE applies
* to the current subquery, requiring all rels to be opened with RowShareLock.
! * This should always be false at the start of the recursion.
*
* A secondary purpose of this routine is to fix up JOIN RTE references to
* dropped columns (see details below). Because the RTEs are modified in
--- 76,93 ----
* These locks will ensure that the relation schemas don't change under us
* while we are rewriting and planning the query.
*
+ * forExecute indicates that the query is about to be executed and that a
+ * RowExclusiveLock should be acquired on the query's resultRelation, and a
+ * RowShareLock should be acquired on any relation accessed FOR [KEY] UPDATE/SHARE.
+ * If forExecute is false, an AccessShareLock is acquired on all relations.
+ * The rewriter and most other callers generally assume that the query is
+ * about to be executed. The query deparsing code in ruleutils.c assumes that
+ * it is not.
+ *
* forUpdatePushedDown indicates that a pushed-down FOR [KEY] UPDATE/SHARE applies
* to the current subquery, requiring all rels to be opened with RowShareLock.
! * This should always be false at the start of the recursion. This flag is
! * ignored if forExecute is false.
*
* A secondary purpose of this routine is to fix up JOIN RTE references to
* dropped columns (see details below). Because the RTEs are modified in
*************** static Bitmapset *adjust_view_column_set
*** 101,110 ****
* construction of a nested join was O(N^2) in the nesting depth.)
*/
void
! AcquireRewriteLocks(Query *parsetree, bool forUpdatePushedDown)
{
ListCell *l;
int rt_index;
/*
* First, process RTEs of the current query level.
--- 115,129 ----
* construction of a nested join was O(N^2) in the nesting depth.)
*/
void
! AcquireRewriteLocks(Query *parsetree,
! bool forExecute,
! bool forUpdatePushedDown)
{
ListCell *l;
int rt_index;
+ acquireLocksOnSubLinks_context context;
+
+ context.for_execute = forExecute;
/*
* First, process RTEs of the current query level.
*************** AcquireRewriteLocks(Query *parsetree, bo
*** 130,135 ****
--- 149,158 ----
* release it until end of transaction. This protects the
* rewriter and planner against schema changes mid-query.
*
+ * If we are not actually about the execute the query, then we
+ * always just grab an AccessShareLock. Otherwise the type of
+ * lock required varies:
+ *
* If the relation is the query's result relation, then we
* need RowExclusiveLock. Otherwise, check to see if the
* relation is accessed FOR [KEY] UPDATE/SHARE or not. We
*************** AcquireRewriteLocks(Query *parsetree, bo
*** 137,143 ****
* would be trying to upgrade the lock, leading to possible
* deadlocks.
*/
! if (rt_index == parsetree->resultRelation)
lockmode = RowExclusiveLock;
else if (forUpdatePushedDown ||
get_parse_rowmark(parsetree, rt_index) != NULL)
--- 160,168 ----
* would be trying to upgrade the lock, leading to possible
* deadlocks.
*/
! if (!forExecute)
! lockmode = AccessShareLock;
! else if (rt_index == parsetree->resultRelation)
lockmode = RowExclusiveLock;
else if (forUpdatePushedDown ||
get_parse_rowmark(parsetree, rt_index) != NULL)
*************** AcquireRewriteLocks(Query *parsetree, bo
*** 225,230 ****
--- 250,256 ----
* recurse to process the represented subquery.
*/
AcquireRewriteLocks(rte->subquery,
+ forExecute,
(forUpdatePushedDown ||
get_parse_rowmark(parsetree, rt_index) != NULL));
break;
*************** AcquireRewriteLocks(Query *parsetree, bo
*** 240,246 ****
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
! AcquireRewriteLocks((Query *) cte->ctequery, false);
}
/*
--- 266,272 ----
{
CommonTableExpr *cte = (CommonTableExpr *) lfirst(l);
! AcquireRewriteLocks((Query *) cte->ctequery, forExecute, false);
}
/*
*************** AcquireRewriteLocks(Query *parsetree, bo
*** 248,254 ****
* the rtable and cteList.
*/
if (parsetree->hasSubLinks)
! query_tree_walker(parsetree, acquireLocksOnSubLinks, NULL,
QTW_IGNORE_RC_SUBQUERIES);
}
--- 274,281 ----
* the rtable and cteList.
*/
if (parsetree->hasSubLinks)
! query_tree_walker(parsetree, acquireLocksOnSubLinks,
! (void *) &context,
QTW_IGNORE_RC_SUBQUERIES);
}
*************** AcquireRewriteLocks(Query *parsetree, bo
*** 256,262 ****
* Walker to find sublink subqueries for AcquireRewriteLocks
*/
static bool
! acquireLocksOnSubLinks(Node *node, void *context)
{
if (node == NULL)
return false;
--- 283,289 ----
* Walker to find sublink subqueries for AcquireRewriteLocks
*/
static bool
! acquireLocksOnSubLinks(Node *node, acquireLocksOnSubLinks_context *context)
{
if (node == NULL)
return false;
*************** acquireLocksOnSubLinks(Node *node, void
*** 265,271 ****
SubLink *sub = (SubLink *) node;
/* Do what we came for */
! AcquireRewriteLocks((Query *) sub->subselect, false);
/* Fall through to process lefthand args of SubLink */
}
--- 292,298 ----
SubLink *sub = (SubLink *) node;
/* Do what we came for */
! AcquireRewriteLocks((Query *) sub->subselect, context->for_execute, false);
/* Fall through to process lefthand args of SubLink */
}
*************** rewriteRuleAction(Query *parsetree,
*** 307,312 ****
--- 334,342 ----
int rt_length;
Query *sub_action;
Query **sub_action_ptr;
+ acquireLocksOnSubLinks_context context;
+
+ context.for_execute = true;
/*
* Make modifiable copies of rule action and qual (what we're passed are
*************** rewriteRuleAction(Query *parsetree,
*** 318,325 ****
/*
* Acquire necessary locks and fix any deleted JOIN RTE entries.
*/
! AcquireRewriteLocks(rule_action, false);
! (void) acquireLocksOnSubLinks(rule_qual, NULL);
current_varno = rt_index;
rt_length = list_length(parsetree->rtable);
--- 348,355 ----
/*
* Acquire necessary locks and fix any deleted JOIN RTE entries.
*/
! AcquireRewriteLocks(rule_action, true, false);
! (void) acquireLocksOnSubLinks(rule_qual, &context);
current_varno = rt_index;
rt_length = list_length(parsetree->rtable);
*************** ApplyRetrieveRule(Query *parsetree,
*** 1389,1395 ****
*/
rule_action = copyObject(linitial(rule->actions));
! AcquireRewriteLocks(rule_action, forUpdatePushedDown);
/*
* Recursively expand any view references inside the view.
--- 1419,1425 ----
*/
rule_action = copyObject(linitial(rule->actions));
! AcquireRewriteLocks(rule_action, true, forUpdatePushedDown);
/*
* Recursively expand any view references inside the view.
*************** CopyAndAddInvertedQual(Query *parsetree,
*** 1713,1725 ****
/* Don't scribble on the passed qual (it's in the relcache!) */
Node *new_qual = (Node *) copyObject(rule_qual);
/*
* In case there are subqueries in the qual, acquire necessary locks and
* fix any deleted JOIN RTE entries. (This is somewhat redundant with
* rewriteRuleAction, but not entirely ... consider restructuring so that
* we only need to process the qual this way once.)
*/
! (void) acquireLocksOnSubLinks(new_qual, NULL);
/* Fix references to OLD */
ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
--- 1743,1759 ----
/* Don't scribble on the passed qual (it's in the relcache!) */
Node *new_qual = (Node *) copyObject(rule_qual);
+ acquireLocksOnSubLinks_context context;
+
+ context.for_execute = true;
+
/*
* In case there are subqueries in the qual, acquire necessary locks and
* fix any deleted JOIN RTE entries. (This is somewhat redundant with
* rewriteRuleAction, but not entirely ... consider restructuring so that
* we only need to process the qual this way once.)
*/
! (void) acquireLocksOnSubLinks(new_qual, &context);
/* Fix references to OLD */
ChangeVarNodes(new_qual, PRS2_OLD_VARNO, rt_index, 0);
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
new file mode 100644
index add5cd1..ea574ee
*** a/src/backend/utils/adt/ruleutils.c
--- b/src/backend/utils/adt/ruleutils.c
*************** make_ruledef(StringInfo buf, HeapTuple r
*** 3966,3972 ****
query = getInsertSelectQuery(query, NULL);
/* Must acquire locks right away; see notes in get_query_def() */
! AcquireRewriteLocks(query, false);
context.buf = buf;
context.namespaces = list_make1(&dpns);
--- 3966,3972 ----
query = getInsertSelectQuery(query, NULL);
/* Must acquire locks right away; see notes in get_query_def() */
! AcquireRewriteLocks(query, false, false);
context.buf = buf;
context.namespaces = list_make1(&dpns);
*************** get_query_def(Query *query, StringInfo b
*** 4108,4115 ****
* relations, and fix up deleted columns in JOIN RTEs. This ensures
* consistent results. Note we assume it's OK to scribble on the passed
* querytree!
*/
! AcquireRewriteLocks(query, false);
context.buf = buf;
context.namespaces = lcons(&dpns, list_copy(parentnamespace));
--- 4108,4118 ----
* relations, and fix up deleted columns in JOIN RTEs. This ensures
* consistent results. Note we assume it's OK to scribble on the passed
* querytree!
+ *
+ * Note that we are only deparsing the query (we are not about the execute
+ * it), so we only need an AccessShareLock on the relations it mentions.
*/
! AcquireRewriteLocks(query, false, false);
context.buf = buf;
context.namespaces = lcons(&dpns, list_copy(parentnamespace));
diff --git a/src/include/rewrite/rewriteHandler.h b/src/include/rewrite/rewriteHandler.h
new file mode 100644
index e4027bd..22551ec
*** a/src/include/rewrite/rewriteHandler.h
--- b/src/include/rewrite/rewriteHandler.h
***************
*** 18,24 ****
#include "nodes/parsenodes.h"
extern List *QueryRewrite(Query *parsetree);
! extern void AcquireRewriteLocks(Query *parsetree, bool forUpdatePushedDown);
extern Node *build_column_default(Relation rel, int attrno);
extern Query *get_view_query(Relation view);
--- 18,26 ----
#include "nodes/parsenodes.h"
extern List *QueryRewrite(Query *parsetree);
! extern void AcquireRewriteLocks(Query *parsetree,
! bool forExecute,
! bool forUpdatePushedDown);
extern Node *build_column_default(Relation rel, int attrno);
extern Query *get_view_query(Relation view);
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #9371: pg_dump acquiring ROW EXCLUSIVE locks on tables
@ 2014-03-06 19:47 Tom Lane <tgl@sss.pgh.pa.us>
parent: Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 3+ messages in thread
From: Tom Lane @ 2014-03-06 19:47 UTC (permalink / raw)
To: Dean Rasheed <dean.a.rasheed@gmail.com>; +Cc: pgsql-bugs
Dean Rasheed <dean.a.rasheed@gmail.com> writes:
> Here's a patch for HEAD along those lines.
> I've tested it on our production data and confirmed that with this
> patch pg_dump no longer acquires exclusive locks. I think this should
> be back-patched, since we do promise that pg_dump does not block other
> readers or writers.
I think this patch looks generally sane, and I agree that the excess
locking is a bug worthy of being back-patched. But is anyone concerned
about changing the signature of AcquireRewriteLocks() in back branches?
I can't immediately think of a reason why extensions might be calling it,
but ...
We could avoid a signature change in back branches by making
AcquireRewriteLocks() into a wrapper around some new function.
But I don't want to do it like that in HEAD, so this would create
a divergence between HEAD and back branches.
I'm inclined to think a signature change is OK. Objections?
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2014-03-06 19:47 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-02-27 11:54 BUG #9371: pg_dump acquiring ROW EXCLUSIVE locks on tables dean.a.rasheed@gmail.com
2014-03-03 15:21 ` Dean Rasheed <dean.a.rasheed@gmail.com>
2014-03-06 19:47 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox