diff options
author | Jason Song <i@wolfogre.com> | 2022-12-31 02:25:58 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-30 12:25:58 -0600 |
commit | e5deeda0aa2901014827aea16f5212878ff80208 (patch) | |
tree | 0b268e49eda4b127f156d170015198cb2dbc6925 /models | |
parent | cf07f247b775ce285902358c0ff0a26be0806406 (diff) | |
download | gitea-e5deeda0aa2901014827aea16f5212878ff80208.tar.gz gitea-e5deeda0aa2901014827aea16f5212878ff80208.zip |
Support estimated count with multiple schemas (#22276)
The `EstimateCount` could be incorrect when the table lives in multiple
schemas. Related to #19775.
Diffstat (limited to 'models')
-rw-r--r-- | models/db/context.go | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/models/db/context.go b/models/db/context.go index c8ad0c1aa2..3db8b16528 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -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: |