我在一台机器上执行select * from artist where id = 170 for update;
然后立即到另一台机器上执行update artist  set artist_labelmobile_name = 'gghhhhhgg' where id = 170;为什么能执行呢  不是在第一台机器上锁定了么 

解决方案 »

  1.   


    第一个连接:
    t_girl=# select * from t;
     id |       username       
    ----+----------------------
      1 | wangwei             
      2 | meimei              
      3 | Hunter Valley       
      4 | I love              
      5 | shizhu              
      6 | shicui              
      7 | cuihua              
      8 | wanli               
      9 | shi4zhu             
     10 | sh4icui             
    (10 rows)
    要保证不自动提交
    t_girl=# begin;
    BEGIN
    t_girl=# select * from t where id = 2 for update;
     id |       username       
    ----+----------------------
      2 | meimei              
    (1 row)t_girl=# 
    这个执行commit后 第二个连接才起作用。
    t_girl=# commit;
    COMMIT
    现在查询结果变了。
    t_girl=# select * from t where id =2;
     id |       username       
    ----+----------------------
      2 | huahua              
    (1 row)Time: 0.333 ms
    t_girl=# 
    第二个连接:
    [postgres@localhost david_yeung]$ psql -hlocalhost -d t_girl
    could not change directory to "/home/david_yeung"
    Welcome to psql 8.2.6, the PostgreSQL interactive terminal.Type:  \copyright for distribution terms
           \h for help with SQL commands
           \? for help with psql commands
           \g or terminate with semicolon to execute query
           \q to quitt_girl=# \dt
              List of relations
     Schema |   Name   | Type  |  Owner   
    --------+----------+-------+----------
     public | a        | table | postgres
     public | b        | table | postgres
     public | orders   | table | postgres
     public | products | table | postgres
     public | t        | table | postgres
     public | t1       | table | postgres
     public | t2       | table | postgres
     public | t_r      | table | postgres
     public | test     | table | postgres
    (9 rows)t_girl=# select * from t where id = 2;
     id |       username       
    ----+----------------------
      2 | meimei              
    (1 row)t_girl=# update t set username = 'huahua' where id =2;第一个连接执行了提交后这个连接起了作用:
    t_girl=# update t set username = 'huahua' where id =2;
    UPDATE 1
    Time: 37177.512 ms
    t_girl=# select * from t where id = 2;
     id |       username       
    ----+----------------------
      2 | huahua              
    (1 row)Time: 0.332 ms