{"id":850,"date":"2008-05-30T01:00:00","date_gmt":"2008-05-29T16:00:00","guid":{"rendered":"https:\/\/ecoop.net\/memo\/2008-05-30-1.html"},"modified":"2026-08-10T14:44:44","modified_gmt":"2026-08-10T05:44:44","slug":"2008-05-30-1","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/2008-05-30-1.html","title":{"rendered":"PostgreSQL \u30c6\u30fc\u30d6\u30eb LOCK MODE \u95a2\u4fc2\u56f3"},"content":{"rendered":"<p>\n\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u30e2\u30fc\u30c9\u306e\u95a2\u4fc2\u3092\u628a\u63e1\u3057\u3084\u3059\u3044\u3088\u3046\u306b\u56f3\u3067\u8868\u3057\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.ecoop.net\/memo\/rs\/pgsql_lock_mode.png\" alt=\"PostgreSQL LOCK MODE \u95a2\u4fc2\u56f3\"\/><br \/>\n() \u5185\u306f\u305d\u306e\u30ed\u30c3\u30af\u3092\u81ea\u52d5\u7684\u306b\u7372\u5f97\u3059\u308b\u30af\u30a8\u30ea\u3067\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u25a0\u30ed\u30c3\u30af\u3068\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<br \/>\nSQL \u306b\u304a\u3051\u308b\u6392\u4ed6\u5236\u5fa1\u306e\u65b9\u6cd5\u3068\u3057\u3066\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u30ed\u30c3\u30af\u304c\u6319\u3052\u3089\u308c\u307e\u3059\u3002<br \/>\n\u8907\u6570\u306e\u30af\u30a8\u30ea\u3092\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u3072\u3068\u3064\u306b\u307e\u3068\u3081\u308b\u3053\u3068\u3067\u3001(\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30ec\u30d9\u30eb READ COMMITTED \u306e\u5834\u5408)\u30b3\u30df\u30c3\u30c8\u6e08\u307f\u306e\u7d50\u679c\u306e\u307f\u53c2\u7167\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u4e2d\u306e\u5185\u5bb9\u3092\u5916\u90e8\u304b\u3089\u306f\u898b\u3048\u306a\u304f\u306a\u308b\u305f\u3081\u4e00\u898b\u30a2\u30c8\u30df\u30c3\u30af\u6027\u304c\u5b88\u3089\u308c\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u3057\u304b\u3057\u3001\u8907\u6570\u306e\u63a5\u7d9a\u3067\u540c\u6642\u306b\u65e2\u5b58\u306e\u540c\u3058\u30ec\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u53c2\u7167\u3068\u66f4\u65b0\u3092\u5206\u3051\u3066\u5b9f\u884c\u3059\u308b\u5834\u5408\u306a\u3069\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3060\u3051\u3067\u306f\u9650\u754c\u304c\u3042\u308b\u306e\u3082\u4e8b\u5b9f\u3067\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u305f\u3068\u3048\u3070\u4ee5\u4e0b\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<blockquote><p>CREATE TABLE foo (<br \/>\n&nbsp;&nbsp;id int NOT NULL,   &#8212; \u5546\u54c1\u306e\u8b58\u5225\u756a\u53f7<br \/>\n&nbsp;&nbsp;price int NOT NULL &#8212; \u5024\u6bb5<br \/>\n);<br \/>\nINSERT INTO foo (id, price) VALUES(1, 100); &#8212; id:1 \u306f 100 \u5186<br \/>\nINSERT INTO foo (id, price) VALUES(2, 500); &#8212; id:2 \u306f 500 \u5186\n<\/p><\/blockquote>\n<p>\u3053\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3057\u3066\u3001\u4ee5\u4e0b\u306e\u51e6\u7406\u3092\u884c\u3046\u3082\u306e\u3068\u3057\u307e\u3059\u3002\n<\/p>\n<blockquote><p>BEGIN;<br \/>\nSELECT id FROM foo WHERE price < 500; -- 500\u5186\u672a\u6e80\u306e id:1 \u3092\u5f97\u308b<br \/>\nUPDATE foo SET price = price + 500 WHERE id = 1; &#8212; 500\u5186\u672a\u6e80\u3060\u3063\u305f id:1 \u3092 500\u5186\u5897\u3057<br \/>\nCOMMIT;\n<\/p><\/blockquote>\n<p>READ COMMITED \u30ec\u30d9\u30eb\u3067\u540c\u6642\u306b\u540c\u3058\u306e\u30af\u30a8\u30ea\u304c\u5b9f\u884c\u3055\u308c\u308b\u3068\u3001id:1 \u304c 1100 \u5186\u306b\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u307f\u3067\u3053\u308c\u3092\u9632\u3050\u306b\u306f\u885d\u7a81\u3059\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u5168\u3066\u306e\u30af\u30a8\u30ea\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30ec\u30d9\u30eb\u3092\u6700\u3082\u53b3\u5bc6\u306a SERIALIZABLE \u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\n<\/p>\n<blockquote><p>&#8212; \u885d\u7a81\u56de\u907f\u7248<br \/>\nBEGIN;<br \/>\nSET TRANSACTION SERIALIZABLE; &#8212; \u3053\u306e\u63a5\u7d9a\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u30ec\u30d9\u30eb\u3092 SERIALIZABLE \u306b\u3002<br \/>\nSELECT id FROM foo WHERE price < 500; -- 500\u5186\u672a\u6e80\u306e id:1 \u3092\u5f97\u308b<br \/>\nUPDATE foo SET price = price + 500 WHERE id = 1; &#8212; 500\u5186\u672a\u6e80\u3060\u3063\u305f id:1 \u3092 500\u5186\u5897\u3057<br \/>\nCOMMIT;\n<\/p><\/blockquote>\n<p>\u305f\u3060\u3057\u3001 SERIALIZABLE \u9694\u96e2\u30ec\u30d9\u30eb\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u5b9f\u884c\u4e2d\u306b\u4ed6\u306e\u63a5\u7d9a\u3067 COMMIT \u3055\u308c\u305f\u5834\u5408\u3001\u76f4\u5217\u6027\u3092\u5b88\u308b\u305f\u3081\u306b\u30a8\u30e9\u30fc\u306b\u306a\u308a\u51e6\u7406\u304c\u7d9a\u884c\u3067\u304d\u306a\u304f\u306a\u308a\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001\u885d\u7a81\u3057\u305f\u5834\u5408\u306f ROLLBACK \u306e\u5f8c\u518d\u51e6\u7406\u3092\u884c\u3046\u5fc5\u8981\u304c\u3042\u308b\u306a\u3069, \u6b20\u70b9\u3082\u3042\u308a\u307e\u3059\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u307f\u3067\u306f\u3001\u53c2\u7167\u3092\u5b8c\u5168\u306b\u30d6\u30ed\u30c3\u30af\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u306e\u304c\u539f\u56e0\u306e\u3072\u3068\u3064\u3067\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u305d\u3053\u3067\u3001\u8aad\u307f\u8fbc\u307f\u3082\u5236\u5fa1\u3067\u304d\u308b\u30ed\u30c3\u30af\u306e\u51fa\u756a\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u25a0\u30ed\u30c3\u30af\u306e\u7372\u5f97\u65b9\u6cd5<br \/>\n\u591a\u304f\u306e\u30af\u30a8\u30ea\u306f\u6697\u9ed9\u306b\u30af\u30a8\u30ea\u5358\u4f4d\u3067\u884c\u30ec\u30d9\u30eb\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u30ed\u30c3\u30af\u306e\u7a2e\u985e\u3068\u5f71\u97ff\u3059\u308b\u7bc4\u56f2\u306b\u3064\u3044\u3066\u306f\u56f3\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n&nbsp;<br \/>\n\u8907\u6570\u306e\u30af\u30a8\u30ea\u3067\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3059\u308b\u306b\u306f LOCK \u69cb\u6587\u3092\u7528\u3044\u307e\u3059\u3002<br \/>\nLOCK \u69cb\u6587\u3067\u53d6\u5f97\u3067\u304d\u308b\u30ed\u30c3\u30af\u306f\u30c6\u30fc\u30d6\u30eb\u306b\u5bfe\u3059\u308b\u30ed\u30c3\u30af\u306e\u307f\u3067\u3059\u3002\n<\/p>\n<blockquote><p>BEGIN;<br \/>\nLOCK foo_table IN EXCLUSIVE MODE; &#8212; EXCLUSIVE \u30ed\u30c3\u30af\u7372\u5f97\u3002<br \/>\nSELECT * FROM foo_table;<br \/>\nUPDATE foo_table SET &#8230;<br \/>\n&#8212; \u305d\u306e\u4ed6\u4f55\u304b\u8272\u3005\u306a\u30af\u30a8\u30ea&#8230;<br \/>\nCOMMIT;\n<\/p><\/blockquote>\n<p>\u305f\u3068\u3048\u3070 EXCLUSIVE \u30e2\u30fc\u30c9\u3067\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3059\u308b\u3068\u3001\u4ed6\u306e\u63a5\u7d9a\u3067\u306f SELECT, ANALYZE \u6587\u4ee5\u5916\u306e\u5168\u3066\u306e\u30af\u30a8\u30ea\u3001\u304a\u3088\u3073 ACCESS_SHARE \u4ee5\u5916\u306e\u5168\u3066\u306e\u30ed\u30c3\u30af\u3092\u660e\u793a\u7684\u306b\u7372\u5f97\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u30ed\u30c3\u30af\u5f85\u3061\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u203b\u5177\u4f53\u7684\u306b\u306f UPDATE, DELETE, INSERT \u6587\u304a\u3088\u3073 SELECT &#8230; FROM table FOR &#8230;, CREATE INDEX, ALTER TABLE, DROP TABLE, REINDEX, CLUSTER, VACUUM, VACUUM FULL \u6587\u306e\u5b9f\u884c\u3092\u9632\u304e\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001ACCESS EXCLUSIVE, EXCLUSIVE, ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE ROW EXCLUSIVE, SHARE \u306e\u660e\u793a\u30ed\u30c3\u30af\u7372\u5f97\u3092\u9632\u304e\u307e\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u25a0\u30ed\u30c3\u30af\u306e\u89e3\u653e\u65b9\u6cd5<br \/>\n\u30ed\u30c3\u30af\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u7d42\u4e86\u6642(COMMIT, ROLLBACK\u5b9f\u884c\u6642)\u306b\u81ea\u52d5\u3067\u89e3\u653e\u3055\u308c\u307e\u3059\u3002<br \/>\n\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u9014\u4e2d\u3067\u30ed\u30c3\u30af\u3060\u3051\u3092\u89e3\u653e\u3059\u308b\u65b9\u6cd5\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n&nbsp;<br \/>\n\u53c2\u8003:<br \/>\n&#8211;<a href=\"http:\/\/www.postgresql.jp\/document\/pg831doc\/html\/transaction-iso.html\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u9694\u96e2<\/a>(PostgreSQL 8.3.1 \u30de\u30cb\u30e5\u30a2\u30eb)<br \/>\n&#8211;<a href=\"http:\/\/www.postgresql.jp\/document\/pg831doc\/html\/sql-lock.html\">LOCK \u69cb\u6587<\/a>(\u540c\u4e0a)<br \/>\n&#8211;<a href=\"http:\/\/www.fireproject.jp\/feature\/postgresql\/sql-intro\/transaction-lock.html\">\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u9694\u96e2\u30ec\u30d9\u30eb\u3068\u30ed\u30c3\u30af<\/a>(\u30d5\u30a1\u30a4\u30e4\u30fc\u30d7\u30ed\u30b8\u30a7\u30af\u30c8)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u30e2\u30fc\u30c9\u306e\u95a2\u4fc2\u3092\u628a\u63e1\u3057\u3084\u3059\u3044\u3088\u3046\u306b\u56f3\u3067\u8868\u3057\u3066\u307f\u307e\u3057\u305f\u3002 () \u5185\u306f\u305d\u306e\u30ed\u30c3\u30af\u3092\u81ea\u52d5\u7684\u306b\u7372\u5f97\u3059\u308b\u30af\u30a8\u30ea\u3067\u3059\u3002 &nbsp; \u25a0\u30ed\u30c3\u30af\u3068\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3 SQL \u306b\u304a\u3051\u308b\u6392\u4ed6\u5236\u5fa1\u306e\u65b9\u6cd5\u3068\u3057\u3066\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3068\u30ed &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/2008-05-30-1.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">PostgreSQL \u30c6\u30fc\u30d6\u30eb LOCK MODE \u95a2\u4fc2\u56f3<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[141],"tags":[45],"class_list":["post-850","post","type-post","status-publish","format-standard","hentry","category-lifehack","tag-postgresql"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=850"}],"version-history":[{"count":1,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/850\/revisions"}],"predecessor-version":[{"id":1750,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/850\/revisions\/1750"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}