E 'ngoe ea mekhoa ea ho fumana nalane ea senotlolo ho PostgreSQL

Tsoelo-pele ea sehlooho "Teko ea ho theha analogue ea ASH bakeng sa PostgreSQL ".

Sengoloa se tla shebisisa le ho bonts'a ka lipotso tse ikhethileng le mehlala hore na ke lintlha life tsa bohlokoa tse ka fumanoang ho sebelisoa nalane ea pono ea pg_locks.

Temoso
Ka lebaka la ntho e ncha ea sehlooho le ho se phethehe ha nako ea teko, sengoloa se ka ba le liphoso. Ho nyatsuoa le maikutlo ho amohelehile ebile ho lebeletsoe.

Kenya data

pg_locks histori ea boemeli

archive_nocking

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 
);

Ha e le hantle, tafole e tšoana le tafole archive_pg_stat_activitye hlalositsoe ka botlalo mona - pg_stat_statements + pg_stat_activity + loq_query = pg_ash? mme mona - Teko ea ho theha analogue ea ASH bakeng sa PostgreSQL.

Ho tlatsa kholomo queryid ts'ebetso e sebelisoa

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;

Tlhaloso: boleng ba kholomo ea queryid bo nchafatsoa ka har'a tafole ea histori_locking, 'me ha karohano e ncha e etsoa bakeng sa tafole ea archive_locking, boleng bo tla bolokoa mekhoeng ea nalane.

Phatlalatso

Lintlha tse akaretsang mabapi le lits'ebetso ka kakaretso.

HO LETELA LOKELA KA LOKOTELA

Kopa

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 

Mohlala:

| E LETETSE LINTHO KA LI LOCKTYPES +--------------------------------------------- -------+-------------------- | mofuta oa lock| mokhoa| nako +----------------------------------------------+-- ------------------ | transactionid| sharelock| 19:39:26 | tupo| AccessExclusiveLock| 00:03:35 +---------------------------------------------- -------+---------------------

HO NKA LINTLHA KA LIKETI

Kopa

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 

Mohlala:

| HO NKA LILINTLHA KA MEFUTA +---------------------------------------------- -------+-------------------- | mofuta oa lock| mokhoa| nako +----------------------------------------------+-- ------------------ | kamano| RowExclusiveLock| 51:11:10 | virtualxid| ExclusiveLock| 48:10:43 | transactionid| ExclusiveLock| 44:24:53 | kamano| AccessShareLock| 20:06:13 | tupo| AccessExclusiveLock| 17:58:47 | tupo| ExclusiveLock| 01:40:41 | kamano| ShareUpdateExclusiveLock| 00:26:41 | ntho | RowExclusiveLock| 00:00:01 | transactionid| sharelock| 00:00:01 | eketsa | ExclusiveLock| 00:00:01 +---------------------------------------------- -------+---------------------

Lintlha tse qaqileng bakeng sa lipotso tse khethehileng tsa queryid

E LETETSE LOCKTYPES KA QUERYID

Kopa

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

Mohlala:

| E LETETSE LOCKTYPES KA QUERYID +----------+--------------------------+------ ---------------+------------------------------+--- ------------------+--------------------+---------- --------- | pid| mofuta oa lock| mokhoa| qadile| potso| blocking_pids| nako +-------------------------------------------------- ---------+---------------------------------------- ------------------------------------------ --- | 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:10

HO NKA LILOKO KA MEFUTA KA QUERYID

Kopa

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

Mohlala:

| HO NKA LONA KA LI LOCKTYPES KA QUERYID +----------+--------------------------+------- ---------------------------------------------+---- - + ---------------------------------------- pid| mofuta oa lock| mokhoa| qadile| potso| blocking_pids| nako +-------------------------------------------------- ---------+---------------------------------------- ------------------------------------------ --- | 11288| kamano| 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| kamano| RowExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {}| 00:00:10 | 11092 | kamano| 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| tupo| AccessExclusiveLock| 2019-09-17 10:00:00.302936| 389015618226997618| {11092}| 00:03:34

Ho sebelisa histori e thibelang ha ho hlahlojoa liketsahalo tsa ts'ebetso.

  1. Potso e nang le queryid=389015618226997618 e entsoeng ke ts'ebetso e nang le pid=11288 esale e emetse senotlolo ho tloha ka 2019-09-17 10:00:00 metsotso e 3.
  2. Senotlolo se ne se ts'oaroe ke ts'ebetso e nang le pid=11092
  3. Ts'ebetso e nang le pid=11092 e botsang potso ka queryid=389015618226997618 ho tloha ka 2019-09-17 10:00:00 e tšoere senotlolo metsotso e 3.

Phello

Hona joale, ke tšepa hore ntho e thahasellisang le e molemo ka ho fetisisa e tla qala - ho bokella lipalo-palo le ho hlahloba linyeoe tsa histori ea litebello le ho thibela.

Nakong e tlang, ke rata ho lumela, u tla fumana sete ea mofuta o itseng oa lintlha (tse tšoanang le metalink ea Oracle).

Ka kakaretso, ke ka lebaka lena mokhoa o sebelisoang o bontšoa kapele kamoo ho ka khonehang bakeng sa ho tseba sechaba.

Haufinyane haholo ke tla leka ho beha morero ho github.

Source: www.habr.com

Eketsa ka tlhaloso