వ్యాసం కొనసాగింపు "
నిర్దిష్ట ప్రశ్నలు మరియు ఉదాహరణలను ఉపయోగించి, pg_locks వీక్షణ చరిత్రను ఉపయోగించి ఏ ఉపయోగకరమైన సమాచారాన్ని పొందవచ్చో వ్యాసం పరిశీలిస్తుంది మరియు చూపుతుంది.
హెచ్చరిక.
అంశం యొక్క కొత్తదనం మరియు అసంపూర్తిగా ఉన్న పరీక్షా కాలం కారణంగా, వ్యాసంలో లోపాలు ఉండవచ్చు. విమర్శలు మరియు వ్యాఖ్యలు గట్టిగా స్వాగతించబడతాయి మరియు ఆశించబడతాయి.
ఇన్పుట్ డేటా
pg_locks ప్రాతినిధ్య చరిత్ర
ఆర్కైవ్_లాకింగ్
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, ఇక్కడ మరింత వివరంగా వివరించబడింది -
నిలువు వరుసను పూరించడానికి ప్రశ్నించాడు ఫంక్షన్ ఉపయోగించబడుతుంది
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 నిలువు వరుస విలువ చరిత్ర_లాకింగ్ పట్టికలో నవీకరించబడుతుంది, ఆపై 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
ఉదాహరణకు
| లాక్టైప్ల వారీగా లాక్ల కోసం వేచి ఉంది +------------+------------------------------------- ------------------- | లాక్టైప్| మోడ్| వ్యవధి +--------------------------------------------------- --+--------------------- | లావాదేవీలు| ShareLock| 19:39:26 | టుపుల్| AccessExclusiveLock| 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
ఉదాహరణకు
| లాక్టైప్ల వారీగా లాక్లను తీసుకోవడం +---------------------+ ---------+------------------- | లాక్టైప్| మోడ్| వ్యవధి +--------------------------------------------------- --+--------------------- | సంబంధం| RowExclusiveLock| 51:11:10 | virtualxid| ప్రత్యేక లాక్| 48:10:43 | లావాదేవీలు| ప్రత్యేక లాక్| 44:24:53 | సంబంధం| AccessShareLock| 20:06:13 | టుపుల్| AccessExclusiveLock| 17:58:47 | టుపుల్| ప్రత్యేక లాక్| 01:40:41 | సంబంధం| ShareUpdateExclusiveLock| 00:26:41 | వస్తువు| RowExclusiveLock| 00:00:01 | లావాదేవీలు| ShareLock| 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 ద్వారా లాక్టైప్ల ద్వారా లాక్ల కోసం వేచి ఉంది +----------+---------------------------------- +------------------------------------------------------------ -+---------------------+---------------------- | పిడ్| లాక్టైప్| మోడ్| ప్రారంభించారు| queryid| blocking_pids| వ్యవధి +------------+--------------------------------------------- -------+------------------------------------------ -----------+--------------------------------------------- --- | 11288| లావాదేవీలు| ShareLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11626| లావాదేవీలు| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {12380}| 00:00:29 | 11626| లావాదేవీలు| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {11092}| 00:03:25 | 11626| లావాదేవీలు| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {12213}| 00:01:55 | 11626| లావాదేవీలు| ShareLock| 2019-09-17 10:00:21.380921| 389015618226997618| {12751}| 00:00:01 | 11629| లావాదేవీలు| ShareLock| 2019-09-17 10:00:24.331935| 389015618226997618| {11092}| 00:03:22 | 11629| లావాదేవీలు| ShareLock| 2019-09-17 10:00:24.331935| 389015618226997618| {12007}| 00:00:01 | 12007| లావాదేవీలు| ShareLock| 2019-09-17 10:05:03.327933| 389015618226997618| {11629}| 00:00:13 | 12007| లావాదేవీలు| ShareLock| 2019-09-17 10:05:03.327933| 389015618226997618| {11092}| 00:01:10 | 12007| లావాదేవీలు| ShareLock| 2019-09-17 10:05:03.327933| 389015618226997618| {11288}| 00:00:05 | 12213| లావాదేవీలు| ShareLock| 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 ద్వారా లాక్టైప్ల ద్వారా తాళాలు తీసుకోవడం +----------+------------------------------------ -------------------------------------------------------------+ ----------------------+--------------------- | పిడ్| లాక్టైప్| మోడ్| ప్రారంభించారు| ప్రశ్నించింది| blocking_pids| వ్యవధి +------------+--------------------------------------------- -------+------------------------------------------------- -------+------------------------------------------------- - | 11288| సంబంధం| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11092| లావాదేవీలు| ప్రత్యేక లాక్| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| సంబంధం| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:00:10 | 11092| సంబంధం| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11092| virtualxid| ప్రత్యేక లాక్| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| virtualxid| ప్రత్యేక లాక్| 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 | 11288| టుపుల్| AccessExclusiveLock| 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తో ఒక ప్రక్రియ ద్వారా లాక్ని ఉంచారు
- pid=11092తో queryid=389015618226997618తో అభ్యర్థనను అమలు చేయడం 2019-09-17 10:00:00 నుండి 3 నిమిషాల పాటు లాక్ని ఉంచింది.
ఫలితం
ఇప్పుడు, అత్యంత ఆసక్తికరమైన మరియు ఉపయోగకరమైన భాగం ప్రారంభమవుతుందని నేను ఆశిస్తున్నాను - గణాంకాలను సేకరించడం మరియు నిరీక్షణలు మరియు నిరోధాల చరిత్రపై కేసులను విశ్లేషించడం.
భవిష్యత్తులో, మేము కొన్ని గమనికలను (ఒరాకిల్ యొక్క మెటాలింక్ మాదిరిగానే) పొందుతామని నేను నమ్మాలనుకుంటున్నాను.
సాధారణంగా, ఈ కారణంగానే ఉపయోగించిన పద్దతి వీలైనంత త్వరగా అందరికీ అందుబాటులోకి వస్తుంది.
నేను సమీప భవిష్యత్తులో ప్రాజెక్ట్ను గిథబ్లో పోస్ట్ చేయడానికి ప్రయత్నిస్తాను.
మూలం: www.habr.com