๊ธฐ์ฌ ๊ณ์ โ
์ด ๊ธฐ์ฌ์์๋ ํน์ ์ฟผ๋ฆฌ์ ์์ ๋ฅผ ์ฌ์ฉํ์ฌ 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, ์ฌ๊ธฐ์ ๋ ์์ธํ ์ค๋ช
๋์ด ์์ต๋๋ค.
์ด์ ์ฑ์ฐ๋ ค๋ฉด ์ฟผ๋ฆฌID ๊ธฐ๋ฅ์ด ์ฌ์ฉ๋ฉ๋๋ค
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 ํ ์ด๋ธ์ ๋ํด ์ ํํฐ์ ์ด ์์ฑ๋๋ฉด ํด๋น ๊ฐ์ด ๊ธฐ๋ก ๊ฐ์ ์ ์ฅ๋ฉ๋๋ค.
์ถ๋ ฅ
์ผ๋ฐ์ ์ธ ํ๋ก์ธ์ค์ ๋ํ ์ผ๋ฐ ์ ๋ณด์ ๋๋ค.
์ ๊ธ ์ ํ๋ณ ์ ๊ธ ๋๊ธฐ ์ค
์ฃผ์ฅ
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
์
| ์ ๊ธ ์ ํ๋ณ ์ ๊ธ ๋๊ธฐ ์ค +------------+------------ ---------+ ------ | ์ ๊ธ ์ ํ| ๋ชจ๋| ๊ธฐ๊ฐ +---------+--------------- - ------------ | ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 19:39:26 | ํํ| ์ ๊ทผ๋ ์ ์ ๊ธ| 00:03:35 +------+------------ - -----+------------
์๋ฌผ์ ์ข ๋ฅ์ ๋ฐ๋ฅธ ์๋ฌผ์ ์ฌ์ฉ
์ฃผ์ฅ
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
์
| ์ ๊ธ ์ ํ๋ณ ์ ๊ธ ํด์ +---------+------------------------- --------+------ | ์ ๊ธ ์ ํ| ๋ชจ๋| ๊ธฐ๊ฐ +---------+--------------- - ------------ | ๊ด๊ณ| ํ๋ ์ ์ ๊ธ| 51:11:10 | ๊ฐ์ID| ๋ ์ ์ ๊ธ| 48:10:43 | ๊ฑฐ๋ID| ๋ ์ ์ ๊ธ| 44:24:53 | ๊ด๊ณ| ์ก์ธ์ค๊ณต์ ์ ๊ธ| 20:06:13 | ํํ| ์ ๊ทผ๋ ์ ์ ๊ธ| 17:58:47 | ํํ| ๋ ์ ์ ๊ธ| 01:40:41 | ๊ด๊ณ| ๊ณต์ ์ ๋ฐ์ดํธ๋ ์ ์ ๊ธ| 00:26:41 | ๊ฐ์ฒด| ํ๋ ์ ์ ๊ธ| 00:00:01 | ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 00:00:01 | ์ฐ์ฅ| ๋ ์ ์ ๊ธ| 00:00:01 +------+------------ - -----+------------
ํน์ queryid ์์ฒญ์ ๋ํ ์์ธํ ์ ๋ณด
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
์
| QUERYID์ ์ ๊ธ ์ ํ๋ณ ์ ๊ธ ๋๊ธฐ ์ค +----------+------------+------ --------------- +---------------+--- ---- -+---------+------------ --------- | PID| ์ ๊ธ ์ ํ| ๋ชจ๋| ์์๋จ| ์ฟผ๋ฆฌID| Blocking_pids| ๊ธฐ๊ฐ +------------+------------+----------- ---------+---------------+--------- -- --------+---------+------ -- - | 11288| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11626| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:21.380921| 389015618226997618| {12380}| 00:00:29 | 11626| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:21.380921| 389015618226997618| {11092}| 00:03:25 | 11626| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:21.380921| 389015618226997618| {12213}| 00:01:55 | 11626| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:21.380921| 389015618226997618| {12751}| 00:00:01 | 11629| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:24.331935| 389015618226997618| {11092}| 00:03:22 | 11629| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:00:24.331935| 389015618226997618| {12007}| 00:00:01 | 12007| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:05:03.327933| 389015618226997618| {11629}| 00:00:13 | 12007| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:05:03.327933| 389015618226997618| {11092}| 00:01:10 | 12007| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:05:03.327933| 389015618226997618| {11288}| 00:00:05 | 12213| ๊ฑฐ๋ID| ๊ณต์ ์ ๊ธ| 2019-09-17 10:06:07.328019| 389015618226997618| {12007}| 00:00:10
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
์
| QUERYID๋ก ์ ๊ธ ์ ํ๋ณ๋ก ์ ๊ธ ์ค์ +----------+----------+------ --------------+ ---------------+------ ---+ -------+------------ -------- | PID| ์ ๊ธ ์ ํ| ๋ชจ๋| ์์๋จ| ์ฟผ๋ฆฌID| Blocking_pids| ๊ธฐ๊ฐ +------------+------------+----------- ---------+---------------+--------- -- --------+---------+------ -- - | 11288| ๊ด๊ณ| ํ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11092| ๊ฑฐ๋ID| ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| ๊ด๊ณ| ํ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:00:10 | 11092| ๊ด๊ณ| ํ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11092| ๊ฐ์ID| ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| ๊ฐ์ID| ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| ๊ฑฐ๋ID| ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| ํํ| ์ ๊ทผ๋ ์ ์ ๊ธ| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34
์ ๊ธ ๊ธฐ๋ก์ ์ฌ์ฉํ์ฌ ์ฑ๋ฅ ์ฌ๊ณ ๋ฅผ ๋ถ์ํฉ๋๋ค.
- pid=389015618226997618์ธ ํ๋ก์ธ์ค์ ์ํด ์คํ๋ queryid=11288์ธ ์์ฒญ์ด 2019-09-17 10:00:00๋ถํฐ 3๋ถ๊ฐ ์ฐจ๋จ์ ๊ธฐ๋ค๋ฆฌ๊ณ ์์์ต๋๋ค.
- ์ ๊ธ์ pid=11092์ธ ํ๋ก์ธ์ค์ ์ํด ์ ์ง๋์์ต๋๋ค.
- 11092-389015618226997618-2019 09:17:10๋ถํฐ queryid=00๋ก ์์ฒญ์ ์คํํ๋ pid=00์ ํ๋ก์ธ์ค๊ฐ 3๋ถ ๋์ ์ ๊ธ์ ์ ์งํ์ต๋๋ค.
ํฉ๊ณ
์ด์ ๊ฐ์ฅ ํฅ๋ฏธ๋กญ๊ณ ์ ์ฉํ ๋ถ๋ถ, ์ฆ ๋๊ธฐ ๋ฐ ์ฐจ๋จ ๋ด์ญ์ ๋ํ ํต๊ณ ์์ง ๋ฐ ์ฌ๋ก ๋ถ์์ด ์์๋๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
์์ผ๋ก๋ ๋ช ๊ฐ์ง ๋ฉ๋ชจ(์ค๋ผํด์ metalink์ ์ ์ฌ)๋ฅผ ์ป๊ฒ ๋ ๊ฒ์ด๋ผ๊ณ ๋ฏฟ๊ณ ์ถ์ต๋๋ค.
์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋ ๋ฐฉ๋ฒ๋ก ์ด ๊ฐ๋ฅํ ํ ๋นจ๋ฆฌ ๋ชจ๋ ์ฌ๋์๊ฒ ์ ๊ณต๋๋ ๊ฒ์ ๋ฐ๋ก ์ด๋ฌํ ์ด์ ๋๋ฌธ์ ๋๋ค.
๊ฐ๊น์ด ์์ผ ๋ด์ ํ๋ก์ ํธ๋ฅผ github์ ๊ฒ์ํ๋ ค๊ณ ํฉ๋๋ค.
์ถ์ฒ : habr.com