Skip to content

Commit 77a69a2

Browse files
committed
Patch to LOCK multiple tables in one LOCK command.
Neil Padgett
1 parent 49eb4f4 commit 77a69a2

File tree

7 files changed

+54
-35
lines changed

7 files changed

+54
-35
lines changed

doc/src/sgml/ref/lock.sgml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/lock.sgml,v 1.26 2001/08/04 22:01:38 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/lock.sgml,v 1.27 2001/08/10 14:30:14 momjian Exp $
33
Postgres documentation
44
-->
55

@@ -15,16 +15,16 @@ Postgres documentation
1515
LOCK
1616
</refname>
1717
<refpurpose>
18-
Explicitly lock a table inside a transaction
18+
Explicitly lock a table / tables inside a transaction
1919
</refpurpose>
2020
</refnamediv>
2121
<refsynopsisdiv>
2222
<refsynopsisdivinfo>
2323
<date>2001-07-09</date>
2424
</refsynopsisdivinfo>
2525
<synopsis>
26-
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable>
27-
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable> IN <replaceable class="PARAMETER">lockmode</replaceable> MODE
26+
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable> [, ...]
27+
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable> [, ...] IN <replaceable class="PARAMETER">lockmode</replaceable> MODE
2828

2929
where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
3030

@@ -373,6 +373,7 @@ ERROR <replaceable class="PARAMETER">name</replaceable>: Table does not exist.
373373
An example for this rule was given previously when discussing the
374374
use of SHARE ROW EXCLUSIVE mode rather than SHARE mode.
375375
</para>
376+
376377
</listitem>
377378
</itemizedlist>
378379

@@ -383,6 +384,12 @@ ERROR <replaceable class="PARAMETER">name</replaceable>: Table does not exist.
383384
</para>
384385
</note>
385386

387+
<para>
388+
When locking multiple tables, the command LOCK a, b; is equivalent to LOCK
389+
a; LOCK b;. The tables are locked one-by-one in the order specified in the
390+
<command>LOCK</command> command.
391+
</para>
392+
386393
<refsect2 id="R2-SQL-LOCK-3">
387394
<refsect2info>
388395
<date>1999-06-08</date>
@@ -406,6 +413,7 @@ ERROR <replaceable class="PARAMETER">name</replaceable>: Table does not exist.
406413
<para>
407414
<command>LOCK</command> works only inside transactions.
408415
</para>
416+
409417
</refsect2>
410418
</refsect1>
411419

src/backend/commands/command.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.138 2001/08/04 22:01:38 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.139 2001/08/10 14:30:14 momjian Exp $
1212
*
1313
* NOTES
1414
* The PerformAddAttribute() code, like most of the relation
@@ -1984,8 +1984,7 @@ needs_toast_table(Relation rel)
19841984
MAXALIGN(data_length);
19851985
return (tuple_length > TOAST_TUPLE_THRESHOLD);
19861986
}
1987-
1988-
1987+
19891988
/*
19901989
*
19911990
* LOCK TABLE
@@ -1994,26 +1993,38 @@ needs_toast_table(Relation rel)
19941993
void
19951994
LockTableCommand(LockStmt *lockstmt)
19961995
{
1997-
Relation rel;
1998-
int aclresult;
1999-
2000-
rel = heap_openr(lockstmt->relname, NoLock);
2001-
2002-
if (rel->rd_rel->relkind != RELKIND_RELATION)
2003-
elog(ERROR, "LOCK TABLE: %s is not a table", lockstmt->relname);
2004-
2005-
if (lockstmt->mode == AccessShareLock)
2006-
aclresult = pg_aclcheck(lockstmt->relname, GetUserId(), ACL_SELECT);
2007-
else
2008-
aclresult = pg_aclcheck(lockstmt->relname, GetUserId(),
2009-
ACL_UPDATE | ACL_DELETE);
1996+
List *p;
1997+
Relation rel;
1998+
1999+
/* Iterate over the list and open, lock, and close the relations
2000+
one at a time
2001+
*/
20102002

