PgHero
No long running queries
Connections healthy 42
Vacuuming healthy
No columns near integer overflow (2 unreadable sequences)
No invalid indexes or constraints
2 duplicate indexes
No suggested indexes
6 slow queries

Unreadable Sequences

This is likely due to missing privileges. Make sure your user has the SELECT privilege for each sequence.

Column Sequence
dashboards.id dashboards_id_seq
sessions.id sessions_id_seq

Duplicate Indexes

These indexes exist, but aren’t needed. Remove them for faster writes.

Details
On services
services_space_id (space_id)
is covered by
services_space_id_entity_id_unique (space_id, entity_id)
On ticket_history
ticket_history_space_id_idx (space_id)
is covered by
ticket_history_space_id_state_branch_id_created_at_user_id_idx (space_id, state, branch_id, created_at, user_id)

Slow Queries

Slow queries take 20 ms or more on average and have been called at least 100 times.

Explain queries to see where to add indexes.

Total Time Average Time Calls
699 min 54% 805 ms 52,071 postgres
-- scheduler/jobs
      SELECT id
      FROM jobs
      WHERE rrule IS NOT NULL
      AND status = $1
      AND (now() >= next_run OR next_run IS NULL)
      ORDER BY next_run ASC NULLS FIRST, updated_at ASC
      FOR UPDATE SKIP LOCKED
118 min 9% 60 ms 118,134 postgres
with operators as (
      select
        distinct on (u.id) u.id as user_id,
        s.id as session_id,
        c.id as counter_id,
        (case when c.override_user_services then cs.priority else $5 end) as counter_service_priority,
        us.priority as user_service_priority
      from users u
      join sessions s on s.user_id = u.id
      join counters c on c.id = s.counter_id
      join branch_sessions bs on bs.id = s.branch_session_id
      join counters_services cs on cs.counter_id = c.id and cs.service_id = $1
      left join users_services us on us.user_id = u.id and us.service_id = $2
      where u.space_id = $3
      
      and s.status = $6
      and s.branch_id = $4
      and s.counter_id is not null
      and bs.current = $7
      and not exists (
        select $8
        from tickets t
        where t.space_id = u.space_id
        and t.user_id = u.id
        and t.branch_session_id = s.branch_session_id
        and t.state in ($9)
        limit $10
      )
      and (
        select us.status
        from user_statuses us
        where us.user_id = u.id
        and us.space_id = u.space_id
        order by us.created_at desc
        limit $11
      ) = $12
    )
    select user_id, session_id, counter_id
    from operators
    order by counter_service_priority desc nulls last, user_service_priority asc
    limit $13
2 min 0.2% 21 ms 6,580 postgres
SELECT schemaname AS schema, t.relname AS table, ix.relname AS name, regexp_replace(pg_get_indexdef(i.indexrelid), $1, $2) AS columns, regexp_replace(pg_get_indexdef(i.indexrelid), $3, $4) AS using, indisunique AS unique, indisprimary AS primary, indisvalid AS valid, indexprs::text, indpred::text, pg_get_indexdef(i.indexrelid) AS definition FROM pg_index i INNER JOIN pg_class t ON t.oid = i.indrelid INNER JOIN pg_class ix ON ix.oid = i.indexrelid LEFT JOIN pg_stat_user_indexes ui ON ui.indexrelid = i.indexrelid WHERE schemaname IS NOT NULL ORDER BY 1, 2 /*pghero*/
1 min 0.1% 66 ms 1,196 postgres
with operators as (
      select
        distinct on (u.id) u.id as user_id,
        s.id as session_id,
        c.id as counter_id,
        (case when c.override_user_services then cs.priority else $5 end) as counter_service_priority,
        us.priority as user_service_priority
      from users u
      join sessions s on s.user_id = u.id
      join counters c on c.id = s.counter_id
      join branch_sessions bs on bs.id = s.branch_session_id
      join counters_services cs on cs.counter_id = c.id and cs.service_id = $1
      left join users_services us on us.user_id = u.id and us.service_id = $2
      where u.space_id = $3
      and u.id not in ($6)
      and s.status = $7
      and s.branch_id = $4
      and s.counter_id is not null
      and bs.current = $8
      and not exists (
        select $9
        from tickets t
        where t.space_id = u.space_id
        and t.user_id = u.id
        and t.branch_session_id = s.branch_session_id
        and t.state in ($10)
        limit $11
      )
      and (
        select us.status
        from user_statuses us
        where us.user_id = u.id
        and us.space_id = u.space_id
        order by us.created_at desc
        limit $12
      ) = $13
    )
    select user_id, session_id, counter_id
    from operators
    order by counter_service_priority desc nulls last, user_service_priority asc
    limit $14
1 min < 0.1% 27 ms 2,185 postgres
select "incidents".* from "incidents" where "branch_id" = $1 and "space_id" = $2 order by "created_at" asc
1 min < 0.1% 232 ms 135 postgres
select u.*
    from users u
    cross join lateral (
        select max(cs.priority) as max_priority, count(*) as session_count
        from sessions s
        join branch_sessions bs on bs.id = s.branch_session_id
        join branches b on b.id = s.branch_id
        join counters c on c.id = s.counter_id
        join counters_services cs on cs.counter_id = c.id
        and cs.service_profile_id = $1
        where bs.current = $4
        and s.user_id = u.id
        and s.status = $5
        and s.counter_id is not null
        and (b.code is null or b.code != $6)
        
        
        and not exists (
          select $7
          from tickets t
          where t.space_id = $2
          and t.user_id = u.id
          and t.branch_session_id = s.branch_session_id 
          and t.state in ($8, $9)
          limit $10
        )
    ) priorities
    where u.space_id = $3
    
    and priorities.session_count > $11
    order by priorities.max_priority desc nulls last, random()