diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2009-10-27 17:11:30 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2009-10-27 17:11:30 +0000 |
commit | bd02b48ba44c6e0593320a9c3e071ba78efe1c04 (patch) | |
tree | e7707af135ae9d537dbd43fe38c8aa5a46b9b25a /src/backend/parser/parse_relation.c | |
parent | 0dfba4e98f7ef724839a6f446d96040645b9b487 (diff) | |
download | postgresql-bd02b48ba44c6e0593320a9c3e071ba78efe1c04.tar.gz postgresql-bd02b48ba44c6e0593320a9c3e071ba78efe1c04.zip |
Make FOR UPDATE/SHARE in the primary query not propagate into WITH queries;
for example in
WITH w AS (SELECT * FROM foo) SELECT * FROM w, bar ... FOR UPDATE
the FOR UPDATE will now affect bar but not foo. This is more useful and
consistent than the original 8.4 behavior, which tried to propagate FOR UPDATE
into the WITH query but always failed due to assorted implementation
restrictions. Even though we are in process of removing those restrictions,
it seems correct on philosophical grounds to not let the outer query's
FOR UPDATE affect the WITH query.
In passing, fix isLockedRel which frequently got things wrong in
nested-subquery cases: "FOR UPDATE OF foo" applies to an alias foo in the
current query level, not subqueries. This has been broken for a long time,
but it doesn't seem worth back-patching further than 8.4 because the actual
consequences are minimal. At worst the parser would sometimes get
RowShareLock on a relation when it should be AccessShareLock or vice versa.
That would only make a difference if someone were using ExclusiveLock
concurrently, which no standard operation does, and anyway FOR UPDATE
doesn't result in visible changes so it's not clear that the someone would
notice any problem. Between that and the fact that FOR UPDATE barely works
with subqueries at all in existing releases, I'm not excited about worrying
about it.
Diffstat (limited to 'src/backend/parser/parse_relation.c')
-rw-r--r-- | src/backend/parser/parse_relation.c | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index b506c042c54..ffa5e8ac68b 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.142 2009/06/11 14:49:00 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.142.2.1 2009/10/27 17:11:30 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -41,7 +41,6 @@ static RangeTblEntry *scanNameSpaceForRelid(ParseState *pstate, Oid relid, int location); static void markRTEForSelectPriv(ParseState *pstate, RangeTblEntry *rte, int rtindex, AttrNumber col); -static bool isLockedRel(ParseState *pstate, char *refname); static void expandRelation(Oid relid, Alias *eref, int rtindex, int sublevels_up, int location, bool include_dropped, @@ -920,7 +919,7 @@ addRangeTableEntry(ParseState *pstate, * to a rel in a statement, be careful to get the right access level * depending on whether we're doing SELECT FOR UPDATE/SHARE. */ - lockmode = isLockedRel(pstate, refname) ? RowShareLock : AccessShareLock; + lockmode = isLockedRefname(pstate, refname) ? RowShareLock : AccessShareLock; rel = parserOpenTable(pstate, relation, lockmode); rte->relid = RelationGetRelid(rel); @@ -1465,40 +1464,46 @@ addRangeTableEntryForCTE(ParseState *pstate, /* * Has the specified refname been selected FOR UPDATE/FOR SHARE? * - * Note: we pay no attention to whether it's FOR UPDATE vs FOR SHARE. + * This is used when we have not yet done transformLockingClause, but need + * to know the correct lock to take during initial opening of relations. + * + * Note: we pay no attention to whether it's FOR UPDATE vs FOR SHARE, + * since the table-level lock is the same either way. */ -static bool -isLockedRel(ParseState *pstate, char *refname) +bool +isLockedRefname(ParseState *pstate, const char *refname) { - /* Outer loop to check parent query levels as well as this one */ - while (pstate != NULL) + ListCell *l; + + /* + * If we are in a subquery specified as locked FOR UPDATE/SHARE from + * parent level, then act as though there's a generic FOR UPDATE here. + */ + if (pstate->p_locked_from_parent) + return true; + + foreach(l, pstate->p_locking_clause) { - ListCell *l; + LockingClause *lc = (LockingClause *) lfirst(l); - foreach(l, pstate->p_locking_clause) + if (lc->lockedRels == NIL) { - LockingClause *lc = (LockingClause *) lfirst(l); + /* all tables used in query */ + return true; + } + else + { + /* just the named tables */ + ListCell *l2; - if (lc->lockedRels == NIL) - { - /* all tables used in query */ - return true; - } - else + foreach(l2, lc->lockedRels) { - /* just the named tables */ - ListCell *l2; - - foreach(l2, lc->lockedRels) - { - RangeVar *thisrel = (RangeVar *) lfirst(l2); + RangeVar *thisrel = (RangeVar *) lfirst(l2); - if (strcmp(refname, thisrel->relname) == 0) - return true; - } + if (strcmp(refname, thisrel->relname) == 0) + return true; } } - pstate = pstate->parentParseState; } return false; } |