Last active
November 29, 2024 00:58
-
-
Save radityopw/6e7ee675eb8e26af9a989d838e5349c6 to your computer and use it in GitHub Desktop.
query utilitas postgresql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kumpulan query utilitas untuk postgresql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT relname AS Table_Name, n_live_tup AS Live_Tuples, | |
n_dead_tup AS Dead_Tuples FROM | |
pg_stat_user_tables where relname = 'coba'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
schemaname, relname, | |
last_vacuum, last_autovacuum, | |
vacuum_count, autovacuum_count, | |
last_analyze,last_autoanalyze | |
FROM pg_stat_user_tables | |
where relname = 'tablename'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT pid, age(backend_xid) AS age_in_xids, | |
now () - xact_start AS xact_age, | |
now () - query_start AS query_age, | |
state, | |
query | |
FROM pg_stat_activity | |
WHERE state != 'idle' | |
ORDER BY 2 DESC | |
LIMIT 10; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT gid, prepared, owner, database, transaction | |
FROM pg_prepared_xacts | |
ORDER BY age(transaction) DESC; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--The query doesn't take into consideration that autovacuum can be configured on a per-table basis using the "alter table" DDL command. | |
SELECT * | |
,n_dead_tup > av_threshold AS av_needed | |
,CASE | |
WHEN reltuples > 0 | |
THEN round(100.0 * n_dead_tup / (reltuples)) | |
ELSE 0 | |
END AS pct_dead | |
FROM ( | |
SELECT N.nspname | |
,C.relname | |
,pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins | |
,pg_stat_get_tuples_updated(C.oid) AS n_tup_upd | |
,pg_stat_get_tuples_deleted(C.oid) AS n_tup_del | |
,pg_stat_get_live_tuples(C.oid) AS n_live_tup | |
,pg_stat_get_dead_tuples(C.oid) AS n_dead_tup | |
,C.reltuples AS reltuples | |
,round(current_setting('autovacuum_vacuum_threshold')::INTEGER + current_setting('autovacuum_vacuum_scale_factor')::NUMERIC * C.reltuples) AS av_threshold | |
,date_trunc('minute', greatest(pg_stat_get_last_vacuum_time(C.oid), pg_stat_get_last_autovacuum_time(C.oid))) AS last_vacuum | |
,date_trunc('minute', greatest(pg_stat_get_last_analyze_time(C.oid), pg_stat_get_last_autoanalyze_time(C.oid))) AS last_analyze | |
FROM pg_class C | |
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) | |
WHERE C.relkind IN ( | |
'r' | |
,'t' | |
) | |
AND N.nspname NOT IN ( | |
'pg_catalog' | |
,'information_schema' | |
) | |
AND N.nspname !~ '^pg_toast' | |
) AS av | |
ORDER BY av_needed DESC ,n_dead_tup DESC; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT slot_name, slot_type, database, xmin | |
FROM pg_replication_slots | |
ORDER BY age(xmin) DESC; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select schemaname,relname,n_dead_tup,n_live_tup,round(n_dead_tup::float/n_live_tup::float*100) dead_pct | |
,autovacuum_count,last_vacuum,last_autovacuum | |
,last_autoanalyze,last_analyze | |
from pg_stat_all_tables | |
where n_live_tup >0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER TABLE <table name> SET (autovacuum_analyze_scale_factor = xx); | |
ALTER TABLE <table name> SET (autovacuum_analyze_threshold = xx); | |
ALTER TABLE <table name> SET (autovacuum_vacuum_scale_factor = xx); | |
ALTER TABLE <table name> SET (autovacuum_vacuum_threshold = xx); | |
ALTER TABLE <table name> SET (autovacuum_vacuum_cost_delay = xx); | |
ALTER TABLE <table name> SET (autovacuum_vacuum_cost_limit = xx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment