The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Reinstate default sort but limited to within threshold row count (#28503)
Joshen Lim111a9d51a71b
diff --git a/apps/studio/components/grid/SupabaseGrid.utils.ts b/apps/studio/components/grid/SupabaseGrid.utils.tsindex 1dcc9a2867d..1e6a6e8c5be 100644--- a/apps/studio/components/grid/SupabaseGrid.utils.ts+++ b/apps/studio/components/grid/SupabaseGrid.utils.ts@@ -120,6 +120,7 @@ export function parseSupaTable(comment: table.comment,schema: table.schema,columns: supaColumns,+ estimateRowCount: table.live_rows_estimate,}}diff --git a/apps/studio/components/grid/components/grid/Grid.tsx b/apps/studio/components/grid/components/grid/Grid.tsxindex 447ed7e731e..36ff6ac60aa 100644--- a/apps/studio/components/grid/components/grid/Grid.tsx+++ b/apps/studio/components/grid/components/grid/Grid.tsx@@ -11,7 +11,7 @@ import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'import { useForeignKeyConstraintsQuery } from 'data/database/foreign-key-constraints-query'import { useUrlState } from 'hooks/ui/useUrlState'import { copyToClipboard } from 'lib/helpers'-import { Button } from 'ui'+import { Button, cn } from 'ui'import { useDispatch, useTrackedState } from '../../store/Store'import type { Filter, GridProps, SupaRow } from '../../types'import { useKeyboardShortcuts } from '../common/Hooks'@@ -170,7 +170,7 @@ export const Grid = memo(return (<div- className={`${containerClass} flex flex-col`}+ className={cn(`flex flex-col`, containerClass)}style={{ width: width || '100%', height: height || '50vh' }}><DataGriddiff --git a/apps/studio/components/grid/types/table.ts b/apps/studio/components/grid/types/table.tsindex 54853778886..8af19c6530b 100644--- a/apps/studio/components/grid/types/table.ts+++ b/apps/studio/components/grid/types/table.ts@@ -24,6 +24,7 @@ export interface SupaTable {readonly name: stringreadonly schema?: string | nullreadonly comment?: string | null+ readonly estimateRowCount: number}export interface SupaRow extends Dictionary<any> {diff --git a/apps/studio/data/table-rows/table-rows-query.ts b/apps/studio/data/table-rows/table-rows-query.tsindex ee0c84c4580..366c2ab7689 100644--- a/apps/studio/data/table-rows/table-rows-query.ts+++ b/apps/studio/data/table-rows/table-rows-query.ts@@ -22,6 +22,7 @@ import {} from '../sql/execute-sql-query'import { getPagination } from '../utils/pagination'import { formatFilterValue } from './utils'+import { THRESHOLD_COUNT } from './table-rows-count-query'type GetTableRowsArgs = {table?: SupaTable@@ -73,26 +74,17 @@ export const fetchAllTableRows = async ({queryChains = queryChains.filter(filter.column, filter.operator, value)})- /**- * [Joshen] JFYI 190724 We've commented the default sort logic out below and in getTableRowsSqlQuery- * as the default sort causes resource issues in particular for very large tables (e.g 200M rows)- * The long term fix here should be to only apply default sort if table has < 50k rows- * - this is the same threshold that we do for retrieving the exact count on the table (ref table-rows-count-query)- * - we can check the estimate row count when we get the table details via pg-meta under the live_rows_estimate property- * - i.e the larger the table, the less hand holding the table editor does (which i think is fair from a scalability pov)- */-- // If sorts is empty, use the primary key as the default sort- // if (sorts.length === 0) {- // const primaryKey = getDefaultOrderByColumn(table)- // if (primaryKey) {- // queryChains = queryChains.order(table.name, primaryKey, true, true)- // }- // } else {- sorts.forEach((sort) => {- queryChains = queryChains.order(sort.table, sort.column, sort.ascending, sort.nullsFirst)- })- // }+ // If sorts is empty and table row count is within threshold, use the primary key as the default sort+ if (sorts.length === 0 && table.estimateRowCount <= THRESHOLD_COUNT) {+ const primaryKey = getDefaultOrderByColumn(table)+ if (primaryKey) {+ queryChains = queryChains.order(table.name, primaryKey, true, true)+ }+ } else {+ sorts.forEach((sort) => {+ queryChains = queryChains.order(sort.table, sort.column, sort.ascending, sort.nullsFirst)+ })+ }// Starting from page 0, fetch 500 records per calllet page = -1@@ -152,18 +144,17 @@ export const getTableRowsSqlQuery = ({queryChains = queryChains.filter(x.column, x.operator, value)})- // If sorts is empty, use the primary key as the default sort- // if (sorts.length === 0) {- // const defaultOrderByColumn = getDefaultOrderByColumn(table)-- // if (defaultOrderByColumn) {- // queryChains = queryChains.order(table.name, defaultOrderByColumn, true, true)- // }- // } else {- sorts.forEach((x) => {- queryChains = queryChains.order(x.table, x.column, x.ascending, x.nullsFirst)- })- // }+ // If sorts is empty and table row count is within threshold, use the primary key as the default sort+ if (sorts.length === 0 && table.estimateRowCount <= THRESHOLD_COUNT) {+ const defaultOrderByColumn = getDefaultOrderByColumn(table)+ if (defaultOrderByColumn) {+ queryChains = queryChains.order(table.name, defaultOrderByColumn, true, true)+ }+ } else {+ sorts.forEach((x) => {+ queryChains = queryChains.order(x.table, x.column, x.ascending, x.nullsFirst)+ })+ }// getPagination is expecting to start from 0const { from, to } = getPagination((page ?? 1) - 1, limit)@@ -185,9 +176,7 @@ export const getTableRowsSqlQuery = ({return outputSql}-export type TableRows = {- rows: SupaRow[]-}+export type TableRows = { rows: SupaRow[] }export type TableRowsVariables = GetTableRowsArgs & {projectRef?: string