<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                <span style="display:none">https://blog.csdn.net/greywolf0824/article/details/85072530</span> [TOC] PostgreSQL中有兩類鎖:表級鎖和行級鎖。當要查詢、插入、更新、刪除表中數據時,首先要獲得表級鎖,然后獲得行級鎖。 # <span style="font-size:15px">**1、表級鎖**<span> <table border="1" cellspacing="0"> <tbody> <tr> <td style="width:77.75pt;"><p style="margin-left:0cm;"><span style="color:#000000;">鎖</span><span style="color:#000000;">模式</span></p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">解釋</p></td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Access Share</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">只與Access Exclusive鎖模式沖突。</p></td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">查詢命令(Select command)將會在它查詢的表上獲取Access Shared鎖,一般地,任何一個對表上的只讀查詢操作都將獲取這種類型鎖。</p></td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Row Share</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">與Exclusive和Access Exclusive鎖模式沖突。</p> </td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">Select for update和Select for share命令將獲得這種類型鎖,并且所有被引用但沒有for update 的表上會加上Access Shared鎖。</p></td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Row Exclusive</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">與Share,Shared Row Exclusive,Exclusive,Access Exclusive模式沖突。</p></td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;"> Update/Delete/Insert命令會在目標表上獲得這種類型的鎖,并且在其它被引用的表上加上Access Share鎖,一般地,更改表數據的命令都將在這張表上獲得Row Exclusive鎖。</p> </td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Share Update Exclusive</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">Share Update Exclusive,Share,Share Row Exclusive,Exclusive,Access exclusive模式沖突,這種模式保護一張表不被并發的模式更改和Vacuum。</p></td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">Vacuum(without full),Analyze 和 Create index concur-ently命令會獲得這種類型鎖。</p></td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Share</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">與Row Exclusive,Shared Update Exclusive,Share Row Exclusive,Exclusive,Access exclusive鎖模式沖突,這種模式保護一張表數據不被并發的更改。</p></td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">Create index命令會獲得這種鎖模式。</p></td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Share Row Exclusive</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">與Row Exclusive,Share Update Exclusive,Shared,Shared Row Exclusive,Exclusive,Access Exclusive鎖模式沖突。</p></td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">任何PostgreSQL命令不會自動獲得這種類型的鎖。</p></td> </tr> <tr> <td rowspan="2" style="width:77.75pt;"><p style="margin-left:0cm;">Exclusive</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">與ROW Share , Row Exclusive, Share &nbsp;Update &nbsp;Exclusive, Share , Share &nbsp;Row Exclusive, Exclusive, Access Exclusive模式沖突,這種鎖模式僅能與Access Share 模式并發,換句話說,只有讀操作可以和持有Exclusive鎖的事務并行。</p></td> </tr> <tr> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">任何PostgreSQL命令不會自動獲得這種類型的鎖。</p></td> </tr> <tr> <td style="width:77.75pt;"><p style="margin-left:0cm;">Access Exclusive</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">與所有模式鎖沖突(Access Share,Row Share,Row Exclusive,Share&nbsp; Update Exclusive,Share , Share Row Exclusive,Exclusive,Access Exclusive),這種模式保證了當前只有一個人訪問這張表;ALTER TABLE,DROP TABLE,TRUNCATE,REINDEX,CLUSTER,VACUUM FULL 命令會獲得這種類型鎖,在Lock table 命令中,如果沒有申明其它模式,它也是默認模式。</p></td> </tr> </tbody> </table> # <span style="font-size:15px">**2、表級鎖的沖突矩陣**</span> “N”表示這兩種表沖突,也就是不同的進程不能同時持有這兩種鎖 <table border="1" cellspacing="0"> <tbody> <tr> <td rowspan="2" style="width:56.45pt;"><p style="margin-left:0cm;">請求的鎖模式</p></td> <td colspan="8" style="width:358.35pt;"><p style="margin-left:0cm;">已申請到的鎖模式</p></td> </tr> <tr> <td style="width:48.05pt;"><p style="margin-left:0cm;">Access Share</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Row Share</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Row Exclusive</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Share Update Exclusive</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Share</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Share Row Exclusive</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Exclusive</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Access Exclusive</p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Access Share</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Row Share</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Row Exclusive</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Share Update Exclusive</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Share</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Share Row Exclusive</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Exclusive</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;">Y</p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> <tr> <td style="width:56.45pt;"><p style="margin-left:0cm;">Access Exclusive</p></td> <td style="width:48.05pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:39.4pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> <td style="width:46.3pt;"><p style="margin-left:0cm;"><span style="color:#ff0000;">N</span></p></td> </tr> </tbody> </table> # <span style="font-size:15px">**3、表級鎖對應的操作**</span> <table border="1" cellspacing="0"> <tbody> <tr> <td style="vertical-align:top;width:113.15pt;"><p style="margin-left:0cm;">鎖類型</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">對應的數據庫操作</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Access Share</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">select</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Row Share</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">select for update, select for share</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Row Exclusive</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">update,delete,insert</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Share Update Exclusive</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">vacuum(without full),analyze,create index concurrently</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Share</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">create index</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Share Row Exclusive</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">任何Postgresql命令不會自動獲得這種類型的鎖</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Exclusive</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">任何Postgresql命令不會自動獲得這種類型的鎖</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">Access Exclusive</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">alter table,drop table,truncate,reindex,cluster,vacuum full</p></td> </tr> </tbody> </table> # <span style="font-size:15px">**4、行級鎖**<span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;行級鎖模式只有兩種,分別是共享鎖和排他鎖,或者說是讀鎖或寫鎖。由于多版本的實現,實際讀取行數據時,并不會在行上執行任何鎖。 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;特定行上的行級鎖是在行被更新的時候自動請求的(或者被刪除時或標記為更新)。 鎖一直保持到事務提交或者回滾。行級鎖不影響對數據的查詢;它們只阻塞對同一行的寫入。 要在不修改某行的前提下請求在該行的行級鎖,用 SELECT FOR UPDATE 選取該行。請注意一旦我們請求了特定的行級鎖,那么該事務就可以多次對該行進行更新而不用擔心沖突。 <table border="1" cellspacing="0"> <tbody> <tr> <td style="width:77.75pt;">鎖</td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">解釋</p></td> </tr> <tr> <td style="width:77.75pt;"><p style="margin-left:0cm;">FOR UPDATE</p></td> <td style="vertical-align:top;width:337.05pt;"><p style="margin-left:0cm;">FOR UPDATE會導致由SELECT語句檢索到的行被鎖定,就好像它們要被更新。這可以阻止它們被其他事務鎖定、修改或者刪除,一直到當前事務結束。也就是說其他嘗試UPDATE、DELETE、SELECT FOR UPDATE、SELECT FOR NO KEY UPDATE、SELECT FOR SHARE或者SELECT FOR KEY SHARE這些行的事務將被阻塞,直到當前事務結束。反過來,SELECT FOR UPDATE將等待已經在相同行上運行以上這些命令的并發事務,并且接著鎖定并且返回被更新的行(或者沒有行,因為行可能已被刪除)。不過,在一個REPEATABLE READ或SERIALIZABLE事務中,如果一個要被鎖定的行在事務開始后被更改,將會拋出一個錯誤</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">FOR NO KEY UPDATE</p></td> <td style="vertical-align:top;width:301.65pt;"> <p style="margin-left:0cm;">行為與FOR UPDATE類似,不過獲得的鎖較弱:這種鎖將不會阻塞嘗試在相同行上獲得鎖的SELECT FOR KEY SHARE命令。任何不獲取FOR UPDATE鎖的UPDATE也會獲得這種鎖模式。</p> </td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">FOR SHARE</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">行為與FOR NO KEY UPDATE類似,不過它在每個檢索到的行上獲得一個共享鎖而不是排他鎖。一個共享鎖會阻塞其他事務在這些行上執行UPDATE、DELETE、SELECT FOR UPDATE或者SELECT FOR NO KEY UPDATE,但是它不會阻止它們執行SELECT FOR SHARE或者SELECT FOR KEY SHARE</p></td> </tr> <tr> <td style="width:113.15pt;"><p style="margin-left:0cm;">FOR KEY SHARE</p></td> <td style="vertical-align:top;width:301.65pt;"><p style="margin-left:0cm;">行為與FOR SHARE類似,不過鎖較弱:SELECT FOR UPDATE會被阻塞,但是SELECT FOR NO KEY UPDATE不會被阻塞。一個鍵共享鎖會阻塞其他事務執行修改鍵值的DELETE或者UPDATE,但不會阻塞其他UPDATE,也不會阻止SELECT FOR NO KEY UPDATE、SELECT FOR SHARE或者SELECT FOR KEY SHARE。</p></td> </tr> </tbody> </table> # <span style="font-size:15px">**5、行級鎖的沖突矩陣**<span> <table summary="沖突的行級鎖" border="1"> <colgroup> <col class="col1"> <col class="lockst"> <col class="col3"> <col class="col4"> <col class="lockend"> </colgroup> <thead> <tr> <th rowspan="2">要求的鎖模式</th> <th colspan="4">當前的鎖模式</th> </tr> <tr> <th>FOR KEY SHARE</th> <th>FOR SHARE</th> <th>FOR NO KEY UPDATE</th> <th>FOR UPDATE</th> </tr> </thead> <tbody> <tr> <td>FOR KEY SHARE</td> <td align="center">Y</td> <td align="center">Y</td> <td align="center">Y</td> <td align="center">N</td> </tr> <tr> <td>FOR SHARE</td> <td align="center">Y</td> <td align="center">Y</td> <td align="center">N</td> <td align="center">N</td> </tr> <tr> <td>FOR NO KEY UPDATE</td> <td align="center">Y</td> <td align="center">N</td> <td align="center">N</td> <td align="center">N</td> </tr> <tr> <td>FOR UPDATE</td> <td align="center">N</td> <td align="center">N</td> <td align="center">N</td> <td align="center">N</td> </tr> </tbody> </table>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看