The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
Wiring a logs query in the Studio codebase
This covers writing analytics log SQL inside apps/studio so it is safe, routed
to the right endpoint, and consistent with the existing OTEL builders. Read this
when you are editing TypeScript that builds or runs a logs query, not when you are
just writing a query in the Logs Explorer UI.
Branded SQL: never concatenate user input
All analytics log SQL must be a SafeLogSqlFragment, built with the helpers in
apps/studio/data/logs/safe-analytics-sql.ts. Two things enforce this: the
sql parameter of executeAnalyticsSql (apps/studio/data/logs/execute-analytics-sql.ts)
is typed SafeLogSqlFragment, and an eslint no-restricted-syntax rule in
apps/studio/eslint.config.cjs blocks direct post()/get() calls to the
logs.all / logs.all.otel endpoints from any file other than
execute-analytics-sql.ts. The branding is what keeps interpolated values from
becoming injection. The key
exports:
safeSql\...`— a tagged template that only acceptsSafeLogSqlFragmentinterpolations. Plain strings (and Postgres-brandedSafeSqlFragment`) are rejected at compile time, so you cannot accidentally drop a raw value in.analyticsLiteral(value)— turns a string/number/boolean into a safely escaped literal fragment (single quotes and backslashes are escaped). Use it for every dynamic value, especially thesource.joinSqlFragments(fragments, separator)— joins already-safe fragments with a fixed structural separator (' and ',', ', etc.).keyword(value, allowed)— resolves a value against a compile-time allow-list of fragments (e.g. anAND/ORoperator). Returns the allow-listed fragment, never the raw input.quotedIdent(value)— backtick-quotes a dotted identifier path after validating each segment.
Compose with safeSql plus these helpers. The only way to run SQL that was not
built from them is the user-authored path: untrustedLogSql(text) marks editor
text as UntrustedLogSqlFragment (displayable, storable, never executable), and
acceptUntrustedLogsSql(fragment) promotes it to SafeLogSqlFragment. That
promotion is a security boundary — call it only from a run gesture (Run
button click, Cmd+Enter) or an approval-gated tool call (the AI notebook
tools). Never from render, useEffect, or any automatic path. The notebook
persist path also promotes cells because the writable notebook type requires
SafeLogSqlFragment; that is storage typing, not execution approval, and is
not precedent for promoting anywhere else.
import { analyticsLiteral, safeSql } from '@/data/logs/safe-analytics-sql'
const source = 'edge_logs'
const sql = safeSql`
select timestamp, event_message
from logs
where source = ${analyticsLiteral(source)}
order by timestamp desc
limit 100
`
Pick the endpoint and builder by flag
The ClickHouse path is gated by the otelLegacyLogs ConfigCat flag
(useFlag('otelLegacyLogs') from common — ConfigCat, not PostHog). Keep the BigQuery path working when
the flag is off. Two helpers in apps/studio/data/logs/logs-endpoint.ts express
the split:
logsAllEndpointUrl(useOtel)— returns thelogs.all.otelendpoint whenuseOtel, otherwise the legacylogs.allendpoint.pickLogsQueryBuilder(useOtel, otel, bq)— picks between an OTEL builder and a BigQuery builder while preserving the type.
const useOtel = useFlag('otelLegacyLogs')
const builder = pickLogsQueryBuilder(useOtel, genDefaultQueryOtel, genDefaultQuery)
const endpoint = logsAllEndpointUrl(useOtel)
// include { otel: useOtel } in the React Query key so the two paths cache separately
Run the fragment through executeAnalyticsSql (apps/studio/data/logs/execute-analytics-sql.ts)
against that endpoint:
import { executeAnalyticsSql } from '@/data/logs/execute-analytics-sql'
import { analyticsLiteral, quotedIdent, safeSql } from '@/data/logs/safe-analytics-sql'
// ✅ GOOD: every interpolation is sanitized.
const sql = safeSql`
SELECT timestamp, event_message
FROM ${quotedIdent(table)}
WHERE id = ${analyticsLiteral(id)}
`
await executeAnalyticsSql({
projectRef,
endpoint,
sql,
iso_timestamp_start,
iso_timestamp_end,
})
// 🛑 BAD: raw string interpolation. This fails to type-check at the
// executeAnalyticsSql boundary because the result is `string`, not
// `SafeLogSqlFragment`.
const sql = `SELECT * FROM ${table} WHERE id = '${id}'`
await executeAnalyticsSql({ projectRef, endpoint, sql, iso_timestamp_start, iso_timestamp_end })
Follow the existing OTEL builders
When you need a new query shape, mirror the generators in
apps/studio/components/interfaces/Settings/Logs/Logs.utils.otel.ts rather than
inventing a parallel style. They already encode the conventions:
genDefaultQueryOtel,genCountQueryOtel,genChartQueryOtel,genSingleLogQueryOtel— the row/count/chart/single-log builders. They select the real columns plus source-specificlog_attributes[...]lookups aliased to the leaf names the renderers expect.mapOtelPreviewRow,mapOtelSingleLogToLegacy,otelTimestampToMicros— the JS normalization layer.timestampmust end up a microsecond number for the pagination cursor and the renderers, so reuseotelTimestampToMicrosrather than parsing the ISO string yourself.
The map-key discovery hook (apps/studio/data/logs/otel-log-keys-query.ts,
fetchOtelLogKeys / useOtelLogKeysQuery) is the canonical example of a small,
correctly-branded OTEL query — read it before writing a new one.
Checklist
- Every dynamic value goes through
analyticsLiteral(or another sanitizer), never string concatenation. - The query filters by
sourceand includes aLIMIT. - Numeric
log_attributesvalues are wrapped intoInt32OrZero. - The endpoint and builder are chosen with
logsAllEndpointUrl/pickLogsQueryBuilderoffuseFlag('otelLegacyLogs'). - The React Query key distinguishes the OTEL and BigQuery paths.
-
timestampis normalized to micros for any row consumed by the table/cursor. - There is a unit test asserting the generated SQL string (see
Logs.utils.otel.test.tsandsafe-analytics-sql.test.tsfor the pattern).