The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
fix(studio): tighten time bounds on single-log inspection query (#47978)
David Whittington591b621567a5
diff --git a/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsx b/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsxindex 0404cc71aee..f2727a762ea 100644--- a/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsx+++ b/apps/studio/components/interfaces/UnifiedLogs/ServiceFlowPanel.tsx@@ -80,6 +80,8 @@ export function ServiceFlowPanel({const { logsMetadata } = useIsFeatureEnabled(['logs:metadata'])+ const timestampMs = getRowTimestampMs(selectedRow)+// Query the logs API directlyconst {data: serviceFlowData,@@ -91,13 +93,13 @@ export function ServiceFlowPanel({logId: selectedRow?.id,type: serviceFlowType,search: searchParameters,+ logTimestampMs: timestampMs,},{ enabled: Boolean(selectedRow?.id) && Boolean(serviceFlowType) })if (!selectedRowKey || !selectedRow) return null- const timestampMs = getRowTimestampMs(selectedRow)const formattedTime = timestampMs ? new Date(timestampMs).toLocaleString() : null// Prepare JSON data for Raw JSON tabdiff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.constants.tsx b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.constants.tsxindex 5e5493b7b23..084dce16c00 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.constants.tsx+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.constants.tsx@@ -33,6 +33,23 @@ type LogType = keyof typeof LOG_TYPES_LABELSexport const LOG_TYPES = Object.keys(LOG_TYPES_LABELS) as [LogType, ...LogType[]]export const DEFAULT_LOG_TYPES = ['postgres', 'edge'] as const+// ClickHouse `source` value for each unified log type. Single source of truth+// consumed by both the unified logs list query (LOG_TYPE_CONDITION in+// UnifiedLogs.queries.ts) and the single-log inspection point lookup+// (unified-log-inspection-query.ts).+export const LOG_TYPE_TO_SOURCE: Record<LogType, string> = {+ edge: 'edge_logs',+ postgrest: 'postgrest_logs',+ storage: 'storage_logs',+ postgres: 'postgres_logs',+ 'edge function': 'function_edge_logs',+ auth: 'auth_logs',+ realtime: 'realtime_logs',+ supavisor: 'supavisor_logs',+ pgbouncer: 'pgbouncer_logs',+ multigres: 'multigres_logs',+}+const parseAsSort = createParser({parse(queryValue: string) {const [id, desc] = queryValue.split(SORT_DELIMITER)diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.tsindex ed7c5385af9..7a51e9c0412 100644--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.queries.ts@@ -1,6 +1,6 @@import dayjs from 'dayjs'-import { DEFAULT_LOG_TYPES } from './UnifiedLogs.constants'+import { DEFAULT_LOG_TYPES, LOG_TYPE_TO_SOURCE } from './UnifiedLogs.constants'import {groupLogsFiltersByColumn,parseLogsFilterUrlParams,@@ -54,18 +54,12 @@ const HTTP_STATUS_EXPR: SafeLogSqlFragment = safeSql`if(source = 'auth_logs', lo* logs from postgREST / storage-api and are intentionally not part of unified* logs; the UI surfaces gateway HTTP traffic for those buckets.*/-const LOG_TYPE_CONDITION: Record<string, SafeLogSqlFragment> = {- edge: safeSql`source = 'edge_logs'`,- postgrest: safeSql`source = 'postgrest_logs'`,- storage: safeSql`source = 'storage_logs'`,- postgres: safeSql`source = 'postgres_logs'`,- 'edge function': safeSql`source = 'function_edge_logs'`,- auth: safeSql`source = 'auth_logs'`,- realtime: safeSql`source = 'realtime_logs'`,- supavisor: safeSql`source = 'supavisor_logs'`,- pgbouncer: safeSql`source = 'pgbouncer_logs'`,- multigres: safeSql`source = 'multigres_logs'`,-}+const LOG_TYPE_CONDITION: Record<string, SafeLogSqlFragment> = Object.fromEntries(+ Object.entries(LOG_TYPE_TO_SOURCE).map(([type, source]) => [+ type,+ safeSql`source = ${lit(source)}`,+ ])+)// Derived `log_type` column for SELECT / GROUP BY / countIf use.// WHEN source = 'edge_logs' AND ${ATTR.path} LIKE '%/rest/%' THEN 'postgrest'diff --git a/apps/studio/data/logs/unified-log-inspection-query.ts b/apps/studio/data/logs/unified-log-inspection-query.tsindex 1bac1578530..216a173fab2 100644--- a/apps/studio/data/logs/unified-log-inspection-query.ts+++ b/apps/studio/data/logs/unified-log-inspection-query.ts@@ -20,6 +20,7 @@ import {getPostgrestServiceFlowQuery,getStorageServiceFlowQuery,} from '@/components/interfaces/UnifiedLogs/Queries/ServiceFlowQueries/ServiceFlow.sql'+import { LOG_TYPE_TO_SOURCE } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.constants'import { QuerySearchParamsType } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'import type { ResponseError, UseCustomQueryOptions } from '@/types'@@ -34,12 +35,50 @@ export const SERVICE_FLOW_TYPES = [export type ServiceFlowType = (typeof SERVICE_FLOW_TYPES)[number]+// The logs table's primary key is (project, source, timestamp) — filtering on+// source narrows the sorted range ClickHouse has to scan before the timestamp+// bound even applies, so it matters for performance, not just correctness.+// Values are drawn from LOG_TYPE_TO_SOURCE (shared with LOG_TYPE_CONDITION in+// UnifiedLogs.queries.ts) so there's one source of truth for the type -> source+// mapping; only the key spelling differs ('edge-function' vs 'edge function').+const SERVICE_FLOW_TYPE_SOURCE: Record<ServiceFlowType, SafeLogSqlFragment> = {+ postgrest: lit(LOG_TYPE_TO_SOURCE.postgrest),+ auth: lit(LOG_TYPE_TO_SOURCE.auth),+ 'edge-function': lit(LOG_TYPE_TO_SOURCE['edge function']),+ storage: lit(LOG_TYPE_TO_SOURCE.storage),+ postgres: lit(LOG_TYPE_TO_SOURCE.postgres),+}+export type UnifiedLogInspectionVariables = {projectRef?: stringlogId?: stringtype?: ServiceFlowTypesearch: QuerySearchParamsTypeuseOtel?: boolean+ /**+ * The selected row's own timestamp (ms since epoch), when known. A single log+ * is always looked up by `id`, so we can bound the query tightly around this+ * timestamp instead of the (potentially much wider) selected search range.+ */+ logTimestampMs?: number | null+}++// The row's timestamp is the exact stored value (parsed from the same+// `timestamp` column), not an approximate clock reading, so this only needs to+// absorb millisecond-vs-microsecond rounding — not real clock skew.+const INSPECTION_WINDOW_MS = 60 * 1000++function getInspectionISOStartEnd(+ search: QuerySearchParamsType,+ logTimestampMs: number | null | undefined+) {+ if (typeof logTimestampMs === 'number' && Number.isFinite(logTimestampMs)) {+ return {+ isoTimestampStart: new Date(logTimestampMs - INSPECTION_WINDOW_MS).toISOString(),+ isoTimestampEnd: new Date(logTimestampMs + INSPECTION_WINDOW_MS).toISOString(),+ }+ }+ return getUnifiedLogsISOStartEnd(search)}export type UnifiedLogInspectionResponse = {@@ -124,7 +163,14 @@ export interface UnifiedLogInspectionEntry {}export async function getUnifiedLogInspection(- { projectRef, logId, type, search, useOtel = false }: UnifiedLogInspectionVariables,+ {+ projectRef,+ logId,+ type,+ search,+ useOtel = false,+ logTimestampMs,+ }: UnifiedLogInspectionVariables,signal?: AbortSignal) {if (!projectRef) {@@ -137,7 +183,7 @@ export async function getUnifiedLogInspection(throw new Error('type is required')}- const { isoTimestampStart, isoTimestampEnd } = getUnifiedLogsISOStartEnd(search)+ const { isoTimestampStart, isoTimestampEnd } = getInspectionISOStartEnd(search, logTimestampMs)if (!useOtel) {let sql: SafeLogSqlFragment@@ -189,7 +235,7 @@ export async function getUnifiedLogInspection(const sql = safeSql`-- unified logs: inspect single log by idSELECT id, timestamp, source, event_message, severity_text, log_attributesFROM logs-WHERE id = ${lit(logId)}+WHERE id = ${lit(logId)} AND source = ${SERVICE_FLOW_TYPE_SOURCE[type]}LIMIT 1`@@ -249,7 +295,7 @@ export type UnifiedLogInspectionData = Awaited<ReturnType<typeof getUnifiedLogInexport type UnifiedLogInspectionError = ResponseErrorexport const useUnifiedLogInspectionQuery = <TData = UnifiedLogInspectionData>(- { projectRef, logId, type, search }: UnifiedLogInspectionVariables,+ { projectRef, logId, type, search, logTimestampMs }: UnifiedLogInspectionVariables,{enabled = true,...options@@ -259,7 +305,7 @@ export const useUnifiedLogInspectionQuery = <TData = UnifiedLogInspectionData>(return useQuery<UnifiedLogInspectionData, UnifiedLogInspectionError, TData>({queryKey: [...logsKeys.serviceFlow(projectRef, search, logId), { otel: useOtel }],queryFn: ({ signal }) =>- getUnifiedLogInspection({ projectRef, logId, type, search, useOtel }, signal),+ getUnifiedLogInspection({ projectRef, logId, type, search, useOtel, logTimestampMs }, signal),enabled: enabled && typeof projectRef !== 'undefined',...UNIFIED_LOGS_QUERY_OPTIONS,...options,