Files
supabase/apps/studio/data/read-replicas/replica-lag-query.ts
Alaister YoungandJoshen Lim 3a27070dc2 chore(perf): table editor query optimisation (#30184)
* chore: table editor query optimisation

* removed unused queries and fix invalidations

* address feedback

* fix filtering for foreign tables

* Update

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-10-31 15:20:40 +08:00

45 lines
1.3 KiB
TypeScript

import { UseQueryOptions } from '@tanstack/react-query'
import { ExecuteSqlData, ExecuteSqlError, useExecuteSqlQuery } from '../sql/execute-sql-query'
export const replicationLagQuery = () => {
const sql = /* SQL */ `
select
case
when (select count(*) from pg_stat_wal_receiver) = 1 and pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn()
then 0
else coalesce(extract(epoch from now() - pg_last_xact_replay_timestamp()),0)
end as physical_replica_lag_second
`
return sql
}
export type ReplicationLagVariables = {
id: string
projectRef?: string
connectionString?: string
}
export type ReplicationLagData = number
export type ReplicationLagError = ExecuteSqlError
export const useReplicationLagQuery = <TData extends ReplicationLagData = ReplicationLagData>(
{ projectRef, connectionString, id }: ReplicationLagVariables,
{ enabled = true, ...options }: UseQueryOptions<ExecuteSqlData, ReplicationLagError, TData> = {}
) =>
useExecuteSqlQuery(
{
projectRef,
connectionString,
sql: replicationLagQuery(),
queryKey: ['replica-lag', id],
},
{
select(data) {
return Number((data.result[0] ?? null)?.physical_replica_lag_second ?? 0) as TData
},
enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined',
...options,
}
)