From 30b57d18dad8f1a1e8c065af2217fb2ce9c3dd72 Mon Sep 17 00:00:00 2001 From: Ryan Slade Date: Fri, 3 Jan 2025 14:00:25 +0100 Subject: [PATCH] Include schema name when fetching estimate --- pkg/migrations/backfill.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/migrations/backfill.go b/pkg/migrations/backfill.go index b094f022..19be1eef 100644 --- a/pkg/migrations/backfill.go +++ b/pkg/migrations/backfill.go @@ -65,10 +65,15 @@ func Backfill(ctx context.Context, conn db.DB, table *schema.Table, batchSize in func getRowCount(ctx context.Context, conn db.DB, tableName string) (int64, error) { var total int64 // Try and get estimated row count - row := conn.QueryRowContext(ctx, ` + var currentSchema string + row := conn.QueryRowContext(ctx, "select current_schema()") + if err := row.Scan(¤tSchema); err != nil { + return 0, fmt.Errorf("getting current schema: %w", err) + } + row = conn.QueryRowContext(ctx, ` SELECT n_live_tup AS estimate FROM pg_stat_user_tables - WHERE relname = $1`, tableName) + WHERE schemaname = $1 AND relname = $2`, currentSchema, tableName) if err := row.Scan(&total); err != nil { return 0, fmt.Errorf("scanning row count estimate for %q: %w", tableName, err) }