Te haere tonu o te tuhinga "
Ka whakaarohia e te tuhinga ka whakaatu i runga i nga patai me nga tauira motuhake he aha nga korero whai hua ka taea te tiki ma te whakamahi i te hitori tirohanga pg_locks.
Whakatupato.
Na te mea hou o te kaupapa me te kore e oti o te waa whakamatautau, kei roto pea i te tuhinga nga hapa. Ko nga whakahee me nga korero e manakohia ana, e tumanakohia ana.
Raraunga whakauru
pg_locks hitori tohu
pūranga_maukati
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
);
Ko te tikanga, he rite te tepu ki te tepu archive_pg_stat_activitywhakaahuatia ake i konei -
Hei whakakī i tētahi tīwae id patai ka whakamahia te mahi
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;
Whakamārama: ka whakahōuhia te uara o te tīwae queryid ki te ripanga_hitori_maukati, katahi ka hangaia he wehewehenga hou mo te ripanga_maukati, ka penapena te uara ki nga uara o mua.
Whakaputa
Nga korero whanui mo nga tukanga i te nuinga o te waa.
TE TAARI MO NGA RAKA MA NGA MOMO KAITI
Tono
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
Hei tauira:
| TAITAI MO NGA RAKA NA NGA MOMO KAITI +----------------------------------- ---------------------------------- | momo maukati| aratau| roa +--------------------+--------------------- ---+-- ------------------- | tauwhitinga| maukati tiritiri| 19:39:26 | tupe| AccessExclusiveLock| 00:03:35 +--------------------+---------------------- --------+---------------------
TANGI O NGA KARAKA NA NGA MONO KAUPAPA
Tono
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
Hei tauira:
| TANGI O NGA raka na nga momo kati +---------------------------------- ---------------------------------- | momo maukati| aratau| roa +--------------------+--------------------- ---+-- ------------------- | hononga| RowExclusiveLock| 51:11:10 | virtualxid| Maukati Motuhake| 48:10:43 | tauwhitinga| Maukati Motuhake| 44:24:53 | hononga| AccessShareLock| 20:06:13 | tupere| AccessExclusiveLock| 17:58:47 | tupere| Maukati Motuhake| 01:40:41 | hononga| ShareUpdateExclusiveLock| 00:26:41 | ahanoa| RowExclusiveLock| 00:00:01 | tauwhitinga| raka tiriti| 00:00:01 | whakaroa| Maukati Motuhake| 00:00:01 +---------------------+---------------------- --------+---------------------
Nga korero taipitopito mo nga uiuinga id patai
TAITAI MO NGA RAKA NA NGA MOMO KAITI NA QUERYID
Tono
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
Hei tauira:
| TAITAI KI NGA RAKA MA NGA LOCKTYPES NA QUERYID +----------+------------------------+------ --------------+--------------------------------+--- --------------------+--------------------+---------- - --------- | pid| momo maukati| aratau| timata| id patai| aukati_pids| roa +----------+------------------------+----------- -----------------------+--------------------------------+-------- -------------------------+---------------------+---------------- --- | 11288| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11626| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:21.380921| 389015618226997618| {12380}| 00:00:29 | 11626| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:21.380921| 389015618226997618| {11092}| 00:03:25 | 11626| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:21.380921| 389015618226997618| {12213}| 00:01:55 | 11626| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:21.380921| 389015618226997618| {12751}| 00:00:01 | 11629| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:24.331935| 389015618226997618| {11092}| 00:03:22 | 11629| tauwhitinga| maukati tiritiri| 2019-09-17 10:00:24.331935| 389015618226997618| {12007}| 00:00:01 | 12007| tauwhitinga| maukati tiritiri| 2019-09-17 10:05:03.327933| 389015618226997618| {11629}| 00:00:13 | 12007| tauwhitinga| maukati tiritiri| 2019-09-17 10:05:03.327933| 389015618226997618| {11092}| 00:01:10 | 12007| tauwhitinga| maukati tiritiri| 2019-09-17 10:05:03.327933| 389015618226997618| {11288}| 00:00:05 | 12213| tauwhitinga| maukati tiritiri| 2019-09-17 10:06:07.328019| 389015618226997618| {12007}| 00:00:10
TANGI raka ma nga momo kati ma te QUERYID
Tono
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
Hei tauira:
| TANGI raka ma nga momo kati ma te QUERYID +----------+------------------------+------ --------------+--------------------------------+---- -----------------------+------------ -------- | pid| momo maukati| aratau| timata| uiui| aukati_pids| roa +----------+------------------------+----------- -----------------------+--------------------------------+-------- ---+--------------------+----------------- --- | 11288| hononga| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11092| tauwhitinga| Maukati Motuhake| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| hononga| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:00:10 | 11092| hononga| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11092| virtualxid| Maukati Motuhake| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| virtualxid| Maukati Motuhake| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| tauwhitinga| Maukati Motuhake| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| tupe| AccessExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34
Te whakamahi i te hitori aukati i te wa e tātari ana i nga maiki mahi.
- Ko te patai me te queryid=389015618226997618 i mahia e te tukanga me te pid=11288 e tatari ana mo te raka mai i te 2019-09-17 10:00:00 mo te 3 meneti.
- I mauhia te raka e te tukanga me te pid=11092
- He tukanga me te pid=11092 e mahi ana i te patai me te queryid=389015618226997618 mai i te 2019-09-17 10:00:00 i pupuri i te raka mo te 3 meneti.
Ko te hua
Inaianei, ko taku tumanako ka timata te mea tino pai me te whai hua - ko te kohi tatauranga me te tarai i nga keehi mo te hitori o nga tumanako me te aukati.
I te wa kei te heke mai, e hiahia ana ahau ki te whakapono, ka whiwhi koe i etahi momo tuhipoka (he rite ki te metalink a Oracle).
I te nuinga o te waa, na konei ka whakaatu tere te tikanga e whakamahia ana mo te maaramatanga a te iwi.
I nga wa e tata mai nei ka ngana ahau ki te whakauru i te kaupapa ki runga i te github.
Source: will.com