Skip to content

Commit

Permalink
Support estimated count with multiple schemas (#22276)
Browse files Browse the repository at this point in the history
The `EstimateCount` could be incorrect when the table lives in multiple
schemas. Related to #19775.
  • Loading branch information
wolfogre committed Dec 30, 2022
1 parent cf07f24 commit e5deeda
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion models/db/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ func EstimateCount(ctx context.Context, bean interface{}) (int64, error) {
case schemas.MYSQL:
_, err = e.Context(ctx).SQL("SELECT table_rows FROM information_schema.tables WHERE tables.table_name = ? AND tables.table_schema = ?;", tablename, x.Dialect().URI().DBName).Get(&rows)
case schemas.POSTGRES:
_, err = e.Context(ctx).SQL("SELECT reltuples AS estimate FROM pg_class WHERE relname = ?;", tablename).Get(&rows)
// the table can live in multiple schemas of a postgres database
// See https://wiki.postgresql.org/wiki/Count_estimate
tablename = x.TableName(bean, true)
_, err = e.Context(ctx).SQL("SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = ?::regclass;", tablename).Get(&rows)
case schemas.MSSQL:
_, err = e.Context(ctx).SQL("sp_spaceused ?;", tablename).Get(&rows)
default:
Expand Down

0 comments on commit e5deeda

Please sign in to comment.