Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix duplicates in export (#39532)

Jordi Enricdb9ac2f5d882
diff --git a/apps/studio/data/table-rows/table-rows-query.ts b/apps/studio/data/table-rows/table-rows-query.ts
index 43798337c51..6bd944e07b9 100644
--- a/apps/studio/data/table-rows/table-rows-query.ts
+++ b/apps/studio/data/table-rows/table-rows-query.ts
@@ -98,9 +98,9 @@ export const getAllTableRowsSql = ({
queryChains = queryChains.filter(filter.column, filter.operator, value)
})
- // 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 primaryKeys = getDefaultOrderByColumns(table)
+ // Always enforce deterministic ordering for pagination/export
+ const primaryKeys = getDefaultOrderByColumns(table)
+ if (sorts.length === 0) {
if (primaryKeys.length > 0) {
primaryKeys.forEach((col) => {
queryChains = queryChains.order(table.name, col)
@@ -110,8 +110,23 @@ export const getAllTableRowsSql = ({
sorts.forEach((sort) => {
queryChains = queryChains.order(sort.table, sort.column, sort.ascending, sort.nullsFirst)
})
+
+ // Add primary keys as tie-breakers so page order doesn't shuffle
+ if (primaryKeys.length > 0) {
+ const sortedColumns = new Set(
+ sorts.filter((s) => s.table === table.name).map((s) => s.column)
+ )
+ primaryKeys
+ .filter((pk) => !sortedColumns.has(pk))
+ .forEach((pk) => {
+ queryChains = queryChains.order(table.name, pk)
+ })
+ }
}
+ // Final tie-breaker: use system column ctid to guarantee a stable, unique order
+ queryChains = queryChains.order(table.name, 'ctid')
+
return queryChains
}