From 0230ac7892322526ed99878848148965250aa20e Mon Sep 17 00:00:00 2001 From: Emanuel Calvo <3manuek@gmail.com> Date: Mon, 12 Jun 2023 14:11:55 +0200 Subject: [PATCH] feat: changing now function to clock_timestamp Signed-off-by: Emanuel Calvo <3manuek@gmail.com> --- sql/Activity/activity.sql | 4 ++-- sql/Activity/get_average_duration_per_query_group.sql | 4 ++-- sql/Activity/idle_in_transaction.sql | 2 +- sql/AutoVacuum/large_tables_non_autovacuumed.sql | 10 +++++----- sql/BackgroundWriter/bgwriter_stats.sql | 4 ++-- sql/Connections/terminate_old_connections.sql | 6 +++--- sql/Locks/locks_flight.sql | 2 +- sql/Locks/tree_of_locks.sql | 2 +- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/sql/Activity/activity.sql b/sql/Activity/activity.sql index 6df2350..d2e15fb 100644 --- a/sql/Activity/activity.sql +++ b/sql/Activity/activity.sql @@ -1,9 +1,9 @@ -- Get activity and lock state -SELECT (now() - a.query_start) > '00:01:00'::interval AS long_tx, +SELECT (clock_timestamp() - a.query_start) > '00:01:00'::interval AS long_tx, a.pid, u.usename AS username, a.wait_event_type, - now() - a.query_start AS "time", + clock_timestamp() - a.query_start AS "time", kl.pid AS b_pid, a.state, a.query diff --git a/sql/Activity/get_average_duration_per_query_group.sql b/sql/Activity/get_average_duration_per_query_group.sql index a2b73ca..dc5104d 100644 --- a/sql/Activity/get_average_duration_per_query_group.sql +++ b/sql/Activity/get_average_duration_per_query_group.sql @@ -1,6 +1,6 @@ -- Gets max /avg duration per usenrname or query pattern - select usename, count(*), avg(now()-query_start), - max(now()-query_start) + select usename, count(*), avg(clock_timestamp()-query_start), + max(clock_timestamp()-query_start) from pg_stat_activity where 1 = 1 diff --git a/sql/Activity/idle_in_transaction.sql b/sql/Activity/idle_in_transaction.sql index 2f79c05..932c85b 100644 --- a/sql/Activity/idle_in_transaction.sql +++ b/sql/Activity/idle_in_transaction.sql @@ -1,6 +1,6 @@ -- Hunting idle in transaction queries -SELECT (now() - pg_stat_activity.xact_start) AS age, +SELECT (clock_timestamp() - pg_stat_activity.xact_start) AS age, pg_stat_activity.datname, pg_stat_activity.pid, pg_stat_activity.usename, pg_stat_activity.state, pg_stat_activity.query_start, pg_stat_activity.client_addr, pg_stat_activity.query FROM pg_stat_activity WHERE (pg_stat_activity.xact_start IS NOT NULL) diff --git a/sql/AutoVacuum/large_tables_non_autovacuumed.sql b/sql/AutoVacuum/large_tables_non_autovacuumed.sql index ddb1704..e52000a 100644 --- a/sql/AutoVacuum/large_tables_non_autovacuumed.sql +++ b/sql/AutoVacuum/large_tables_non_autovacuumed.sql @@ -5,11 +5,11 @@ select pg_size_pretty(pg_total_relation_size(relname::regclass)) as rank_size, rank() over ( order by pg_total_relation_size(relname::regclass) desc), n_dead_tup, - -- n_dead_tup / extract('days' from now()-last_vacuum) as dead_by_day, - -- extract('days' from now()-last_vacuum) as last_vacuum, - extract('days' from now()-last_autovacuum) as last_autovacuum, - -- extract('days' from now()-last_analyze) as last_analyze, - extract('days' from now()-last_autoanalyze) as last_autoanalyze, + -- n_dead_tup / extract('days' from clock_timestamp()-last_vacuum) as dead_by_day, + -- extract('days' from clock_timestamp()-last_vacuum) as last_vacuum, + extract('days' from clock_timestamp()-last_autovacuum) as last_autovacuum, + -- extract('days' from clock_timestamp()-last_analyze) as last_analyze, + extract('days' from clock_timestamp()-last_autoanalyze) as last_autoanalyze, -- vacuum_count, autovacuum_count, -- analyze_count, diff --git a/sql/BackgroundWriter/bgwriter_stats.sql b/sql/BackgroundWriter/bgwriter_stats.sql index 3874deb..bcefa3d 100644 --- a/sql/BackgroundWriter/bgwriter_stats.sql +++ b/sql/BackgroundWriter/bgwriter_stats.sql @@ -10,8 +10,8 @@ SELECT CASE WHEN checkpoints_timed+checkpoints_req>0 THEN round((checkpoint_write_time/checkpoints_timed+checkpoints_req)/1000) ELSE 0 END as per_chkpt_seconds, buffers_alloc, stats_reset::timestamp(0), - CASE WHEN date_part('day',(now() - stats_reset)::interval) > 0 THEN round(checkpoints_req + checkpoints_timed / (date_part('day',(now() - stats_reset)::interval))) ELSE 0 END as chks_per_day, - CASE WHEN date_part('day',(now() - stats_reset)::interval) > 0 THEN round(checkpoints_req / (date_part('day',(now() - stats_reset)::interval))) ELSE 0 END as chk_req_per_day + CASE WHEN date_part('day',(clock_timestamp() - stats_reset)::interval) > 0 THEN round(checkpoints_req + checkpoints_timed / (date_part('day',(clock_timestamp() - stats_reset)::interval))) ELSE 0 END as chks_per_day, + CASE WHEN date_part('day',(clock_timestamp() - stats_reset)::interval) > 0 THEN round(checkpoints_req / (date_part('day',(clock_timestamp() - stats_reset)::interval))) ELSE 0 END as chk_req_per_day FROM pg_stat_bgwriter; diff --git a/sql/Connections/terminate_old_connections.sql b/sql/Connections/terminate_old_connections.sql index 4f2d86d..17dac81 100644 --- a/sql/Connections/terminate_old_connections.sql +++ b/sql/Connections/terminate_old_connections.sql @@ -25,12 +25,12 @@ as $$ datname, usename, state, - age(now(), query_start), - age(now(), state_change), + age(clock_timestamp(), query_start), + age(clock_timestamp(), state_change), pg_terminate_backend(pid) from pg_stat_activity where - state_change < now() - age_minutes * interval '1 minute' + state_change < clock_timestamp() - age_minutes * interval '1 minute' and (any_status or state = 'idle') order by state_change; $$ language sql; diff --git a/sql/Locks/locks_flight.sql b/sql/Locks/locks_flight.sql index 9393ea0..836370b 100644 --- a/sql/Locks/locks_flight.sql +++ b/sql/Locks/locks_flight.sql @@ -6,7 +6,7 @@ SELECT array_agg( distinct pg_blocking_pids(pid)) as "BlockingPids", count(pl.*) as "NumLocks", array_agg( pl.mode) as "Modes", - now() - xact_start as "Running time" + clock_timestamp() - xact_start as "Running time" FROM pg_stat_activity pa join pg_locks pl using (pid) WHERE application_name ~ 'sidekiq' and wait_event IS NOT NULL diff --git a/sql/Locks/tree_of_locks.sql b/sql/Locks/tree_of_locks.sql index 3381547..be5da72 100644 --- a/sql/Locks/tree_of_locks.sql +++ b/sql/Locks/tree_of_locks.sql @@ -51,4 +51,4 @@ from tree left join pairs w on w.waiter = tree.pid and w.locker = tree.dad join pg_stat_activity a using (pid) join pg_stat_activity r on r.pid=tree.root -order by (now() - r.xact_start), path; +order by (clock_timestamp() - r.xact_start), path;