Skip to content

Commit 0b86ade

Browse files
committed
Add NOWAIT option to LOCK command
1 parent 60a068b commit 0b86ade

File tree

9 files changed

+56
-15
lines changed

9 files changed

+56
-15
lines changed

doc/src/sgml/ref/lock.sgml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/lock.sgml,v 1.40 2003/12/14 00:05:29 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/lock.sgml,v 1.41 2004/03/11 01:47:35 ishii Exp $
33
PostgreSQL documentation
44
-->
55

@@ -20,7 +20,7 @@ PostgreSQL documentation
2020

2121
<refsynopsisdiv>
2222
<synopsis>
23-
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable> [, ...] [ IN <replaceable class="PARAMETER">lockmode</replaceable> MODE ]
23+
LOCK [ TABLE ] <replaceable class="PARAMETER">name</replaceable> [, ...] [ IN <replaceable class="PARAMETER">lockmode</replaceable> MODE ] [ NOWAIT ]
2424

2525
where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
2626

@@ -34,8 +34,10 @@ where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
3434

3535
<para>
3636
<command>LOCK TABLE</command> obtains a table-level lock, waiting if
37-
necessary for any conflicting locks to be released. Once obtained,
38-
the lock is held for the remainder of the current transaction.
37+
necessary for any conflicting locks to be released.
38+
If <literal>NOWAIT</literal> is given, <command>LOCK TABLE</command>
39+
does not wait for acquiring lock, and throws an error instead.
40+
Once obtained, the lock is held for the remainder of the current transaction.
3941
(There is no <command>UNLOCK TABLE</command> command; locks are always
4042
released at transaction end.)
4143
</para>

src/backend/access/heap/heapam.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.162 2004/01/16 20:51:30 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.163 2004/03/11 01:47:35 ishii Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -464,6 +464,33 @@ relation_open(Oid relationId, LOCKMODE lockmode)
464464
return r;
465465
}
466466

467+
Relation
468+
conditional_relation_open(Oid relationId, LOCKMODE lockmode, bool nowait)
469+
{
470+
Relation r;
471+
472+
Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);
473+
474+
/* The relcache does all the real work... */
475+
r = RelationIdGetRelation(relationId);
476+
477+
if (!RelationIsValid(r))
478+
elog(ERROR, "could not open relation with OID %u", relationId);
479+
480+
if (lockmode != NoLock)
481+
{
482+
if (nowait)
483+
{
484+
if (!ConditionalLockRelation(r, lockmode))
485+
elog(ERROR, "could not aquire relation lock");
486+
}
487+
else
488+
LockRelation(r, lockmode);
489+
}
490+
491+
return r;
492+
}
493+
467494
/* ----------------
468495
* relation_openrv - open any relation specified by a RangeVar
469496
*

src/backend/commands/lockcmds.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/lockcmds.c,v 1.8 2003/11/29 19:51:47 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/lockcmds.c,v 1.9 2004/03/11 01:47:35 ishii Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -59,7 +59,7 @@ LockTableCommand(LockStmt *lockstmt)
5959
aclcheck_error(aclresult, ACL_KIND_CLASS,
6060
get_rel_name(reloid));
6161

62-
rel = relation_open(reloid, lockstmt->mode);
62+
rel = conditional_relation_open(reloid, lockstmt->mode, lockstmt->nowait);
6363

6464
/* Currently, we only allow plain tables to be locked */
6565
if (rel->rd_rel->relkind != RELKIND_RELATION)

src/backend/nodes/copyfuncs.c

Lines changed: 2 additions & 1 deletion
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-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.277 2004/01/14 23:01:54 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.278 2004/03/11 01:47:35 ishii Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2316,6 +2316,7 @@ _copyLockStmt(LockStmt *from)
23162316

23172317
COPY_NODE_FIELD(relations);
23182318
COPY_SCALAR_FIELD(mode);
2319+
COPY_SCALAR_FIELD(nowait);
23192320

23202321
return newnode;
23212322
}

