2

と を使用する Web アプリケーションがHibernateありPostgreSQLます。

このアプリケーションにはいくつかのエンティティがあり、そのうちの 1 つが次のとおりです。

@Entity
public class UniverseTime extends Model {

    @Required
    public Date realDate;

    // Day of month
    @Required
    public Long diesse;

    // Month of year
    @Required
    public Long cycle;

    // year
    @Required
    public Long soleme;

    //next current cycle
    @Required
    @ManyToOne(fetch = FetchType.LAZY)
    public Cycle currentCycle;

    // Tell if this universe time is the global one
    @Required
    public Boolean global;
}

Web アプリケーションを実行すると、UniverseTime エンティティの保存中にロックがかかります。

このリンクhttp://wiki.postgresql.org/wiki/Lock_Monitoringから、何がロックされているかを把握しようとしました。

最初のコマンドで得られるものは次のとおりです。

db=# SELECT relation::regclass, * FROM pg_locks WHERE NOT granted;                                                                                                                                                                                                    relation |   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid  |   mode    | granted | fastpath 
----------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+-----------+---------+----------
          | transactionid |          |          |      |       |            |          2772 |         |       |          | 14/1479            | 6901 | ShareLock | f       | f
(1 row)

db=# SELECT relation::regclass, * FROM pg_locks;
      relation      |   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid  |       mode       | granted | fastpath 
--------------------+---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+------+------------------+---------+----------
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowShareLock     | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 hibernate_sequence | relation      |    16393 |    17294 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | AccessShareLock  | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowShareLock     | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 4/16298            | 6880 | RowExclusiveLock | t       | t
                    | virtualxid    |          |          |      |       | 4/16298    |               |         |       |          | 4/16298            | 6880 | ExclusiveLock    | t       | t
 cycle_pkey         | relation      |    16393 |    22585 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime_pkey  | relation      |    16393 |    22886 |      |       |            |               |         |       |          | 14/1479            | 6901 | RowExclusiveLock | t       | t
 cycle              | relation      |    16393 |    22582 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 14/1479            | 6901 | AccessShareLock  | t       | t
 universetime       | relation      |    16393 |    22883 |      |       |            |               |         |       |          | 14/1479            | 6901 | RowExclusiveLock | t       | t
                    | virtualxid    |          |          |      |       | 14/1479    |               |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | t
 pg_locks           | relation      |    16393 |    11090 |      |       |            |               |         |       |          | 2/9902             | 5206 | AccessShareLock  | t       | t
                    | virtualxid    |          |          |      |       | 2/9902     |               |         |       |          | 2/9902             | 5206 | ExclusiveLock    | t       | t
                    | transactionid |          |          |      |       |            |          2772 |         |       |          | 4/16298            | 6880 | ExclusiveLock    | t       | f
                    | transactionid |          |          |      |       |            |          2772 |         |       |          | 14/1479            | 6901 | ShareLock        | f       | f
                    | transactionid |          |          |      |       |            |          2773 |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | f
 universetime       | tuple         |    16393 |    22883 |    0 |    13 |            |               |         |       |          | 14/1479            | 6901 | ExclusiveLock    | t       | f
(25 rows)

そして、最後のコマンドで得られるものは次のとおりです。

db=# SELECT bl.pid                 AS blocked_pid,                                                                                                                                                                                                                            a.usename              AS blocked_user,                                                                                                                                                                                                                                        ka.query               AS blocking_statement,                                                                                                                                                                                                                                  now() - ka.query_start AS blocking_duration,                                                                                                                                                                                                                                   kl.pid                 AS blocking_pid,                                                                                                                                                                                                                                        ka.usename             AS blocking_user,                                                                                                                                                                                                                                       a.query                AS blocked_statement,                                                                                                                                                                                                                                   now() - a.query_start  AS blocked_duration                                                                                                                                                                                                                              FROM  pg_catalog.pg_locks         bl                                                                                                                                                                                                                                            JOIN pg_catalog.pg_stat_activity a  ON a.pid = bl.pid                                                                                                                                                                                                                          JOIN pg_catalog.pg_locks         kl ON kl.transactionid = bl.transactionid AND kl.pid != bl.pid                                                                                                                                                                                JOIN pg_catalog.pg_stat_activity ka ON ka.pid = kl.pid                                                                                                                                                                                                                        WHERE NOT bl.granted;
 blocked_pid | blocked_user |                                                                                                                                                        blocking_statement                                                                                                                                                        | blocking_duration | blocking_pid | blocking_user |                                               blocked_statement                                                | blocked_duration 
-------------+--------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------+--------------+---------------+----------------------------------------------------------------------------------------------------------------+------------------
        6901 | postgres     | select universeti0_.id as id30_0_, universeti0_.currentCycle_id as currentC7_30_0_, universeti0_.cycle as cycle30_0_, universeti0_.diesse as diesse30_0_, universeti0_.global as global30_0_, universeti0_.realDate as realDate30_0_, universeti0_.soleme as soleme30_0_ from UniverseTime universeti0_ where universeti0_.id=$1 | 00:07:36.576371   |         6880 | postgres      | update UniverseTime set currentCycle_id=$1, cycle=$2, diesse=$3, global=$4, realDate=$5, soleme=$6 where id=$7 | 00:07:31.574721
(1 row)

この出力は読みにくいため、blocked_statement を次に示します。

select universeti0_.id as id30_0_, universeti0_.currentCycle_id as currentC7_30_0_, universeti0_.cycle as cycle30_0_, universeti0_.diesse as diesse30_0_, universeti0_.global as global30_0_, universeti0_.realDate as realDate30_0_, universeti0_.soleme as soleme30_0_ from UniverseTime universeti0_ where universeti0_.id=$1

そして、blocking_statement は次のとおりです。

update UniverseTime set currentCycle_id=$1, cycle=$2, diesse=$3, global=$4, realDate=$5, soleme=$6 where id=$7

よくわかりませんが、UPDATEクエリがクエリをブロックしているようですが、クエリがロックされて実行されないSELECTのはなぜですか? UPDATEそれは単純UPDATEです。すばやく実行してロックを解除する必要がありますが、そうではありません。なぜですか?

4

0 に答える 0