पीजीसेंटिनल एक्सटेंशन :
«सभी संचित जानकारी केवल रैम में संग्रहीत की जाती है, और खपत की गई मेमोरी की मात्रा अंतिम संग्रहीत रिकॉर्ड की संख्या से नियंत्रित होती है।
क्वेरीआईडी फ़ील्ड जोड़ा गया है - pg_stat_statements एक्सटेंशन से वही क्वेरीआईडी (पूर्व-इंस्टॉलेशन आवश्यक)।«
बेशक, इससे बहुत मदद मिलेगी, लेकिन सबसे परेशानी वाली बात पहला बिंदु है।सारी संचित जानकारी RAM में ही संग्रहित होती है ", अर्थात। लक्ष्य आधार पर प्रभाव पड़ता है. इसके अलावा, कोई लॉक इतिहास और तालिका आँकड़े नहीं हैं। वे। समाधान आम तौर पर अधूरा बोल रहा है: "स्थापना के लिए अभी तक कोई तैयार पैकेज नहीं है। स्रोतों को डाउनलोड करने और लाइब्रेरी को स्वयं इकट्ठा करने का सुझाव दिया गया है। आपको सबसे पहले अपने सर्वर के लिए "डेवेल" पैकेज इंस्टॉल करना होगा और PATH वेरिएबल में pg_config का पथ सेट करना होगा।".
सामान्य तौर पर, बहुत अधिक उपद्रव होता है, और गंभीर उत्पादन डेटाबेस के मामले में, सर्वर के साथ कुछ भी करना संभव नहीं हो सकता है। हमें फिर से अपना कुछ लेकर आने की जरूरत है।
चेतावनी।
बड़ी मात्रा के कारण और अपूर्ण परीक्षण अवधि के कारण, लेख मुख्य रूप से सूचनात्मक प्रकृति का है, बल्कि थीसिस और मध्यवर्ती परिणामों के एक सेट के रूप में है।
अधिक विस्तृत सामग्री बाद में, भागों में तैयार की जाएगी
समाधान के लिए मसौदा आवश्यकताएँ
एक ऐसा उपकरण विकसित करना आवश्यक है जो आपको स्टोर करने की अनुमति दे:
pg_stat_activity इतिहास देखें pg_locks दृश्य का उपयोग करके सत्र लॉक इतिहास
समाधान की आवश्यकता-लक्ष्य डेटाबेस पर प्रभाव को कम करें।
सामान्य विचार- डेटा संग्रह एजेंट को लक्ष्य डेटाबेस में नहीं, बल्कि सिस्टमडी सेवा के रूप में मॉनिटरिंग डेटाबेस में लॉन्च किया जाता है। हां, कुछ डेटा हानि संभव है, लेकिन यह रिपोर्टिंग के लिए महत्वपूर्ण नहीं है, लेकिन मेमोरी और डिस्क स्थान के संदर्भ में लक्ष्य डेटाबेस पर कोई प्रभाव नहीं पड़ता है। और कनेक्शन पूल का उपयोग करने के मामले में, उपयोगकर्ता प्रक्रियाओं पर प्रभाव न्यूनतम होता है।
कार्यान्वयन चरण
1.सेवा टेबल
तालिकाओं को संग्रहीत करने के लिए एक अलग स्कीमा का उपयोग किया जाता है, ताकि उपयोग की जाने वाली मुख्य तालिकाओं के विश्लेषण को जटिल न बनाया जा सके।
DROP SCHEMA IF EXISTS activity_hist ;
CREATE SCHEMA activity_hist AUTHORIZATION monitor ;
महत्वपूर्ण: स्कीमा लक्ष्य डेटाबेस में नहीं, बल्कि मॉनिटरिंग डेटाबेस में बनाई गई है।
pg_stat_activity इतिहास देखें
pg_stat_activity दृश्य के वर्तमान स्नैपशॉट को संग्रहीत करने के लिए एक तालिका का उपयोग किया जाता है
गतिविधि_हिस्ट.इतिहास_पीजी_स्टेट_गतिविधि :
--ACTIVITY_HIST.HISTORY_PG_STAT_ACTIVITY
DROP TABLE IF EXISTS activity_hist.history_pg_stat_activity;
CREATE TABLE activity_hist.history_pg_stat_activity
(
timepoint timestamp without time zone ,
datid oid ,
datname name ,
pid integer,
usesysid oid ,
usename name ,
application_name text ,
client_addr inet ,
client_hostname text ,
client_port integer,
backend_start timestamp with time zone ,
xact_start timestamp with time zone ,
query_start timestamp with time zone ,
state_change timestamp with time zone ,
wait_event_type text ,
wait_event text ,
state text ,
backend_xid xid ,
backend_xmin xid ,
query text ,
backend_type text ,
queryid bigint
);
प्रविष्टि को तेज़ करने के लिए - कोई अनुक्रमणिका या प्रतिबंध नहीं।
इतिहास को संग्रहीत करने के लिए, एक विभाजित तालिका का उपयोग किया जाता है:
गतिविधि_हिस्ट.संग्रह_pg_stat_activity :
DROP TABLE IF EXISTS activity_hist.archive_pg_stat_activity;
CREATE TABLE activity_hist.archive_pg_stat_activity
(
timepoint timestamp without time zone ,
datid oid ,
datname name ,
pid integer,
usesysid oid ,
usename name ,
application_name text ,
client_addr inet ,
client_hostname text ,
client_port integer,
backend_start timestamp with time zone ,
xact_start timestamp with time zone ,
query_start timestamp with time zone ,
state_change timestamp with time zone ,
wait_event_type text ,
wait_event text ,
state text ,
backend_xid xid ,
backend_xmin xid ,
query text ,
backend_type text ,
queryid bigint
)
PARTITION BY RANGE (timepoint);
चूँकि इस मामले में प्रविष्टि गति के लिए कोई आवश्यकता नहीं है, रिपोर्ट के निर्माण में तेजी लाने के लिए कुछ सूचकांक बनाए गए हैं।
सत्र अवरोधन इतिहास
सत्र लॉक के वर्तमान स्नैपशॉट को संग्रहीत करने के लिए एक तालिका का उपयोग किया जाता है:
गतिविधि_हिस्ट.इतिहास_लॉकिंग :
--ACTIVITY_HIST.HISTORY_LOCKING
DROP TABLE IF EXISTS activity_hist.history_locking;
CREATE TABLE activity_hist.history_locking
(
timepoint timestamp without time zone ,
locktype text ,
relation oid ,
mode text ,
tid xid ,
vtid text ,
pid integer ,
blocking_pids integer[] ,
granted boolean
);
इसके अलावा, प्रविष्टि को तेज़ करने के लिए, कोई अनुक्रमणिका या प्रतिबंध नहीं हैं।
इतिहास को संग्रहीत करने के लिए, एक विभाजित तालिका का उपयोग किया जाता है:
गतिविधि_हिस्ट.संग्रह_लॉकिंग:
DROP TABLE IF EXISTS activity_hist.archive_locking;
CREATE TABLE activity_hist.archive_locking
(
timepoint timestamp without time zone ,
locktype text ,
relation oid ,
mode text ,
tid xid ,
vtid text ,
pid integer ,
blocking_pids integer[] ,
granted boolean
)
PARTITION BY RANGE (timepoint);
चूँकि इस मामले में प्रविष्टि गति के लिए कोई आवश्यकता नहीं है, रिपोर्ट के निर्माण में तेजी लाने के लिए कुछ सूचकांक बनाए गए हैं।
2.वर्तमान इतिहास भरना
सीधे दृश्य स्नैपशॉट एकत्र करने के लिए, एक बैश स्क्रिप्ट का उपयोग किया जाता है जो plpgsql फ़ंक्शन चलाता है।
plpgsql Dblink फ़ंक्शन लक्ष्य डेटाबेस में दृश्यों तक पहुँचता है और मॉनिटरिंग डेटाबेस में सेवा तालिकाओं में पंक्तियाँ सम्मिलित करता है।
get_current_activity.sql
CREATE OR REPLACE FUNCTION activity_hist.get_current_activity( current_host text , current_s_name text , current_s_pass text ) RETURNS BOOLEAN AS $$
DECLARE
database_rec record;
dblink_str text ;
BEGIN
EXECUTE 'SELECT dblink_connect(''LINK1'',''host='||current_host||' port=5432 dbname=postgres'||
' user='||current_s_name||' password='||current_s_pass|| ' '')';
--------------------------------------------------------------------
--GET pg_stat_activity stats
INSERT INTO activity_hist.history_pg_stat_activity
(
SELECT * FROM dblink('LINK1',
'SELECT
now() ,
datid ,
datname ,
pid ,
usesysid ,
usename ,
application_name ,
client_addr ,
client_hostname ,
client_port ,
backend_start ,
xact_start ,
query_start ,
state_change ,
wait_event_type ,
wait_event ,
state ,
backend_xid ,
backend_xmin ,
query ,
backend_type
FROM pg_stat_activity
')
AS t (
timepoint timestamp without time zone ,
datid oid ,
datname name ,
pid integer,
usesysid oid ,
usename name ,
application_name text ,
client_addr inet ,
client_hostname text ,
client_port integer,
backend_start timestamp with time zone ,
xact_start timestamp with time zone ,
query_start timestamp with time zone ,
state_change timestamp with time zone ,
wait_event_type text ,
wait_event text ,
state text ,
backend_xid xid ,
backend_xmin xid ,
query text ,
backend_type text
)
);
---------------------------------------
--ACTIVITY_HIST.HISTORY_LOCKING
INSERT INTO activity_hist.history_locking
(
SELECT * FROM dblink('LINK1',
'SELECT
now() ,
lock.locktype,
lock.relation,
lock.mode,
lock.transactionid as tid,
lock.virtualtransaction as vtid,
lock.pid,
pg_blocking_pids(lock.pid),
lock.granted
FROM pg_catalog.pg_locks lock LEFT JOIN pg_catalog.pg_database db ON db.oid = lock.database
WHERE NOT lock.pid = pg_backend_pid()
')
AS t (
timepoint timestamp without time zone ,
locktype text ,
relation oid ,
mode text ,
tid xid ,
vtid text ,
pid integer ,
blocking_pids integer[] ,
granted boolean
)
);
PERFORM dblink_disconnect('LINK1');
RETURN TRUE ;
END
$$ LANGUAGE plpgsql;
दृश्य स्नैपशॉट एकत्र करने के लिए, सिस्टमडी सेवा और दो स्क्रिप्ट का उपयोग किया जाता है:
pg_current_activity.service
# /etc/systemd/system/pg_current_activity.service
[Unit]
Description=Collect history of pg_stat_activity , pg_locks
Wants=pg_current_activity.timer
[Service]
Type=forking
StartLimitIntervalSec=0
ExecStart=/home/postgres/pgutils/demon/get_current_activity.sh 10.124.70.40 postgres postgres
[Install]
WantedBy=multi-user.target
pg_current_activity.timer
# /etc/systemd/system/pg_current_activity.timer
[Unit]
Description=Run pg_current_activity.sh every 1 second
Requires=pg_current_activity.service
[Timer]
Unit=pg_current_activity.service
OnCalendar=*:*:0/1
AccuracySec=1
[Install]
WantedBy=timers.target
स्क्रिप्ट को अधिकार सौंपें:
# chmod 755 pg_current_activity.timer
# chmod 755 pg_current_activity.service
आइए सेवा शुरू करें:
# systemctl डेमॉन-रीलोड
# systemctl प्रारंभ pg_current_activity.service
इस प्रकार, दृश्यों का इतिहास सेकंड-दर-सेकंड स्नैपशॉट के रूप में एकत्र किया जाता है। निःसंदेह, यदि सब कुछ वैसे ही छोड़ दिया जाए, तो तालिकाओं का आकार बहुत तेज़ी से बढ़ जाएगा और अधिक या कम उत्पादक कार्य असंभव हो जाएगा।
डेटा संग्रह को व्यवस्थित करना आवश्यक है।
3. इतिहास संग्रहित करना
संग्रह करने के लिए, विभाजित तालिका संग्रह* का उपयोग किया जाता है।
हर घंटे नए विभाजन बनाए जाते हैं, जबकि पुराना डेटा इतिहास* तालिकाओं से हटा दिया जाता है, इसलिए इतिहास* तालिकाओं का आकार ज्यादा नहीं बदलता है और समय के साथ प्रविष्टि गति कम नहीं होती है।
नए अनुभागों का निर्माण plpgsql फ़ंक्शन गतिविधि_हिस्ट.आर्काइव_करंट_एक्टिविटी द्वारा किया जाता है। कार्य का एल्गोरिथ्म बहुत सरल है (archive_pg_stat_activity तालिका के लिए अनुभाग के उदाहरण का उपयोग करके)।
एक नया अनुभाग बनाएं और भरें
EXECUTE format(
'CREATE TABLE ' || partition_name ||
' PARTITION OF activity_hist.archive_pg_stat_activity FOR VALUES FROM ( %L ) TO ( %L ) ' ,
to_char(date_trunc('year', partition_min_range ),'YYYY')||'-'||
to_char(date_trunc('month', partition_min_range ),'MM')||'-'||
to_char(date_trunc('day', partition_min_range ),'DD')||' '||
to_char(date_trunc('hour', partition_min_range ),'HH24')||':00',
to_char(date_trunc('year', partition_max_range ),'YYYY')||'-'||
to_char(date_trunc('month', partition_max_range ),'MM')||'-'||
to_char(date_trunc('day', partition_max_range ),'DD')||' '||
to_char(date_trunc('hour', partition_max_range ),'HH24')||':00'
);
INSERT INTO activity_hist.archive_pg_stat_activity
(
SELECT *
FROM activity_hist.history_pg_stat_activity
WHERE timepoint BETWEEN partition_min_range AND partition_max_range
);
अनुक्रमणिका बनाना
EXECUTE format (
'CREATE INDEX '||index_name||
' ON '||partition_name||' ( wait_event_type , backend_type , timepoint )'
);
EXECUTE format ('CREATE INDEX '||index_name||
' ON '||partition_name||' ( wait_event_type , backend_type , timepoint , queryid )'
);
हिस्ट्री_पीजी_स्टैट_एक्टिविटी टेबल से पुराना डेटा हटा रहा है
DELETE
FROM activity_hist.history_pg_stat_activity
WHERE timepoint < partition_max_range;
बेशक, समय-समय पर पुराने अनुभागों को अनावश्यक मानकर हटा दिया जाता है।
बुनियादी रिपोर्ट
दरअसल, ये सब क्यों किया जा रहा है? रिपोर्ट प्राप्त करने के लिए बहुत अस्पष्ट रूप से Oracle के AWR की याद दिलाती है।
यह जोड़ना महत्वपूर्ण है कि रिपोर्ट प्राप्त करने के लिए, आपको pg_stat_activity और pg_stat_statements दृश्यों के बीच संबंध बनाना होगा। तालिकाओं को 'history_pg_stat_activity', 'archive_pg_stat_activity' तालिकाओं में 'queryid' कॉलम जोड़कर लिंक किया गया है। कॉलम मान जोड़ने की विधि इस आलेख के दायरे से परे है और यहां वर्णित है - pg_stat_statements + pg_stat_activity + loq_query = pg_ash? .
प्रश्नों के लिए कुल CPU समय
अनुरोध :
WITH hist AS
(
SELECT
aa.query ,aa.queryid ,
count(*) * interval '1 second' AS duration
FROM activity_hist.archive_pg_stat_activity aa
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 backend_type = 'client backend' AND datname != 'postgres' AND ( aa.wait_event_type IS NULL ) ANDaa.state = 'active'
GROUP BY aa.wait_event_type , aa.wait_event , aa.query ,aa.queryid
UNION
SELECT
ha.query ,ha.queryid,
count(*) * interval '1 second' AS duration
FROM activity_hist.history_pg_stat_activity_for_reports ha
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 backend_type = 'client backend' AND datname != 'postgres' AND ( ha.wait_event_type IS NULL )AND ha.state = 'active'
GROUP BY ha.wait_event_type , ha.wait_event , ha.query ,ha.queryid
)
SELECT query , queryid , SUM( duration ) as duration
FROM hist
GROUP BY query , queryid
ORDER BY 3 DESC
WITH hist AS
(
SELECT
aa.query ,aa.queryid ,
count(*) * interval '1 second' AS duration
FROM activity_hist.archive_pg_stat_activity aa
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
backend_type = 'client backend' AND datname != 'postgres' AND
( aa.wait_event_type IS NOT NULL )
GROUP BY aa.wait_event_type , aa.wait_event , aa.query ,aa.queryid
UNION
SELECT
ha.query ,ha.queryid,
count(*) * interval '1 second' AS duration
FROM activity_hist.history_pg_stat_activity_for_reports ha
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
backend_type = 'client backend' AND datname != 'postgres' AND
( ha.wait_event_type IS NOT NULL )
GROUP BY ha.wait_event_type , ha.wait_event , ha.query ,ha.queryid
)
SELECT query , queryid , SUM( duration ) as duration
FROM hist
GROUP BY query , queryid
ORDER BY 3 DESC
WITH hist AS
(
SELECT
aa.wait_event_type , aa.wait_event
FROM activity_hist.archive_pg_stat_activity aa
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
backend_type = 'client backend' AND datname != 'postgres' AND
aa.wait_event IS NOT NULL
GROUP BY aa.wait_event_type , aa.wait_event
UNION
SELECT
ha.wait_event_type , ha.wait_event
FROM activity_hist.history_pg_stat_activity_for_reports ha
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
backend_type = 'client backend' AND datname != 'postgres' AND
ha.wait_event IS NOT NULL
GROUP BY ha.wait_event_type , ha.wait_event
)
SELECT wait_event_type , wait_event
FROM hist
GROUP BY wait_event_type , wait_event
ORDER BY 1 ASC,2 ASC
----------------------------------------------------------------------
WITH hist AS
(
SELECT
aa.wait_event_type , aa.wait_event , aa.query ,aa.queryid ,
count(*) * interval '1 second' AS duration
FROM activity_hist.archive_pg_stat_activity aa
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
backend_type = 'client backend' AND datname != 'postgres' AND
( aa.wait_event_type = waitings_stat_rec.wait_event_type AND aa.wait_event = waitings_stat_rec.wait_event )
GROUP BY aa.wait_event_type , aa.wait_event , aa.query ,aa.queryid
UNION
SELECT
ha.wait_event_type , ha.wait_event , ha.query ,ha.queryid,
count(*) * interval '1 second' AS duration
FROM activity_hist.history_pg_stat_activity_for_reports ha
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
backend_type = 'client backend' AND datname != 'postgres' AND
( ha.wait_event_type = waitings_stat_rec.wait_event_type AND ha.wait_event = waitings_stat_rec.wait_event )
GROUP BY ha.wait_event_type , ha.wait_event , ha.query ,ha.queryid
)
SELECT query , queryid , SUM( duration ) as duration
FROM hist
GROUP BY query , queryid
ORDER BY 3 DESC
SELECT
MIN(date_trunc('second',timepoint)) AS started ,
count(*) * interval '1 second' as duration ,
pid , blocking_pids , relation , mode , locktype
FROM
activity_hist.archive_locking al
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
locktype = 'relation'
GROUP BY pid , blocking_pids , relation , mode , locktype
UNION
SELECT
MIN(date_trunc('second',timepoint)) AS started ,
count(*) * interval '1 second' as duration ,
pid , blocking_pids , relation , mode , locktype
FROM
activity_hist.history_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
locktype = 'relation'
GROUP BY pid , blocking_pids , relation , mode , locktype
ORDER BY 1
SELECT
blocking_pids
FROM
activity_hist.archive_locking al
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
locktype = 'relation'
GROUP BY blocking_pids
UNION
SELECT
blocking_pids
FROM
activity_hist.history_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
locktype = 'relation'
GROUP BY blocking_pids
ORDER BY 1
---------------------------------------------------------------
SELECT
pid , usename , application_name , datname ,
MIN(date_trunc('second',timepoint)) as started ,
count(*) * interval '1 second' as duration ,
state ,
query
FROM activity_hist.archive_pg_stat_activity
WHERE pid= current_pid AND
timepoint BETWEEN pg_stat_history_begin+(current_hour_diff * interval '1 hour') AND pg_stat_history_end+(current_hour_diff * interval '1 hour')
GROUP BY pid , usename , application_name ,
datname ,
state_change,
state ,
query
UNION
SELECT
pid , usename , application_name , datname ,
MIN(date_trunc('second',timepoint)) as started ,
count(*) * interval '1 second' as duration ,
state ,
query
FROM activity_hist.history_pg_stat_activity_for_reports
WHERE pid= current_pid AND
timepoint BETWEEN pg_stat_history_begin+(current_hour_diff * interval '1 hour') AND pg_stat_history_end+(current_hour_diff * interval '1 hour')
GROUP BY pid , usename , application_name ,
datname ,
state_change,
state ,
query
ORDER BY 5 , 1
प्रदर्शन की घटनाओं का विश्लेषण करते समय दिखाए गए बुनियादी प्रश्न और परिणामी रिपोर्ट पहले से ही जीवन को बहुत आसान बनाते हैं।
बुनियादी प्रश्नों के आधार पर, आप एक रिपोर्ट प्राप्त कर सकते हैं जो ओरेकल के AWR से मिलती जुलती है। सारांश रिपोर्ट उदाहरण
+---------------------------------------------------------------- -------------------------------------- | गतिविधि और प्रतीक्षा के लिए समेकित रिपोर्ट।
करने के लिए जारी। अगली पंक्ति में लॉक हिस्ट्री (pg_stat_locks) का निर्माण है, जो तालिकाओं को भरने की प्रक्रिया का अधिक विस्तृत विवरण है।