@@ -2,7 +2,8 @@ import { isDeepStrictEqual } from 'node:util'
22import { AuditAction , AuditResourceType } from '@sim/audit'
33import { type Principal , resolvePrincipalAttribution } from '@sim/auth/principal'
44import { db } from '@sim/db'
5- import { getRequestContext } from '@sim/logger'
5+ import { createLogger , getRequestContext } from '@sim/logger'
6+ import { getPostgresCancellationReason } from '@sim/utils/errors'
67import { generateId } from '@sim/utils/id'
78import { isPlainRecord , toRecord } from '@sim/utils/object'
89import {
@@ -77,6 +78,7 @@ import { columnTypeOf } from '@/lib/table/column-types'
7778import { TableQueryValidationError } from '@/lib/table/errors'
7879import { signalTableRowsChanged , signalTableRowsChangedByActor } from '@/lib/table/events'
7980import { CSV_MAX_BATCH_SIZE } from '@/lib/table/import'
81+ import { READ_STATEMENT_TIMEOUT_MS } from '@/lib/table/planner'
8082import { isTablePredicate , predicateToFilter } from '@/lib/table/query-builder/converters'
8183import {
8284 validatePredicate ,
@@ -98,6 +100,8 @@ import { predicateToStorage, resolveFilterSelectValues } from '@/lib/table/selec
98100import { coerceRowValues } from '@/lib/table/validation'
99101import type { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
100102
103+ const logger = createLogger ( 'TableRowsApplication' )
104+
101105const ROW_CHANGE_COUNTERS = [ 'rowsInserted' , 'rowsUpdated' , 'rowsDeleted' ] as const
102106
103107/**
@@ -501,6 +505,25 @@ async function throwValidationResponse(
501505 throw new TableRowsValidationError ( body . error ?? 'Invalid row data' , body . details )
502506}
503507
508+ /**
509+ * A structured row query cancelled by its own `statement_timeout`: the query shape costs more than
510+ * the table's size allows. Sorting or range-filtering on a stored column reads every row's data, so it is the
511+ * caller's to narrow, and retrying the same query only times out again.
512+ */
513+ function rethrowReadQueryError ( error : unknown , tableId : string ) : never {
514+ if ( getPostgresCancellationReason ( error ) === 'statement_timeout' ) {
515+ logger . warn ( 'Table read hit its statement timeout' , { tableId } )
516+ throw new TableRowsValidationError (
517+ `The query took longer than ${ READ_STATEMENT_TIMEOUT_MS / 1000 } seconds and was stopped. ` +
518+ 'Sorting or range-filtering on a column reads every row in the table, which is too slow ' +
519+ 'at this table size. Sort by createdAt, add an equality filter, or keep ' +
520+ 'fewer rows (an Expiration column deletes old rows automatically).' ,
521+ { code : 'TABLE_QUERY_TIMEOUT' }
522+ )
523+ }
524+ rethrowQueryValidation ( error )
525+ }
526+
504527function rethrowQueryValidation ( error : unknown ) : never {
505528 if ( error instanceof TableQueryValidationError ) {
506529 throw new TableRowsValidationError ( error . message , error . code ? { code : error . code } : undefined )
@@ -680,7 +703,7 @@ export const queryTableRows = defineAuthorizedTableUseCase({
680703 : { } ) ,
681704 }
682705 } catch ( error ) {
683- rethrowQueryValidation ( error )
706+ rethrowReadQueryError ( error , context . table . id )
684707 }
685708 } ,
686709} )
0 commit comments