2011-
if (aclresult != ACLCHECK_OK)
2012-
elog(ERROR, "LOCK TABLE: permission denied");
2003+
foreach(p, lockstmt->rellist)
2004+
{
2005+
char* relname = strVal(lfirst(p));
2006+
int aclresult;
2007+
2008+
rel = heap_openr(relname, NoLock);
2009+
2010+
if (rel->rd_rel->relkind != RELKIND_RELATION)
2011+
elog(ERROR, "LOCK TABLE: %s is not a table",
2012+
relname);
2013+
2014+
if (lockstmt->mode == AccessShareLock)
2015+
aclresult = pg_aclcheck(relname, GetUserId(),
2016+
ACL_SELECT);
2017+
else
2018+
aclresult = pg_aclcheck(relname, GetUserId(),
2019+
ACL_UPDATE | ACL_DELETE);
20132020

2014-
LockRelation(rel, lockstmt->mode);
2021+
if (aclresult != ACLCHECK_OK)
2022+
elog(ERROR, "LOCK TABLE: permission denied");
20152023

2016-
heap_close(rel, NoLock); /* close rel, keep lock */
2024+
LockRelation(rel, lockstmt->mode);
2025+
2026+
heap_close(rel, NoLock); /* close rel, keep lock */
2027+
}
20172028
}
20182029

20192030

src/backend/nodes/copyfuncs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.150 2001/08/04 22:01:38 momjian Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.151 2001/08/10 14:30:14 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2425,8 +2425,8 @@ _copyLockStmt(LockStmt *from)
24252425
{
24262426
LockStmt *newnode = makeNode(LockStmt);
24272427

2428-
if (from->relname)
2429-
newnode->relname = pstrdup(from->relname);
2428+
Node_Copy(from, newnode, rellist);
2429+
24302430
newnode->mode = from->mode;
24312431

24322432
return newnode;

src/backend/nodes/equalfuncs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Portions Copyright (c) 1994, Regents of the University of California
2121
*
2222
* IDENTIFICATION
23-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.98 2001/08/04 22:01:38 momjian Exp $
23+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.99 2001/08/10 14:30:14 momjian Exp $
2424
*
2525
*-------------------------------------------------------------------------
2626
*/
@@ -1283,7 +1283,7 @@ _equalDropUserStmt(DropUserStmt *a, DropUserStmt *b)
12831283
static bool
12841284
_equalLockStmt(LockStmt *a, LockStmt *b)
12851285
{
1286-
if (!equalstr(a->relname, b->relname))
1286+
if (!equal(a->rellist, b->rellist))
12871287
return false;
12881288
if (a->mode != b->mode)
12891289
return false;

src/backend/parser/gram.y

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.241 2001/08/06 05:42:48 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.242 2001/08/10 14:30:14 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -3281,11 +3281,11 @@ DeleteStmt: DELETE FROM relation_expr where_clause
32813281
}
32823282
;
32833283

3284-
LockStmt: LOCK_P opt_table relation_name opt_lock
3284+
LockStmt: LOCK_P opt_table relation_name_list opt_lock
32853285
{
32863286
LockStmt *n = makeNode(LockStmt);
32873287

3288-
n->relname = $3;
3288+
n->rellist = $3;
32893289
n->mode = $4;
32903290
$$ = (Node *)n;
32913291
}

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: parsenodes.h,v 1.138 2001/08/04 22:01:39 momjian Exp $
10+
* $Id: parsenodes.h,v 1.139 2001/08/10 14:30:15 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -760,7 +760,7 @@ typedef struct VariableResetStmt
760760
typedef struct LockStmt
761761
{
762762
NodeTag type;
763-
char *relname; /* relation to lock */
763+
List *rellist; /* relations to lock */
764764
int mode; /* lock mode */
765765
} LockStmt;
766766

src/interfaces/ecpg/preproc/preproc.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ DeleteStmt: DELETE FROM relation_expr where_clause
24212421
}
24222422
;
24232423

2424-
LockStmt: LOCK_P opt_table relation_name opt_lock
2424+
LockStmt: LOCK_P opt_table relation_name_list opt_lock
24252425
{
24262426
$$ = cat_str(4, make_str("lock"), $2, $3, $4);
24272427
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy