ΠΡΠΎΠ΄ΠΎΠ»ΠΆΠ΅Π½ΠΈΠ΅ ΡΡΠ°ΡΡΠΈ «
Π ΡΡΠ°ΡΡΠ΅ Π±ΡΠ΄Π΅Ρ ΡΠ°ΡΡΠΌΠΎΡΡΠ΅Π½ΠΎ ΠΈ ΠΏΠΎΠΊΠ°Π·Π°Π½ΠΎ Π½Π° ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΡΡ Π·Π°ΠΏΡΠΎΡΠ°Ρ ΠΈ ΠΏΡΠΈΠΌΠ΅ΡΠ°Ρ β ΠΊΠ°ΠΊΡΡ ΠΆΠ΅ ΠΏΠΎΠ»Π΅Π·Π½ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡΡΠΈΡΡ Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΈΡΡΠΎΡΠΈΠΈ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΡ pg_locks.
ΠΡΠ΅Π΄ΡΠΏΡΠ΅ΠΆΠ΄Π΅Π½ΠΈΠ΅.
Π ΡΠ²ΡΠ·ΠΈ Ρ Π½ΠΎΠ²ΠΈΠ·Π½ΠΎΠΉ ΡΠ΅ΠΌΡ ΠΈ Π½Π΅Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΠ΅ΠΌ ΠΏΠ΅ΡΠΈΠΎΠ΄Π° ΡΠ΅ΡΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ, ΡΡΠ°ΡΡΡ ΠΌΠΎΠΆΠ΅Ρ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡ ΠΎΡΠΈΠ±ΠΊΠΈ. ΠΡΠΈΡΠΈΠΊΠ° ΠΈ Π·Π°ΠΌΠ΅ΡΠ°Π½ΠΈΡ Π²ΡΡΡΠ΅ΡΠΊΠΈ ΠΏΡΠΈΠ²Π΅ΡΡΡΠ²ΡΡΡΡΡ ΠΈ ΠΎΠΆΠΈΠ΄Π°ΡΡΡΡ.
ΠΡ ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
ΠΡΡΠΎΡΠΈΡ ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΡ pg_locks
archive_locking
CREATE TABLE archive_locking
( timepoint timestamp without time zone ,
locktype text ,
relation oid ,
mode text ,
tid xid ,
vtid text ,
pid integer ,
blocking_pids integer[] ,
granted boolean ,
queryid bigint
);
ΠΠΎ ΡΡΡΠΈ, ΡΠ°Π±Π»ΠΈΡΠ° Π°Π½Π°Π»ΠΎΠ³ΠΈΡΠ½Π° ΡΠ°Π±Π»ΠΈΡΠ΅ archive_pg_stat_activity, Π±ΠΎΠ»Π΅Π΅ ΠΏΠΎΠ΄ΡΠΎΠ±Π½ΠΎ ΠΎΠΏΠΈΡΠ°Π½Π½ΠΎΠΉ Π·Π΄Π΅ΡΡ β
ΠΠ»Ρ Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ ΡΡΠΎΠ»Π±ΡΠ° queryid ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ
update_history_locking_by_queryid
--update_history_locking_by_queryid.sql
CREATE OR REPLACE FUNCTION update_history_locking_by_queryid() RETURNS boolean AS $$
DECLARE
result boolean ;
current_minute double precision ;
start_minute integer ;
finish_minute integer ;
start_period timestamp without time zone ;
finish_period timestamp without time zone ;
lock_rec record ;
endpoint_rec record ;
current_hour_diff double precision ;
BEGIN
RAISE NOTICE '***update_history_locking_by_queryid';
result = TRUE ;
current_minute = extract ( minute from now() );
SELECT * FROM endpoint WHERE is_need_monitoring
INTO endpoint_rec ;
current_hour_diff = endpoint_rec.hour_diff ;
IF current_minute < 5
THEN
RAISE NOTICE 'Current time is less than 5 minute.';
start_period = date_trunc('hour',now()) + (current_hour_diff * interval '1 hour');
finish_period = start_period - interval '5 minute' ;
ELSE
finish_minute = extract ( minute from now() ) / 5 ;
start_minute = finish_minute - 1 ;
start_period = date_trunc('hour',now()) + interval '1 minute'*start_minute*5+(current_hour_diff * interval '1 hour');
finish_period = date_trunc('hour',now()) + interval '1 minute'*finish_minute*5+(current_hour_diff * interval '1 hour') ;
END IF ;
RAISE NOTICE 'start_period = %', start_period;
RAISE NOTICE 'finish_period = %', finish_period;
FOR lock_rec IN
WITH act_queryid AS
(
SELECT
pid ,
timepoint ,
query_start AS started ,
MAX(timepoint) OVER (PARTITION BY pid , query_start ) AS finished ,
queryid
FROM
activity_hist.history_pg_stat_activity
WHERE
timepoint BETWEEN start_period and
finish_period
GROUP BY
pid ,
timepoint ,
query_start ,
queryid
),
lock_pids AS
(
SELECT
hl.pid ,
hl.locktype ,
hl.mode ,
hl.timepoint ,
MIN ( timepoint ) OVER (PARTITION BY pid , locktype ,mode ) as started
FROM
activity_hist.history_locking hl
WHERE
hl.timepoint between start_period and
finish_period
GROUP BY
hl.pid ,
hl.locktype ,
hl.mode ,
hl.timepoint
)
SELECT
lp.pid ,
lp.locktype ,
lp.mode ,
lp.timepoint ,
aq.queryid
FROM lock_pids lp LEFT OUTER JOIN act_queryid aq ON ( lp.pid = aq.pid AND lp.started BETWEEN aq.started AND aq.finished )
WHERE aq.queryid IS NOT NULL
GROUP BY
lp.pid ,
lp.locktype ,
lp.mode ,
lp.timepoint ,
aq.queryid
LOOP
UPDATE activity_hist.history_locking SET queryid = lock_rec.queryid
WHERE pid = lock_rec.pid AND locktype = lock_rec.locktype AND mode = lock_rec.mode AND timepoint = lock_rec.timepoint ;
END LOOP;
RETURN result ;
END
$$ LANGUAGE plpgsql;
ΠΠΎΡΡΠ½Π΅Π½ΠΈΠ΅: Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΡΠΎΠ»Π±ΡΠ° queryid ΠΎΠ±Π½ΠΎΠ²Π»ΡΠ΅ΡΡΡ Π² ΡΠ°Π±Π»ΠΈΡΠ΅ history_locking, ΠΈ Π·Π°ΡΠ΅ΠΌ ΠΏΡΠΈ ΡΠΎΠ·Π΄Π°Π½ΠΈΠΈ Π½ΠΎΠ²ΠΎΠΉ ΡΠ΅ΠΊΡΠΈΠΈ Π΄Π»Ρ ΡΠ°Π±Π»ΠΈΡΡ archive_locking, Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π±ΡΠ΄Π΅Ρ ΡΠΎΡ ΡΠ°Π½Π΅Π½ΠΎ Π² ΠΈΡΡΠΎΡΠΈΡΠ΅ΡΠΊΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡΡ .
ΠΡΡ ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
ΠΠ±ΡΠ°Ρ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ, ΠΏΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ°ΠΌ Π² ΡΠ΅Π»ΠΎΠΌ.
WAITING FOR LOCKS BY LOCKTYPES
ΠΠ°ΠΏΡΠΎΡ
WITH
t AS
(
SELECT
locktype ,
mode ,
count(*) as total
FROM
activity_hist.archive_locking
WHERE
timepoint between pg_stat_history_begin+(current_hour_diff * interval '1 hour') AND pg_stat_history_end+(current_hour_diff * interval '1 hour') AND
NOT granted
GROUP BY
locktype ,
mode
)
SELECT
locktype ,
mode ,
total * interval '1 second' as duration
FROM t
ORDER BY 3 DESC
ΠΡΠΈΠΌΠ΅Ρ
| WAITING FOR LOCKS BY LOCKTYPES +--------------------+------------------------------+-------------------- | locktype| mode| duration +--------------------+------------------------------+-------------------- | transactionid| ShareLock| 19:39:26 | tuple| AccessExclusiveLock| 00:03:35 +--------------------+------------------------------+--------------------
TAKINGS OF LOCKS BY LOCKTYPES
ΠΠ°ΠΏΡΠΎΡ
WITH
t AS
(
SELECT
locktype ,
mode ,
count(*) as total
FROM
activity_hist.archive_locking
WHERE
timepoint between pg_stat_history_begin+(current_hour_diff * interval '1 hour') AND pg_stat_history_end+(current_hour_diff * interval '1 hour') AND
granted
GROUP BY
locktype ,
mode
)
SELECT
locktype ,
mode ,
total * interval '1 second' as duration
FROM t
ORDER BY 3 DESC
ΠΡΠΈΠΌΠ΅Ρ
| TAKINGS OF LOCKS BY LOCKTYPES +--------------------+------------------------------+-------------------- | locktype| mode| duration +--------------------+------------------------------+-------------------- | relation| RowExclusiveLock| 51:11:10 | virtualxid| ExclusiveLock| 48:10:43 | transactionid| ExclusiveLock| 44:24:53 | relation| AccessShareLock| 20:06:13 | tuple| AccessExclusiveLock| 17:58:47 | tuple| ExclusiveLock| 01:40:41 | relation| ShareUpdateExclusiveLock| 00:26:41 | object| RowExclusiveLock| 00:00:01 | transactionid| ShareLock| 00:00:01 | extend| ExclusiveLock| 00:00:01 +--------------------+------------------------------+--------------------
ΠΠ΅ΡΠ°Π»ΡΠ½Π°Ρ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ, ΠΏΠΎ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΡΠΌ queryid Π·Π°ΠΏΡΠΎΡΠΎΠ²
WAITING FOR LOCKS BY LOCKTYPES BY QUERYID
ΠΠ°ΠΏΡΠΎΡ
WITH
lt AS
(
SELECT
pid ,
locktype ,
mode ,
timepoint ,
queryid ,
blocking_pids ,
MIN ( timepoint ) OVER (PARTITION BY pid , locktype ,mode ) as started
FROM
activity_hist.archive_locking
WHERE
timepoint between pg_stat_history_begin+(current_hour_diff * interval '1 hour') AND
pg_stat_history_end+(current_hour_diff * interval '1 hour') AND
NOT granted AND
queryid IS NOT NULL
GROUP BY
pid ,
locktype ,
mode ,
timepoint ,
queryid ,
blocking_pids
)
SELECT
lt.pid ,
lt.locktype ,
lt.mode ,
lt.started ,
lt.queryid ,
lt.blocking_pids ,
COUNT(*) * interval '1 second' as duration
FROM lt
GROUP BY
lt.pid ,
lt.locktype ,
lt.mode ,
lt.started ,
lt.queryid ,
lt.blocking_pids
ORDER BY 4
ΠΡΠΈΠΌΠ΅Ρ
| WAITING FOR LOCKS BY LOCKTYPES BY QUERYID +----------+-------------------------+--------------------+------------------------------+--------------------+--------------------+-------------------- | pid| locktype| mode| started| queryid| blocking_pids| duration +----------+-------------------------+--------------------+------------------------------+--------------------+--------------------+-------------------- | 11288| transactionid| ShareLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11626| transactionid| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {12380}| 00:00:29 | 11626| transactionid| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {11092}| 00:03:25 | 11626| transactionid| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {12213}| 00:01:55 | 11626| transactionid| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {12751}| 00:00:01 | 11629| transactionid| ShareLock| 2019-09-17 10:00:24.331935| 389015618226997618| {11092}| 00:03:22 | 11629| transactionid| ShareLock| 2019-09-17 10:00:24.331935| 389015618226997618| {12007}| 00:00:01 | 12007| transactionid| ShareLock| 2019-09-17 10:05:03.327933| 389015618226997618| {11629}| 00:00:13 | 12007| transactionid| ShareLock| 2019-09-17 10:05:03.327933| 389015618226997618| {11092}| 00:01:10 | 12007| transactionid| ShareLock| 2019-09-17 10:05:03.327933| 389015618226997618| {11288}| 00:00:05 | 12213| transactionid| ShareLock| 2019-09-17 10:06:07.328019| 389015618226997618| {12007}| 00:00:10
TAKING LOCKS BY LOCKTYPES BY QUERYID
ΠΠ°ΠΏΡΠΎΡ
WITH
lt AS
(
SELECT
pid ,
locktype ,
mode ,
timepoint ,
queryid ,
blocking_pids ,
MIN ( timepoint ) OVER (PARTITION BY pid , locktype ,mode ) as started
FROM
activity_hist.archive_locking
WHERE
timepoint between pg_stat_history_begin+(current_hour_diff * interval '1 hour') AND
pg_stat_history_end+(current_hour_diff * interval '1 hour') AND
granted AND
queryid IS NOT NULL
GROUP BY
pid ,
locktype ,
mode ,
timepoint ,
queryid ,
blocking_pids
)
SELECT
lt.pid ,
lt.locktype ,
lt.mode ,
lt.started ,
lt.queryid ,
lt.blocking_pids ,
COUNT(*) * interval '1 second' as duration
FROM lt
GROUP BY
lt.pid ,
lt.locktype ,
lt.mode ,
lt.started ,
lt.queryid ,
lt.blocking_pids
ORDER BY 4
ΠΡΠΈΠΌΠ΅Ρ
| TAKING LOCKS BY LOCKTYPES BY QUERYID +----------+-------------------------+--------------------+------------------------------+--------------------+--------------------+-------------------- | pid| locktype| mode| started| queryid| blocking_pids| duration +----------+-------------------------+--------------------+------------------------------+--------------------+--------------------+-------------------- | 11288| relation| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11092| transactionid| ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| relation| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:00:10 | 11092| relation| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11092| virtualxid| ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| virtualxid| ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| transactionid| ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| tuple| AccessExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34
ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΈΡΡΠΎΡΠΈΠΈ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΎΠΊ ΠΏΡΠΈ Π°Π½Π°Π»ΠΈΠ·Π΅ ΠΈΠ½ΡΠΈΠ΄Π΅Π½ΡΠΎΠ² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ.
- ΠΠ°ΠΏΡΠΎΡ Ρ queryid=389015618226997618 Π²ΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌΡΠΉ ΠΏΡΠΎΡΠ΅ΡΡΠΎΠΌ Ρ pid=11288 ΠΎΠΆΠΈΠ΄Π°Π» Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΠΈ Π½Π°ΡΠΈΠ½Π°Ρ Ρ 2019-09-17 10:00:00 Π² ΡΠ΅ΡΠ΅Π½ΠΈΠΈ 3-Ρ ΠΌΠΈΠ½ΡΡ.
- ΠΠ»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΡ ΡΠ΄Π΅ΡΠΆΠΈΠ²Π°Π» ΠΏΡΠΎΡΠ΅ΡΡ Ρ pid=11092
- ΠΡΠΎΡΠ΅ΡΡ Ρ pid=11092 Π²ΡΠΏΠΎΠ»Π½ΡΡ Π·Π°ΠΏΡΠΎΡ Ρ queryid=389015618226997618 Π½Π°ΡΠΈΠ½Π°Ρ Ρ 2019-09-17 10:00:00 ΡΠ΄Π΅ΡΠΆΠΈΠ²Π°Π» Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΊΡ Π² ΡΠ΅ΡΠ΅Π½ΠΈΠΈ 3-Ρ ΠΌΠΈΠ½ΡΡ.
ΠΡΠΎΠ³
Π’Π΅ΠΏΠ΅ΡΡ, Π½Π°Π΄Π΅ΡΡΡ, Π½Π°ΡΠΈΠ½Π΅ΡΡΡ ΡΠ°ΠΌΠΎΠ΅ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΠΎΠ΅ ΠΈ ΠΏΠΎΠ»Π΅Π·Π½ΠΎΠ΅- ΡΠ±ΠΎΡ ΡΡΠ°ΡΠΈΡΡΠΈΠΊΠΈ ΠΈ Π°Π½Π°Π»ΠΈΠ· ΠΊΠ΅ΠΉΡΠΎΠ² ΠΏΠΎ ΠΈΡΡΠΎΡΠΈΠΈ ΠΎΠΆΠΈΠ΄Π°Π½ΠΈΠΉ ΠΈ Π±Π»ΠΎΠΊΠΈΡΠΎΠ²ΠΎΠΊ.
Π ΠΏΠ΅ΡΡΠΏΠ΅ΠΊΡΠΈΠ²Π΅, Ρ ΠΎΡΠ΅ΡΡΡ Π²Π΅ΡΠΈΡΡ, ΠΏΠΎΠ»ΡΡΠΈΡΡΡ Π½Π°Π±ΠΎΡ Π½Π΅ΠΊΠΎΡΠΎΡΡΡ ΠΊΠ°ΠΊ Π±Ρ note(ΠΏΠΎ Π°Π½Π°Π»ΠΎΠ³Ρ Ρ ΠΌΠ΅ΡΠ°Π»ΠΈΠ½ΠΊΠΎΠΌ ΠΡΠ°ΠΊΠ»Π°).
Π ΠΎΠ±ΡΠ΅ΠΌ-ΡΠΎ, ΠΈΠΌΠ΅Π½Π½ΠΎ ΠΏΠΎ ΡΡΠΎΠΉ ΠΏΡΠΈΡΠΈΠ½Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌΠ°Ρ ΠΌΠ΅ΡΠΎΠ΄ΠΈΠΊΠ°, ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎ Π±ΡΡΡΡΠΎ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡΡ Π΄Π»Ρ Π²ΡΠ΅ΠΎΠ±ΡΠ΅Π³ΠΎ ΠΎΠ·Π½Π°ΠΊΠΎΠΌΠ»Π΅Π½ΠΈΡ.
Π ΡΠ°ΠΌΠΎΠ΅ Π±Π»ΠΈΠΆΠ°ΠΉΡΠ΅Π΅ Π²ΡΠ΅ΠΌΡ ΠΏΠΎΡΡΠ°ΡΠ°ΡΡΡ Π²ΡΠ»ΠΎΠΆΠΈΡΡ ΠΏΡΠΎΠ΅ΠΊΡ Π½Π° github.
ΠΡΡΠΎΡΠ½ΠΈΠΊ: habr.com