src/backend/nodes/equalfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.215 2004/01/14 23:01:55 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.216 2004/03/11 01:47:35 ishii Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1252,6 +1252,7 @@ _equalLockStmt(LockStmt *a, LockStmt *b)
12521252
{
12531253
COMPARE_NODE_FIELD(relations);
12541254
COMPARE_SCALAR_FIELD(mode);
1255+
COMPARE_SCALAR_FIELD(nowait);
12551256

12561257
return true;
12571258
}

src/backend/parser/gram.y

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.447 2004/03/09 05:05:41 momjian Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.448 2004/03/11 01:47:37 ishii Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -169,6 +169,7 @@ static void doNegateFloat(Value *v);
169169
%type <ival> opt_lock lock_type cast_context
170170
%type <boolean> opt_force opt_or_replace transaction_access_mode
171171
opt_grant_grant_option opt_revoke_grant_option
172+
opt_nowait
172173

173174
%type <boolean> like_including_defaults
174175

@@ -375,7 +376,7 @@ static void doNegateFloat(Value *v);
375376
MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
376377

377378
NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
378-
NOCREATEUSER NONE NOT NOTHING NOTIFY NOTNULL NULL_P
379+
NOCREATEUSER NONE NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P
379380
NULLIF NUMERIC
380381

381382
OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
@@ -4347,12 +4348,13 @@ DeleteStmt: DELETE_P FROM relation_expr where_clause
43474348
}
43484349
;
43494350

4350-
LockStmt: LOCK_P opt_table qualified_name_list opt_lock
4351+
LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
43514352
{
43524353
LockStmt *n = makeNode(LockStmt);
43534354

43544355
n->relations = $3;
43554356
n->mode = $4;
4357+
n->nowait = $5;
43564358
$$ = (Node *)n;
43574359
}
43584360
;
@@ -4371,6 +4373,10 @@ lock_type: ACCESS SHARE { $$ = AccessShareLock; }
43714373
| ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
43724374
;
43734375

4376+
opt_nowait: NOWAIT { $$ = TRUE; }
4377+
| /*EMPTY*/ { $$ = FALSE; }
4378+
;
4379+
43744380

43754381
/*****************************************************************************
43764382
*
@@ -7683,6 +7689,7 @@ reserved_keyword:
76837689
| LOCALTIMESTAMP
76847690
| NEW
76857691
| NOT
7692+
| NOWAIT
76867693
| NULL_P
76877694
| OFF
76887695
| OFFSET

src/backend/parser/keywords.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/keywords.c,v 1.146 2004/03/09 05:05:41 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/keywords.c,v 1.147 2004/03/11 01:47:40 ishii Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -213,6 +213,7 @@ static const ScanKeyword ScanKeywords[] = {
213213
{"nothing", NOTHING},
214214
{"notify", NOTIFY},
215215
{"notnull", NOTNULL},
216+
{"nowait", NOWAIT},
216217
{"null", NULL_P},
217218
{"nullif", NULLIF},
218219
{"numeric", NUMERIC},

src/include/access/heapam.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.86 2003/11/29 22:40:55 pgsql Exp $
10+
* $PostgreSQL: pgsql/src/include/access/heapam.h,v 1.87 2004/03/11 01:47:41 ishii Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -129,6 +129,7 @@ extern Datum heap_getsysattr(HeapTuple tup, int attnum, bool *isnull);
129129
/* heapam.c */
130130

131131
extern Relation relation_open(Oid relationId, LOCKMODE lockmode);
132+
extern Relation conditional_relation_open(Oid relationId, LOCKMODE lockmode, bool nowait);
132133
extern Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode);
133134
extern Relation relation_openr(const char *sysRelationName, LOCKMODE lockmode);
134135
extern void relation_close(Relation relation, LOCKMODE lockmode);

src/include/nodes/parsenodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.253 2004/01/14 23:01:55 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.254 2004/03/11 01:47:41 ishii Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1619,6 +1619,7 @@ typedef struct LockStmt
16191619
NodeTag type;
16201620
List *relations; /* relations to lock */
16211621
int mode; /* lock mode */
1622+
bool nowait; /* no wait mode */
16221623
} LockStmt;
16231624

16241625
/* ----------------------

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