Skip to content

Commit 625b4c8

Browse files
committed
sql/stats: ignore partial stats in crdb_internal.table_row_statistics
Previously, the `crdb_internal.table_row_statistics` virtual table was being populated with the row count from the most recent table statistic. This caused incorrect row counts to be shown for `SHOW TABLES` when the most recent statistic was a partial collection. This change ignores partial stats when populating `crdb_internal.table_row_statistics` to fix this issue. Fixes: #152024 Release note (bug fix): Previously, `SHOW TABLES` would show inaccurate row counts if the most recent statistic collection was partial, which is now fixed.
1 parent 97da885 commit 625b4c8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/sql/crdb_internal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ CREATE TABLE crdb_internal.table_row_statistics (
777777
SELECT DISTINCT ON ("tableID") "tableID", "rowCount"
778778
FROM system.table_statistics
779779
AS OF SYSTEM TIME '%s'
780+
WHERE "partialPredicate" IS NULL
780781
ORDER BY "tableID", "createdAt" DESC, "rowCount" DESC`,
781782
statsAsOfTimeClusterMode.String(&p.ExecCfg().Settings.SV))
782783
statRows, err := p.ExtendedEvalContext().ExecCfg.InternalDB.Executor().QueryBufferedEx(

pkg/sql/logictest/testdata/logic_test/table

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,3 +674,23 @@ pr_check CHECK ((price > 0)) true
674674

675675
statement ok
676676
COMMIT;
677+
678+
subtest end
679+
680+
# Test that partial statistics are filtered out from table_row_statistics
681+
statement ok
682+
CREATE TABLE t (k INT PRIMARY KEY)
683+
684+
statement ok
685+
INSERT INTO t SELECT generate_series(0, 9)
686+
687+
statement ok
688+
ANALYZE t
689+
690+
statement ok
691+
CREATE STATISTICS partial FROM t USING EXTREMES
692+
693+
query I
694+
SELECT estimated_row_count FROM crdb_internal.table_row_statistics WHERE table_name = 't'
695+
----
696+
10

0 commit comments

Comments
 (0)