Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31b15fe

Browse files
committed
Disallow LOCK TABLE outside a transaction block (or function), since this case
almost certainly represents user error. Per a gripe from Sebastian Böhmand subsequent discussion.
1 parent99e0996 commit31b15fe

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

‎doc/src/sgml/ref/declare.sgml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/declare.sgml,v 1.42 2007/10/24 23:27:07 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/declare.sgml,v 1.43 2008/11/04 00:57:19 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -175,10 +175,9 @@ DECLARE <replaceable class="parameter">name</replaceable> [ BINARY ] [ INSENSITI
175175
<productname>PostgreSQL</productname> reports an error if such a
176176
command is used outside a transaction block.
177177
Use
178-
<xref linkend="sql-begin" endterm="sql-begin-title">,
178+
<xref linkend="sql-begin" endterm="sql-begin-title"> and
179179
<xref linkend="sql-commit" endterm="sql-commit-title">
180-
and
181-
<xref linkend="sql-rollback" endterm="sql-rollback-title">
180+
(or <xref linkend="sql-rollback" endterm="sql-rollback-title">)
182181
to define a transaction block.
183182
</para>
184183

‎doc/src/sgml/ref/lock.sgml

Lines changed: 10 additions & 7 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.49 2008/09/08 00:47:40 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/lock.sgml,v 1.50 2008/11/04 00:57:19 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -160,12 +160,15 @@ where <replaceable class="PARAMETER">lockmode</replaceable> is one of:
160160
</para>
161161

162162
<para>
163-
<command>LOCK TABLE</command> is useful only inside a transaction
164-
block (<command>BEGIN</>/<command>COMMIT</> pair), since the lock
165-
is dropped as soon as the transaction ends. A <command>LOCK
166-
TABLE</> command appearing outside any transaction block forms a
167-
self-contained transaction, so the lock will be dropped as soon as
168-
it is obtained.
163+
<command>LOCK TABLE</> is useless outside a transaction block: the lock
164+
would remain held only to the completion of the statement. Therefore
165+
<productname>PostgreSQL</productname> reports an error if <command>LOCK</>
166+
is used outside a transaction block.
167+
Use
168+
<xref linkend="sql-begin" endterm="sql-begin-title"> and
169+
<xref linkend="sql-commit" endterm="sql-commit-title">
170+
(or <xref linkend="sql-rollback" endterm="sql-rollback-title">)
171+
to define a transaction block.
169172
</para>
170173

171174
<para>

‎src/backend/tcop/utility.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.299 2008/10/10 13:48:05 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/tcop/utility.c,v 1.300 2008/11/04 00:57:19 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -938,6 +938,11 @@ ProcessUtility(Node *parsetree,
938938
break;
939939

940940
caseT_LockStmt:
941+
/*
942+
* Since the lock would just get dropped immediately, LOCK TABLE
943+
* outside a transaction block is presumed to be user error.
944+
*/
945+
RequireTransactionChain(isTopLevel,"LOCK TABLE");
941946
LockTableCommand((LockStmt*)parsetree);
942947
break;
943948

‎src/test/regress/expected/privileges.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ INSERT INTO atest1 VALUES (1, 'one');
4545
DELETE FROM atest1;
4646
UPDATE atest1 SET a = 1 WHERE b = 'blech';
4747
TRUNCATE atest1;
48+
BEGIN;
4849
LOCK atest1 IN ACCESS EXCLUSIVE MODE;
50+
COMMIT;
4951
REVOKE ALL ON atest1 FROM PUBLIC;
5052
SELECT * FROM atest1;
5153
a | b
@@ -102,8 +104,10 @@ DELETE FROM atest2; -- fail
102104
ERROR: permission denied for relation atest2
103105
TRUNCATE atest2; -- fail
104106
ERROR: permission denied for relation atest2
107+
BEGIN;
105108
LOCK atest2 IN ACCESS EXCLUSIVE MODE; -- fail
106109
ERROR: permission denied for relation atest2
110+
COMMIT;
107111
COPY atest2 FROM stdin; -- fail
108112
ERROR: permission denied for relation atest2
109113
GRANT ALL ON atest1 TO PUBLIC; -- fail
@@ -155,7 +159,9 @@ DELETE FROM atest2; -- fail
155159
ERROR: permission denied for relation atest2
156160
TRUNCATE atest2; -- fail
157161
ERROR: permission denied for relation atest2
162+
BEGIN;
158163
LOCK atest2 IN ACCESS EXCLUSIVE MODE; -- ok
164+
COMMIT;
159165
COPY atest2 FROM stdin; -- fail
160166
ERROR: permission denied for relation atest2
161167
-- checks in subquery, both fail

‎src/test/regress/sql/privileges.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ INSERT INTO atest1 VALUES (1, 'one');
4848
DELETEFROM atest1;
4949
UPDATE atest1SET a=1WHERE b='blech';
5050
TRUNCATE atest1;
51+
BEGIN;
5152
LOCK atest1IN ACCESS EXCLUSIVE MODE;
53+
COMMIT;
5254

5355
REVOKE ALLON atest1FROM PUBLIC;
5456
SELECT*FROM atest1;
@@ -80,7 +82,9 @@ SELECT * FROM atest1 FOR UPDATE; -- ok
8082
SELECT*FROM atest2 FORUPDATE;-- fail
8183
DELETEFROM atest2;-- fail
8284
TRUNCATE atest2;-- fail
85+
BEGIN;
8386
LOCK atest2IN ACCESS EXCLUSIVE MODE;-- fail
87+
COMMIT;
8488
COPY atest2FROM stdin;-- fail
8589
GRANT ALLON atest1 TO PUBLIC;-- fail
8690

@@ -105,7 +109,9 @@ SELECT * FROM atest1 FOR UPDATE; -- fail
105109
SELECT*FROM atest2 FORUPDATE;-- fail
106110
DELETEFROM atest2;-- fail
107111
TRUNCATE atest2;-- fail
112+
BEGIN;
108113
LOCK atest2IN ACCESS EXCLUSIVE MODE;-- ok
114+
COMMIT;
109115
COPY atest2FROM stdin;-- fail
110116

111117
-- checks in subquery, both fail

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp