From 27429f6f7c1b1361865ebdd8aca0df7267a5a7c9 Mon Sep 17 00:00:00 2001 From: Emanuel Calvo <3manuek@gmail.com> Date: Wed, 23 Aug 2023 18:21:22 +0000 Subject: [PATCH] Adding documentation and cosmetic fixes --- .../most_active_tables.sql} | 2 +- sql/DDL/get_create_table.md | 3 ++ sql/PgStatStatements/unstable_queries.sql | 2 +- sql/Replication/Logical/publication_info.sql | 2 +- .../leader_replication_status_v10+.sql | 34 +++++++++---------- .../{difference.sql => lsn_difference.sql} | 0 sql/Settings/all_settings.sql | 6 ++++ sql/Settings/generic.sql | 15 -------- sql/SharedBuffers/shared_buffers_agg.md | 5 +++ .../shared_buffers_inspection.md | 5 +++ .../shared_buffers_inspection.sql | 6 ++-- ...inality.sql => attributes_cardinality.sql} | 0 sql/WAL/distance_wals.sql | 3 +- 13 files changed, 43 insertions(+), 40 deletions(-) rename sql/{Connections/activity.sql => Activity/most_active_tables.sql} (98%) create mode 100644 sql/DDL/get_create_table.md rename sql/Replication/Streaming/{difference.sql => lsn_difference.sql} (100%) create mode 100644 sql/Settings/all_settings.sql delete mode 100644 sql/Settings/generic.sql create mode 100644 sql/SharedBuffers/shared_buffers_agg.md create mode 100644 sql/SharedBuffers/shared_buffers_inspection.md rename sql/Statistics/{cardinality.sql => attributes_cardinality.sql} (100%) diff --git a/sql/Connections/activity.sql b/sql/Activity/most_active_tables.sql similarity index 98% rename from sql/Connections/activity.sql rename to sql/Activity/most_active_tables.sql index 2094601..5e22e0d 100644 --- a/sql/Connections/activity.sql +++ b/sql/Activity/most_active_tables.sql @@ -1,4 +1,4 @@ --- Most active tables. 8.4 +-- Most active tables. +v8.4 -- Table activity limited to top 10 by size of the table -- This query will show the size of the table with/out indexes , how many times was accessed seq/index, and metrics of the several cache hits of the table(rows, index, pg_toast, index.pg_toast) WITH most_active_tables AS ( diff --git a/sql/DDL/get_create_table.md b/sql/DDL/get_create_table.md new file mode 100644 index 0000000..d1df576 --- /dev/null +++ b/sql/DDL/get_create_table.md @@ -0,0 +1,3 @@ +# Get CREATE TABLE query + +This query brings the DDL of the defined table in the filter `c.relname like ''`. Change the filter with the table that you want to inspect. \ No newline at end of file diff --git a/sql/PgStatStatements/unstable_queries.sql b/sql/PgStatStatements/unstable_queries.sql index 63a2d1b..0da90e3 100644 --- a/sql/PgStatStatements/unstable_queries.sql +++ b/sql/PgStatStatements/unstable_queries.sql @@ -1,5 +1,5 @@ -- Unstable queries, with larges deltas. - +-- Uses the standard deviation for calculating the query stability SELECT queryid, calls,mean_exec_time, max_exec_time, query, rows, shared_blks_read, diff --git a/sql/Replication/Logical/publication_info.sql b/sql/Replication/Logical/publication_info.sql index 6e070e4..f9021e2 100644 --- a/sql/Replication/Logical/publication_info.sql +++ b/sql/Replication/Logical/publication_info.sql @@ -1,4 +1,4 @@ --- getting publication info more verbose 10 +-- Getting verbose publication info for +v10 select pubname, pu.usename, puballtables , pr.prrelid::regclass as source_class, ltrim(case pubinsert when true then ' ins' end || case pubdelete when true then ' del' end || case pubupdate when true then ' upd' end) as events diff --git a/sql/Replication/Streaming/leader_replication_status_v10+.sql b/sql/Replication/Streaming/leader_replication_status_v10+.sql index c003d0b..e4f19c0 100644 --- a/sql/Replication/Streaming/leader_replication_status_v10+.sql +++ b/sql/Replication/Streaming/leader_replication_status_v10+.sql @@ -1,19 +1,19 @@ -- Only for getting the status of those pids for streaming replication. -- - SELECT client_addr, - client_hostname, - client_port, - rs.slot_name, - rs.slot_type, - state, - pg_wal_lsn_diff(pg_current_wal_flush_lsn(), flush_lsn), --in bytes - pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_flush_lsn(), flush_lsn)), - pg_wal_lsn_diff(pg_current_wal_lsn(), write_lsn) as fromCurrentToWriteLSN, --in bytes - pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), write_lsn)) as fromCurrentToWriteLSN_pretty, - write_lsn, sent_lsn, flush_lsn, replay_lsn, - write_lag, flush_lag, replay_lag, - rs.restart_lsn, rs.confirmed_flush_lsn, - sync_state, sync_priority - FROM pg_stat_replication sr JOIN pg_replication_slots rs - ON (sr.pid = rs.active_pid) - WHERE rs.slot_type = 'physical'; \ No newline at end of file +SELECT client_addr, + client_hostname, + client_port, + rs.slot_name, + rs.slot_type, + state, + pg_wal_lsn_diff(pg_current_wal_flush_lsn(), flush_lsn), --in bytes + pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_flush_lsn(), flush_lsn)), + pg_wal_lsn_diff(pg_current_wal_lsn(), write_lsn) as fromCurrentToWriteLSN, --in bytes + pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), write_lsn)) as fromCurrentToWriteLSN_pretty, + write_lsn, sent_lsn, flush_lsn, replay_lsn, + write_lag, flush_lag, replay_lag, + rs.restart_lsn, rs.confirmed_flush_lsn, + sync_state, sync_priority + FROM pg_stat_replication sr JOIN pg_replication_slots rs + ON (sr.pid = rs.active_pid) + WHERE rs.slot_type = 'physical'; \ No newline at end of file diff --git a/sql/Replication/Streaming/difference.sql b/sql/Replication/Streaming/lsn_difference.sql similarity index 100% rename from sql/Replication/Streaming/difference.sql rename to sql/Replication/Streaming/lsn_difference.sql diff --git a/sql/Settings/all_settings.sql b/sql/Settings/all_settings.sql new file mode 100644 index 0000000..27e9ab7 --- /dev/null +++ b/sql/Settings/all_settings.sql @@ -0,0 +1,6 @@ +-- Get settings ordered by context +SELECT name, setting, + short_desc::text , extra_desc::text, + context +FROM pg_settings +ORDER BY context; diff --git a/sql/Settings/generic.sql b/sql/Settings/generic.sql deleted file mode 100644 index 6ba16ea..0000000 --- a/sql/Settings/generic.sql +++ /dev/null @@ -1,15 +0,0 @@ --- Get a better output of pg_settings -/* -SELECT name, category, - setting, - short_desc::text , extra_desc::text, - context, - sourcefile || '#L' || sourceline as varloc -FROM pg_settings -WHERE - category ~ '^Replication' - -- category ~ 'eplication' -ORDER BY context; -*/ -SELECT * -FROM pg_settings; diff --git a/sql/SharedBuffers/shared_buffers_agg.md b/sql/SharedBuffers/shared_buffers_agg.md new file mode 100644 index 0000000..a42d9f7 --- /dev/null +++ b/sql/SharedBuffers/shared_buffers_agg.md @@ -0,0 +1,5 @@ +# Shared Buffers Aggregation + +This query requires the `pg_buffercache` extension installed in the database. + +> NOTE: beware that this extension needs locks for reading blocks in the shared buffers. \ No newline at end of file diff --git a/sql/SharedBuffers/shared_buffers_inspection.md b/sql/SharedBuffers/shared_buffers_inspection.md new file mode 100644 index 0000000..d185fdf --- /dev/null +++ b/sql/SharedBuffers/shared_buffers_inspection.md @@ -0,0 +1,5 @@ +# Shared Buffers Inspection + +It is recommended to LIMIT the bufs table expression in production environments. + +> NOTE: beware that this extension needs locks over shared buffers, use carefully in production environments. \ No newline at end of file diff --git a/sql/SharedBuffers/shared_buffers_inspection.sql b/sql/SharedBuffers/shared_buffers_inspection.sql index c6b70a2..5f1989c 100644 --- a/sql/SharedBuffers/shared_buffers_inspection.sql +++ b/sql/SharedBuffers/shared_buffers_inspection.sql @@ -15,8 +15,7 @@ SELECT c.relname as rel, ORDER BY 2 DESC -- In the case of Productive environments, you can limit the number of relations to reduce the LWLock issuing. -- LIMIT 20 -) -, +), _settings AS ( -- TODO: We want also to consider wal_buffers, max_connection allocations SELECT setting::int @@ -47,5 +46,4 @@ SELECT rel, buffers, -- The percentage of dirty shared buffers sum( round(((100*dirtypages::double precision)/s.setting::double precision)::numeric,2) ) OVER () as dirty_perc_of_sb -from bufs b, _settings s -; +from bufs b, _settings s; diff --git a/sql/Statistics/cardinality.sql b/sql/Statistics/attributes_cardinality.sql similarity index 100% rename from sql/Statistics/cardinality.sql rename to sql/Statistics/attributes_cardinality.sql diff --git a/sql/WAL/distance_wals.sql b/sql/WAL/distance_wals.sql index 4772cf2..3ade513 100644 --- a/sql/WAL/distance_wals.sql +++ b/sql/WAL/distance_wals.sql @@ -1,3 +1,4 @@ -- Execute this on replica to know which is the --- files need to be copied +-- files need to be copied. This returns the current WAL LSN in the replica, +-- you should copy all the WAL files newer than this point. select pg_walfile_name(pg_current_wal_lsn()); \ No newline at end of file