Continuació de l'article "
L'article examinarà i mostrarà, mitjançant consultes i exemples específics, quina informació útil es pot obtenir mitjançant l'historial de la vista pg_locks.
Avís
A causa de la novetat del tema i del període de proves inacabat, l'article pot contenir errors. Les crítiques i els comentaris són molt benvinguts i esperats.
Dades d'entrada
Història de la representació de pg_locks
bloqueig_arxiu
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
);
Bàsicament, la taula és semblant a la taula archive_pg_stat_activity, descrit amb més detall aquí -
Per omplir una columna queryid s'utilitza la funció
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;
Explicació: el valor de la columna queryid s'actualitza a la taula history_locking i, després, quan es crea una partició nova per a la taula archive_locking, el valor s'emmagatzemarà als valors històrics.
Sortida
Informació general dels processos en general.
ESPERANT PANYS PER TIPUS DE PANY
Sol·licitud
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
Exemple
| ESPERANT PANYS PER TIPUS DE PANY +-----------+----------------------- --------+ ------------------- | tipus de bloqueig| mode| durada +--------------------+--------------------------- --+-------------------- | identificador de transacció| ShareLock| 19:39:26 | tupla| Accés ExclusiveLock| 00:03:35 +-------------------+------------------------ -------+----------
TRES DE PANYS PER TIPUS DE PANY
Sol·licitud
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
Exemple
| TENS DE PANYS PER TIPUS DE PANY +--------------------+------------------------ --------+------------------- | tipus de bloqueig| mode| durada +--------------------+--------------------------- --+-------------------- | relació| RowExclusiveLock| 51:11:10 | virtualxid| ExclusiveLock| 48:10:43 | identificador de transacció| ExclusiveLock| 44:24:53 | relació| AccessShareLock| 20:06:13 | tupla| Accés ExclusiveLock| 17:58:47 | tupla| ExclusiveLock| 01:40:41 | relació| ShareUpdateExclusiveLock| 00:26:41 | objecte| RowExclusiveLock| 00:00:01 | identificador de transacció| ShareLock| 00:00:01 | estendre| ExclusiveLock| 00:00:01 +-------------------+------------------------ -------+----------
Informació detallada sobre sol·licituds de queryid específiques
S'ESPERANT PANNYS PER LOCKTYPES PER QUERYID
Sol·licitud
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
Exemple
| ESPERANT BLOCS PER LOCKTYPES PER QUERYID +----------+----------+----- ---------------- +----------------------------+--- ------------------ -+--------------------+----------- ---------- | pid| tipus de bloqueig| mode| començat| queryid| bloqueig_pids| durada +----------+-------------------------+------------ ---------+------------------------------+---------- -----------+--------------------+------------------ --- | 11288| identificador de transacció| ShareLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11626| identificador de transacció| ShareLock| 2019/09/17 10:00:21.380921| 389015618226997618| {12380}| 00:00:29 | 11626| identificador de transacció| ShareLock| 2019/09/17 10:00:21.380921| 389015618226997618| {11092}| 00:03:25 | 11626| identificador de transacció| ShareLock| 2019/09/17 10:00:21.380921| 389015618226997618| {12213}| 00:01:55 | 11626| identificador de transacció| ShareLock| 2019/09/17 10:00:21.380921| 389015618226997618| {12751}| 00:00:01 | 11629| identificador de transacció| ShareLock| 2019/09/17 10:00:24.331935| 389015618226997618| {11092}| 00:03:22 | 11629| identificador de transacció| ShareLock| 2019/09/17 10:00:24.331935| 389015618226997618| {12007}| 00:00:01 | 12007| identificador de transacció| ShareLock| 2019/09/17 10:05:03.327933| 389015618226997618| {11629}| 00:00:13 | 12007| identificador de transacció| ShareLock| 2019/09/17 10:05:03.327933| 389015618226997618| {11092}| 00:01:10 | 12007| identificador de transacció| ShareLock| 2019/09/17 10:05:03.327933| 389015618226997618| {11288}| 00:00:05 | 12213| identificador de transacció| ShareLock| 2019/09/17 10:06:07.328019| 389015618226997618| {12007}| 00:00:10
APRENDRE PANYS PER TIPUS DE PANY PER QUERYID
Sol·licitud
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
Exemple
| APRENDRE PANYS PER TIPUS DE PANY PER QUERYID +----------+----------+------ ---------------+ ----------------------------+---- ----------------+ --------------------+------------ -------- | pid| tipus de bloqueig| mode| començat| queryid| bloqueig_pids| durada +----------+-------------------------+------------ ---------+------------------------------+---------- -----------+--------------------+------------------ --- | 11288| relació| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11092| identificador de transacció| ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:03:34 | 11288| relació| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:00:10 | 11092| relació| 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| identificador de transacció| ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34 | 11288| tupla| Accés ExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34
Ús de l'historial de bloqueig per analitzar incidents de rendiment.
- Una sol·licitud amb queryid=389015618226997618 executada per un procés amb pid=11288 estava esperant el bloqueig a partir del 2019/09/17 a les 10:00:00 durant 3 minuts.
- El bloqueig es va mantenir mitjançant un procés amb pid=11092
- Un procés amb pid=11092 executant una sol·licitud amb queryid=389015618226997618 a partir del 2019/09/17 a les 10:00:00 va mantenir el bloqueig durant 3 minuts.
Total
Ara, espero, començarà la part més interessant i útil: recollir estadístiques i analitzar casos sobre l'historial d'espera i bloqueig.
En el futur, vull creure, obtindrem un conjunt d'algunes notes (similars al metalink d'Oracle).
En general, és per aquest motiu que la metodologia utilitzada es posa a l'abast de tothom el més aviat possible.
Intentaré publicar el projecte a github en un futur proper.
Font: www.habr.com