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

Commit0e41fd5

Browse files
committed
MVCC updates.
1 parentc0b4b42 commit0e41fd5

File tree

2 files changed

+147
-43
lines changed

2 files changed

+147
-43
lines changed

‎src/man/lock.l

Lines changed: 138 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,156 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/lock.l,v 1.8 1999/06/04 04:28:54 momjian Exp $
4-
.THFETCH SQL 01/23/93 PostgreSQL PostgreSQL
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/lock.l,v 1.9 1999/06/09 03:51:40 vadim Exp $
4+
.THLOCK SQL 01/23/93 PostgreSQL PostgreSQL
55
.SH NAME
6-
lock -exclusive lock a table
6+
lock -Explicit lockofa table inside a transaction
77
.SH SYNOPSIS
88
.nf
99
\fBlock\fR [\fBtable\fR] classname
10-
[\fBin\fR [\fBrow\fR|\fBaccess\fR][\fBshare\fR|\fBexclusive\fR] |
11-
[\fBsharerowexclusive\fR]\fBmode\fR]
10+
\fBlock\fR [\fBtable\fR] classname\fBin\fR [\fBrow\fR|\fBaccess\fR]{\fBshare\fR|\fBexclusive\fR}\fBmode\fR
11+
\fBlock\fR [\fBtable\fR] classname\fBin\fR\fBsharerowexclusive\fR\fBmode\fR
1212
.fi
1313
.SH DESCRIPTION
14-
By default,
15-
.BR lock
16-
exclusive locks an entire table inside a transaction.
17-
Various options allow shared access, or row-level locking control.
14+
Available lock modes from least restrictive to most restrictive:
1815
.PP
19-
The classic use for this
20-
is the case where you want to\fBselect\fP some data, then update it
21-
inside a transaction. If you don't exclusive lock the table before the
22-
\fBselect\fP, some other user may also read the selected data, and try
23-
and do their own\fBupdate\fP, causing a deadlock while you both wait
24-
for the other to release the\fBselect\fP-induced shared lock so you can
25-
get an exclusive lock to do the\fBupdate.\fP
16+
\fBACCESSSHAREMODE\fR
17+
18+
\fBNote\fR: this lock mode is acquired automatically over tables being
19+
\queried.\fBPostgres\fR releases automatically acquired
20+
ACCESS SHARE locks after statement is done.
21+
22+
This is the least restrictive lock mode which conflicts with ACCESS EXCLUSIVE
23+
mode only. It's intended to protect table being queried from concurrent
24+
\fBALTERTABLE\fR,\fBDROPTABLE\fR and
25+
\fBVACUUM\fR statements over the same table.
26+
27+
\fBROWSHAREMODE\fR
28+
29+
\fBNote\fR: Automatically acquired by SELECT FOR UPDATE statement.
30+
31+
Conflicts with EXCLUSIVE and ACCESS EXCLUSIVE lock modes.
32+
33+
\fBROWEXCLUSIVEMODE\fR
34+
35+
\fBNote\fR: Automatically acquired by UPDATE, DELETE, INSERT statements.
36+
37+
Conflicts with SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE and ACCESS EXCLUSIVE
38+
modes. Generally means that a transaction updated/inserted some tuples in a
39+
table.
40+
41+
\fBSHAREMODE\fR
42+
43+
\fBNote\fR: Automatically acquired by CREATE INDEX statement.
44+
45+
Conflicts with ROW EXCLUSIVE, SHARE ROW EXCLUSIVE, EXCLUSIVE and ACCESS
46+
EXCLUSIVE modes. This mode protects a table against concurrent updates.
47+
48+
\fBSHAREROWEXCLUSIVEMODE\fR
49+
50+
Conflicts with ROW EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE and
51+
ACCESS EXCLUSIVE modes. This mode is more restrictive than SHARE mode
52+
because of only one transaction at time can hold this lock.
53+
54+
\fBEXCLUSIVEMODE\fR
55+
56+
Conflicts with ROW SHARE, ROW EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE,
57+
EXCLUSIVE and ACCESS EXCLUSIVE modes. This mode is yet more restrictive than
58+
SHARE ROW EXCLUSIVE one - it blocks concurrent SELECT FOR UPDATE queries.
59+
60+
\fBACCESSEXCLUSIVEMODE\fR
61+
62+
\fBNote\fR: Automatically acquired by ALTER TABLE, DROP TABLE, VACUUM
63+
statements.
64+
65+
This is the most restrictive lock mode which conflicts with all other
66+
lock modes and protects locked table from any concurrent operations.
67+
68+
\fBNote\fR: This lock mode is also acquired by first form of LOCK TABLE
69+
(i.e. without explicit lock mode option).
70+
71+
.SH USAGE
72+
.BR Postgres
73+
always uses less restrictive lock modes ever possible. LOCK TABLE statement
74+
provided for cases when you might need in more restrictive locking.
2675
.PP
27-
Another example of deadlock is where one user locks one table, and
28-
another user locks a second table. While both keep their existing
29-
locks, the first user tries to lock the second user's table, and the
30-
second user tries to lock the first user's table. Both users deadlock
31-
waiting for the tables to become available. The only solution to this
32-
is for both users to lock tables in the same order, so user's lock
33-
aquisitions and requests to not form a deadlock.
76+
For example, application run transaction at READ COMMITTED isolation level
77+
and need to ensure existance data in a table for duration of transaction. To
78+
achieve this you could use SHARE lock mode over table before querying. This
79+
will protect data from concurrent changes and provide your further read
80+
operations over table with data in their real current state, because of
81+
SHARE lock mode conflicts with ROW EXCLUSIVE one, acquired by writers, and
82+
your LOCK TABLE table IN SHARE MODE statement will wait untill concurrent
83+
write operations (if any) commit/rollback. (Note that to read data in their
84+
real current state running transaction at SERIALIZABLE isolation level you
85+
have to execute LOCK TABLE statement before execution any DML statement,
86+
when transaction defines what concurrent changes will be visible to
87+
herself).
88+
89+
If, in addition to requirements above, transaction is going to change data
90+
in a table then SHARE ROW EXCLUSIVE lock mode should be acquired to prevent
91+
deadlock conditions when two concurrent transactions would lock table in
92+
SHARE mode and than would try to change data in this table, both
93+
(implicitly) acquiring ROW EXCLUSIVE lock mode that conflicts with
94+
concurrent SHARE lock.
95+
96+
Following deadlock issue (when two transaction wait one another)
97+
touched above, you should follow two general rules to prevent
98+
deadlock conditions:
99+
100+
\fB1.Transactionshavetoacquirelocksonthesameobjectsinthesameorder.\fR
101+
102+
For example, if one application updates row R1 and than updates row R2 (in
103+
the same transaction) then second application shouldn't update row R2 if
104+
it's going update row R1 later (in single transaction). Instead, it should
105+
update R1 and R2 rows in the same order as first application.
106+
107+
\fB2.Transactionsshouldacquiretwoconflictinglockmodesonlyifoneof
108+
themisself-conflicting(i.e.maybeheldbyonetransactionattimeonly)
109+
andshouldacquiremostrestrictivemodefirst.\fR
110+
111+
Example for this rule is described above when told about using
112+
SHARE ROW EXCLUSIVE mode instead of SHARE one.
113+
114+
\fBNote\fR:\fBPostgres\fR does detect deadlocks and will rollback one of
115+
waiting transactions to resolve the deadlock.
116+
117+
.SH COMPATIBILITY
118+
LOCK TABLE statement is a\fBPostgres\fR language extension.
119+
120+
Except for ACCESS SHARE/EXCLUSIVE lock modes, all other\fBPostgres\fR lock
121+
modes and LOCK TABLE statement syntax are compatible with\fBOracle\fR
122+
ones.
123+
34124
.SH EXAMPLES
35125
.nf
36126
--
37-
-- Proper locking to prevent deadlock
127+
-- SHARE lock primary key table when going to perform
128+
-- insert into foreign key table.
38129
--
39-
begin work;
40-
lock table mytable;
41-
select * from mytable;
42-
update mytable set (x = 100);
43-
commit;
130+
BEGIN WORK;
131+
LOCK TABLE films IN SHARE MODE;
132+
SELECT id FROM films
133+
WHERE name = 'Star Wars: Episode I - The Phantom Menace';
134+
--
135+
-- Do ROLLBACK if record was not returned
136+
--
137+
INSERT INTO films_user_comments VALUES
138+
(_id_, 'GREAT! I was waiting it so long!');
139+
COMMIT WORK;
140+
141+
--
142+
-- SHARE ROW EXCLUSIVE lock primary key table when going to perform
143+
-- delete operation.
144+
--
145+
BEGIN WORK;
146+
LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
147+
DELETE FROM films_user_comments WHERE id IN
148+
(SELECT id FROM films WHERE rating < 5);
149+
DELETE FROM films WHERE rating < 5;
150+
COMMIT WORK;
151+
44152
.SH "SEE ALSO"
45153
begin(l),
46154
commit(l),
155+
set(l),
47156
select(l).

