title: what is the practical use of FOR KEY SHARE In postgres?
body: <p>I am unable to understand where i will practically use it.
  According to the documentation <a href="https://www.postgresql.org/docs/9.4/explicit-locking.html" rel="nofollow noreferrer">here</a> it says</p>
  
  <blockquote>
    <p>Behaves similarly to FOR SHARE, except that the lock is weaker: SELECT
    FOR UPDATE is blocked, but not SELECT FOR NO KEY UPDATE. A key-shared
    <strong>lock blocks other transactions from performing DELETE or any UPDATE</strong>
    <strong>that changes the key values</strong>, but not other UPDATE, and neither does it
    prevent SELECT FOR NO KEY UPDATE, SELECT FOR SHARE, or SELECT FOR KEY
    SHARE.</p>
  </blockquote>
  
  <p>But i tried and it is allowing all updates possible . i even tried changing the keys. May be I failed to understand what the docs refers to as <strong>keys</strong> (I thought it was foreign key <a href="https://stackoverflow.com/questions/45073125/for-key-share-what-are-key-values?rq=1">from this answer</a> and because it did not get blocked as said in docs i tried updating all and everything worked without getting blocked). I experimented this by having two psql terminals emulating two concurrent transactions.</p>
  
  <p><strong>Transaction 1</strong></p>
  
  <pre><code>db1=&gt; begin;
  BEGIN
  db1=&gt; SELECT * from table_base FOR key share;
   base_id | foreign_id | nonkey1 
  ---------+------------+---------
   112 |          2 | plaexpl
    21 |          2 | harish
   111 |          2 | harish
  (3 rows)
  
  db1=&gt; select * from table_foreign ;
  foreign_id |  value  
  ------------+---------
        2 | val2
       12 | val1new
       44 | newval3
  (3 rows)
  
  db1=&gt; \d table_base 
                    Table "public.table_base"
   Column   |         Type          | Collation | Nullable | Default 
   ------------+-----------------------+-----------+----------+---------
   base_id    | integer               |           |          | 
   foreign_id | integer               |           |          | 
   nonkey1    | character varying(50) |           |          | 
   Foreign-key constraints:
    "table_base_foreign_id_fkey" FOREIGN KEY (foreign_id) REFERENCES 
   table_foreign(foreign_id) ON UPDATE CASCADE
  </code></pre>
  
  <p>Transaction 2</p>
  
  <pre><code>db1=&gt; begin;
  BEGIN
  db1=&gt; UPDATE table_base set base_id = 221 where base_id=21;
  UPDATE 1
  db1=&gt; UPDATE table_base set foreign_id = 12 where nonkey1='harish';
  UPDATE 2
  db1=&gt; UPDATE table_base set nonkey1='newharish' where nonkey1='harish';
  UPDATE 2
  db1=&gt; end;
  COMMIT
  db1=&gt; SELECT * from table_base;
   base_id | foreign_id |  nonkey1  
  ---------+------------+-----------
       112 |          2 | plaexpl
       111 |         12 | newharish
       221 |         12 | newharish
  (3 rows)
  
  db1=&gt; begin;
  BEGIN
  db1=&gt; UPDATE table_foreign set foreign_id = 33 where value = 'val1new';
  UPDATE 1
  db1=&gt; UPDATE table_foreign set value ='newvalfor33' where foreign_id = 33;
  UPDATE 1
  db1=&gt; end;
  COMMIT
  db1=&gt; SELECT * from table_foreign ;
   foreign_id |    value    
  ------------+-------------
            2 | val2
           44 | newval3
           33 | newvalfor33
  (3 rows)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/NUDjx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NUDjx.png" alt="enter image description here"></a></p>
  
  <p>More info about the tables in above example
  <a href="https://i.stack.imgur.com/gtUhS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gtUhS.png" alt="enter image description here"></a>
  If it is allowing all updates, then what is the difference between 'FOR KEY SHARE' and normal 'SELECT' (except that it blocks SELECT FOR UPDATE).
  what is the practical use of this ?</p>