Skip to content

Commit

Permalink
feat: changing now function to clock_timestamp
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuel Calvo <[email protected]>
  • Loading branch information
3manuek committed Jun 12, 2023
1 parent e9f9fc6 commit 0230ac7
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions sql/Activity/activity.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions sql/Activity/get_average_duration_per_query_group.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion sql/Activity/idle_in_transaction.sql
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 5 additions & 5 deletions sql/AutoVacuum/large_tables_non_autovacuumed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions sql/BackgroundWriter/bgwriter_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

6 changes: 3 additions & 3 deletions sql/Connections/terminate_old_connections.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion sql/Locks/locks_flight.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sql/Locks/tree_of_locks.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 0230ac7

Please sign in to comment.