‎src/man/set.l

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/set.l,v 1.21 1999/06/04 03:44:42 momjian Exp $
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/set.l,v 1.22 1999/06/09 03:51:40 vadim Exp $
44
.TH SET SQL 05/14/97 PostgreSQL PostgreSQL
55
.SH NAME
66
set - set run-time parameters for session
@@ -81,19 +81,14 @@ The default is unlimited.
8181
.IR TIMEZONE
8282
sets your timezone.
8383
.PP
84-
.ITRANSACTIONISOLATIONLEVEL
85-
sets the current transaction's isolation level to
86-
.IR SERIALIZABLE
87-
or
88-
.IR READCOMMITTED .
89-
.IR SERIALIZABLE
90-
means that the current transaction will place a lock on every row read,
91-
so later reads in that transaction see the rows unmodified by
92-
other transactions.
93-
.IR READCOMMITTED
94-
means that the current transaction reads only committed rows.
95-
.IR READCOMMITTED
96-
is the default.
84+
\fITRANSACTIONISOLATIONLEVEL\fR sets the current transaction's isolation
85+
level to\fISERIALIZABLE\fR or\fIREADCOMMITTED\fR.\fISERIALIZABLE\fR
86+
means that the current transaction queries will read only rows committed
87+
before first DML statement (SELECT/INSERT/DELETE/UPDATE/FETCH/COPY_TO) was
88+
executed in this transaction.\fIREADCOMMITTED\fR means that the current
89+
transaction queries will read only rows committed before a query began.
90+
\fIREADCOMMITTED\fR is the default.\fBNote\fR: SQL92 standard requires
91+
\fISERIALIZABLE\fR to be the default isolation level.
9792
.PP
9893
.IR CLIENT_ENCODING|NAMES
9994
sets the character set encoding of the client. Only available if multi-byte

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp