Fortsetzung des Artikels „«.
In diesem Artikel wird untersucht und anhand konkreter Anfragen und Beispiele gezeigt, welche nützlichen Informationen Sie mithilfe der Ansicht pg_locks erhalten können.
Warnung.
Wegen der Neuheit des Themas und des unvollendeten Testzeitraums kann der Artikel Fehler enthalten. Kritik und Anmerkungen sind jederzeit willkommen.
Eingabedaten
Die Ansicht 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
);Im Wesentlichen ähnelt die Tabelle der archive_pg_stat_activity, die hier ausführlicher beschrieben ist — und hier —
Um die Spalte queryid auszufüllen, wird die Funktion
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 'Aktuelle Zeit liegt unter 5 Minuten.';
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;Erklärung: Der Wert der Spalte queryid wird in der Tabelle history_locking aktualisiert und dann, wenn eine neue Sektion für die Tabelle archive_locking erstellt wird, wird der Wert in den historischen Werten gespeichert.
Ausgabe
Allgemeine Informationen zu den Prozessen insgesamt.
WARTEN AUF LOCKS NACH LOCKARTEN
Abfrage
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 Beispiel
| WARTEN AUF LOCKS NACH LOCKARTEN +--------------------+------------------------------+-------------------- | locktype| mode| duration +--------------------+------------------------------+-------------------- | transactionid| ShareLock| 19:39:26 | tuple| AccessExclusiveLock| 00:03:35 +--------------------+------------------------------+--------------------
ÜBERNEHMEN VON LOCKS NACH LOCKARTEN
Abfrage
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 Beispiel
| BELEGUNGEN VON LOCKS NACH LOCKTYPEN +--------------------+------------------------------+-------------------- | locktype| modus| dauer +--------------------+------------------------------+-------------------- | relation| ZeilenexklusivLock| 51:11:10 | virtualxid| ExklusivLock| 48:10:43 | transactionid| ExklusivLock| 44:24:53 | relation| ZugriffsfreigabeLock| 20:06:13 | tuple| ZugangsexklusivLock| 17:58:47 | tuple| ExklusivLock| 01:40:41 | relation| TeileUpdateExklusivLock| 00:26:41 | object| ZeilenexklusivLock| 00:00:01 | transactionid| TeileLock| 00:00:01 | extend| ExklusivLock| 00:00:01 +--------------------+------------------------------+--------------------
Detailinformationen zu bestimmten queryid-Anfragen
WARTEN AUF LOCKS NACH LOCKTYPEN NACH QUERYID
Abfrage
MIT
lt ALS
(
AUSWÄHLEN
pid ,
locktype ,
mode ,
timepoint ,
queryid ,
blocking_pids ,
MIN ( timepoint ) ÜBER (PARTITION BY pid , locktype ,mode ) als begonnen
VON
activity_hist.archive_locking
WO
timepoint zwischen pg_stat_history_begin+(current_hour_diff * interval '1 hour') UND
pg_stat_history_end+(current_hour_diff * interval '1 hour') UND
NICHT gewährt UND
queryid IST NICHT NULL
GRUPPIEREN NACH
pid ,
locktype ,
mode ,
timepoint ,
queryid ,
blocking_pids
)
AUSWÄHLEN
lt.pid ,
lt.locktype ,
lt.mode ,
lt.begonnen ,
lt.queryid ,
lt.blocking_pids ,
COUNT(*) * interval '1 second' als dauer
VON lt
GRUPPIEREN NACH
lt.pid ,
lt.locktype ,
lt.mode ,
lt.begonnen ,
lt.queryid ,
lt.blocking_pids
BESTELLEN NACH 4Beispiel
| WARTEN AUF SCHLOSSTYPEN NACH ABFRAGE-ID
+----------+-------------------------+--------------------+------------------------------+--------------------+--------------------+--------------------
| pid| schlosstyp| modus| gestartet| abfrageid| blockierende_pids| dauer
+----------+-------------------------+--------------------+------------------------------+--------------------+--------------------+--------------------
| 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:10ABFRAGUNG VON LOCKS NACH LOCKTYPEN UND QUERYID
Abfrage
MIT
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 4Beispiel
| BLOCKIERUNGEN NACH LOCKARTEN BEHANDELN FÜR 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:34Verwendung der Lock-Historie zur Analyse von Performance-Vorfällen.
- Die Anfrage mit queryid=389015618226997618, ausgeführt von dem Prozess mit pid=11288, wartete seit dem 17.09.2019 10:00:00 für drei Minuten auf eine Sperre.
- Die Sperre wurde von dem Prozess mit pid=11092 gehalten.
- Der Prozess mit pid=11092 hielt die Sperre während der Ausführung der Anfrage mit queryid=389015618226997618 ab dem 17.09.2019 10:00:00 für drei Minuten.
Zusammenfassung
Jetzt hoffe ich, dass das Interessanteste und Nützlichste beginnt - das Sammeln von Statistiken und die Analyse von Fällen zur Historie der Wartezeiten und Sperren.
In der Zukunft hoffe ich, eine Sammlung von sogenannten Anmerkungen (analog zu Metadaten in Oracle) zu erstellen.
Genau aus diesem Grund wird die verwendete Methode so schnell wie möglich zur allgemeinen Einsichtnahme bereitgestellt.
Ich werde in naher Zukunft versuchen, das Projekt auf GitHub zu veröffentlichen.
Quelle: habr.com
