Skip to content

Commit cc6cbd0

Browse files
committed
fix(driver/postgres): clamp reltuples -1 to 0 in inspect output
A table that has never been ANALYZEd reports reltuples = -1; surfacing that as a negative row estimate in 'siphon inspect' reads as a bug. GREATEST(...,0) clamps it to 0 (the conventional 'unknown/0' shown by pg tooling) while real counts pass through unchanged. Query structure (and the s. alias fix for the relname ambiguity) is otherwise unchanged — the 'simplify' half of the review note is intentionally not taken, to avoid regressing the just-fixed SQL.
1 parent 56aaa9a commit cc6cbd0

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

internal/driver/postgres/inspect.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
// inspectQuery lists user tables with row estimates and on-disk sizes.
1010
// schemaname/relname are qualified with the pg_stat_user_tables alias (s.)
1111
// because pg_class (c) also exposes a relname column — an unqualified
12-
// reference is ambiguous (SQLSTATE 42702).
12+
// reference is ambiguous (SQLSTATE 42702). reltuples is -1 for a table that
13+
// has never been ANALYZEd; GREATEST clamps that to 0 so the row estimate is
14+
// never reported as a negative number.
1315
const inspectQuery = `
1416
SELECT
1517
s.schemaname || '.' || s.relname AS name,
16-
COALESCE((SELECT reltuples::bigint FROM pg_class WHERE oid = c.oid), 0) AS rows,
18+
GREATEST(COALESCE((SELECT reltuples::bigint FROM pg_class WHERE oid = c.oid), 0), 0) AS rows,
1719
pg_total_relation_size(c.oid) AS size_bytes
1820
FROM pg_stat_user_tables s
1921
JOIN pg_class c ON c.oid = s.relid

0 commit comments

Comments
 (0)