fix(objects): use live PG row estimates and refresh stale stats
This commit is contained in:
3 files changed
+219
-13
No files matched your search
@@ -3140,6 +3140,9 @@ async function loadObjects(options?: { allowCached?: boolean; preserveExistingRo
|
|||||||
// Explicit refresh and metadata invalidation still bypass this branch.
|
// Explicit refresh and metadata invalidation still bypass this branch.
|
||||||
applyObjectBrowserRows(cached.rows);
|
applyObjectBrowserRows(cached.rows);
|
||||||
finishOnce();
|
finishOnce();
|
||||||
|
// 行数/大小是随对象列表一起缓存的,直接返回缓存会让这两列一直停在上一次统计
|
||||||
|
// (#10461)。对象列表本身仍按上面的约定不动,只把统计信息在后台补一次。
|
||||||
|
if (cached.stale) void revalidateCachedObjectStatistics(request, cacheWriteToken);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// No scaffold: first load in this scope, cache invalidated by a DDL mutation,
|
// No scaffold: first load in this scope, cache invalidated by a DDL mutation,
|
||||||
@@ -3205,6 +3208,15 @@ async function loadObjectStatistics(request: ObjectBrowserRowsLoadHandle, cacheW
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refreshes only the table statistics for rows restored from a stale cache
|
||||||
|
* scaffold. The row list itself is intentionally left untouched, and failures
|
||||||
|
* stay silent because the statistics are decorative.
|
||||||
|
*/
|
||||||
|
async function revalidateCachedObjectStatistics(request: ObjectBrowserRowsLoadHandle, cacheWriteToken: ObjectBrowserRowsCacheWriteToken) {
|
||||||
|
await loadObjectStatistics(request, cacheWriteToken, undefined);
|
||||||
|
}
|
||||||
|
|
||||||
function mergeObjectStatistics(stats: ObjectStatistics[], fallbackSchema: string, cacheWriteToken: ObjectBrowserRowsCacheWriteToken, cachedAt: number | undefined) {
|
function mergeObjectStatistics(stats: ObjectStatistics[], fallbackSchema: string, cacheWriteToken: ObjectBrowserRowsCacheWriteToken, cachedAt: number | undefined) {
|
||||||
const statsByKey = new Map(stats.map((stat) => [objectStatisticKey(stat.schema || fallbackSchema, stat.name), stat]));
|
const statsByKey = new Map(stats.map((stat) => [objectStatisticKey(stat.schema || fallbackSchema, stat.name), stat]));
|
||||||
rows.value = rows.value.map((row) => {
|
rows.value = rows.value.map((row) => {
|
||||||
|
|||||||
Vendored
+151
@@ -0,0 +1,151 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
|
||||||
|
import { createApp, defineComponent, h, ref, type App } from "vue";
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import ObjectBrowser from "@/components/objects/ObjectBrowser.vue";
|
||||||
|
import { cacheObjectBrowserRows, createObjectBrowserRowsCacheWriteToken, invalidateObjectBrowserRowsCache } from "@/lib/table/objectBrowserRowsCache";
|
||||||
|
import type { ObjectBrowserRow } from "@/lib/table/objectBrowserRows";
|
||||||
|
import type { ConnectionConfig } from "@/types/database";
|
||||||
|
|
||||||
|
const mocks = vi.hoisted(() => ({
|
||||||
|
listObjects: vi.fn(),
|
||||||
|
listSchemas: vi.fn(),
|
||||||
|
listObjectStatistics: vi.fn(),
|
||||||
|
ensureConnected: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@/lib/backend/api", () => ({
|
||||||
|
listObjects: (...args: unknown[]) => mocks.listObjects(...args),
|
||||||
|
listSchemas: (...args: unknown[]) => mocks.listSchemas(...args),
|
||||||
|
listObjectStatistics: (...args: unknown[]) => mocks.listObjectStatistics(...args),
|
||||||
|
}));
|
||||||
|
vi.mock("@/stores/connectionStore", () => ({
|
||||||
|
useConnectionStore: () => ({
|
||||||
|
getConfig: () => connection,
|
||||||
|
ensureConnected: mocks.ensureConnected,
|
||||||
|
orderByPinnedTreeNodes: (rows: unknown[]) => rows,
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => ({}) }));
|
||||||
|
vi.mock("@/stores/settingsStore", () => ({
|
||||||
|
useSettingsStore: () => ({
|
||||||
|
editorSettings: {
|
||||||
|
shortcuts: { refreshData: "F5" },
|
||||||
|
objectBrowserViewMode: "list",
|
||||||
|
objectBrowserShowCheckbox: false,
|
||||||
|
sidebarCopyTableNameSeparator: "newline",
|
||||||
|
sidebarCopyTableNameIncludeSchema: false,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key, locale: ref("en-US") }) }));
|
||||||
|
vi.mock("@/i18n", () => ({ default: { install: () => undefined } }));
|
||||||
|
vi.mock("@/composables/useSqlHighlighter", () => ({ useSqlHighlighter: () => ({ highlight: (sql: string) => sql }) }));
|
||||||
|
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
|
||||||
|
vi.mock("vue-virtual-scroller", () => ({
|
||||||
|
RecycleScroller: defineComponent({
|
||||||
|
props: { items: { type: Array, default: () => [] } },
|
||||||
|
setup(props, { slots }) {
|
||||||
|
return () =>
|
||||||
|
h(
|
||||||
|
"div",
|
||||||
|
props.items.map((item) => slots.default?.({ item })),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
vi.mock("@/components/ui/searchable-select", () => ({ SearchableSelect: { render: () => null } }));
|
||||||
|
vi.mock("@/components/ui/ToolbarOverflowMenu.vue", () => ({ default: { render: () => null } }));
|
||||||
|
vi.mock("@/components/ui/CustomContextMenu.vue", () => ({
|
||||||
|
default: defineComponent({
|
||||||
|
setup(_, { slots }) {
|
||||||
|
return () => slots.default?.({ onContextMenu: () => undefined, isOpen: false });
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
vi.mock("@/components/editor/QueryEditor.vue", () => ({ default: { render: () => null } }));
|
||||||
|
vi.mock("@/components/editor/DangerConfirmDialog.vue", () => ({ default: { render: () => null } }));
|
||||||
|
vi.mock("@/components/objects/ProcedureExecutionDialog.vue", () => ({ default: { render: () => null } }));
|
||||||
|
vi.mock("@/components/objects/CustomTypeInfoPanel.vue", () => ({ default: { render: () => null } }));
|
||||||
|
vi.mock("@/components/export/XlsxHeaderDialog.vue", () => ({ default: { render: () => null } }));
|
||||||
|
|
||||||
|
const connection = {
|
||||||
|
id: "pg-stale-statistics",
|
||||||
|
name: "Postgres",
|
||||||
|
db_type: "postgres",
|
||||||
|
database: "pgdemo",
|
||||||
|
driver_profile: null,
|
||||||
|
url_params: null,
|
||||||
|
transport_layers: [],
|
||||||
|
} as unknown as ConnectionConfig;
|
||||||
|
|
||||||
|
const scope = { connectionId: connection.id, database: "pgdemo", schema: "public" };
|
||||||
|
const cachedRows: ObjectBrowserRow[] = [
|
||||||
|
{
|
||||||
|
id: "table:orders",
|
||||||
|
name: "orders",
|
||||||
|
displayName: "orders",
|
||||||
|
type: "TABLE",
|
||||||
|
schema: "public",
|
||||||
|
estimatedRows: 0,
|
||||||
|
totalBytes: 24_576,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mocks.listObjects.mockResolvedValue([{ name: "orders", object_type: "TABLE" }]);
|
||||||
|
mocks.listSchemas.mockResolvedValue(["public"]);
|
||||||
|
mocks.ensureConnected.mockResolvedValue(undefined);
|
||||||
|
mocks.listObjectStatistics.mockResolvedValue([{ name: "orders", schema: "public", estimated_rows: 901, total_bytes: 196_608 }]);
|
||||||
|
invalidateObjectBrowserRowsCache({});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const { app, host } of mountedApps.splice(0)) {
|
||||||
|
app.unmount();
|
||||||
|
host.remove();
|
||||||
|
}
|
||||||
|
invalidateObjectBrowserRowsCache({});
|
||||||
|
});
|
||||||
|
|
||||||
|
async function mountBrowser() {
|
||||||
|
const host = document.createElement("div");
|
||||||
|
document.body.append(host);
|
||||||
|
const app = createApp({ setup: () => () => h(ObjectBrowser, { connection, database: "pgdemo", schema: "public" }) });
|
||||||
|
mountedApps.push({ app, host });
|
||||||
|
app.mount(host);
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
function primeRowsCache(cachedAt: number) {
|
||||||
|
cacheObjectBrowserRows(createObjectBrowserRowsCacheWriteToken(scope), cachedRows, { cachedAt });
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("ObjectBrowser table statistics on a cached scaffold (#10461)", () => {
|
||||||
|
it("backfills row/size statistics when the restored scaffold is stale", async () => {
|
||||||
|
primeRowsCache(Date.now() - 31_000);
|
||||||
|
|
||||||
|
const host = await mountBrowser();
|
||||||
|
|
||||||
|
// 行数/大小 随对象列表一起缓存,陈旧条目必须在后台补一次统计。
|
||||||
|
await vi.waitFor(() => expect(mocks.listObjectStatistics).toHaveBeenCalledWith("pg-stale-statistics", "pgdemo", "public"));
|
||||||
|
await vi.waitFor(() => expect(host.textContent).toContain("901"));
|
||||||
|
expect(host.textContent).toContain("192 KB");
|
||||||
|
// 陈旧缓存仍然直接渲染,且不重新拉对象列表。
|
||||||
|
expect(mocks.listObjects).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves a fresh scaffold alone so tab switches stay quiet", async () => {
|
||||||
|
primeRowsCache(Date.now());
|
||||||
|
|
||||||
|
const host = await mountBrowser();
|
||||||
|
|
||||||
|
await vi.waitFor(() => expect(host.textContent).toContain("orders"));
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 30));
|
||||||
|
expect(mocks.listObjectStatistics).not.toHaveBeenCalled();
|
||||||
|
expect(mocks.listObjects).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -7343,22 +7343,47 @@ pub async fn get_custom_type_details(pool: &Pool, schema: &str, name: &str) -> R
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Row/size estimates for the object browser, per schema.
|
||||||
|
///
|
||||||
|
/// `pg_class.reltuples` only moves when ANALYZE (or autovacuum) rewrites it, so a
|
||||||
|
/// table that was written to — or never analyzed at all — keeps reporting its
|
||||||
|
/// last known count, often `0` (#10461). `pg_stat_user_tables.n_live_tup` is the
|
||||||
|
/// statistics collector's live estimate: it tracks DML within seconds and still
|
||||||
|
/// avoids a `COUNT(*)` scan, which is what the UI promises in its column hint.
|
||||||
|
const POSTGRES_OBJECT_STATISTICS_SQL: &str = "SELECT c.relname, \
|
||||||
|
GREATEST(COALESCE(s.n_live_tup, c.reltuples), 0)::bigint AS estimated_rows, \
|
||||||
|
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
|
||||||
|
FROM pg_catalog.pg_class c \
|
||||||
|
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||||
|
LEFT JOIN pg_catalog.pg_stat_user_tables s ON s.relid = c.oid \
|
||||||
|
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
|
||||||
|
ORDER BY c.relname";
|
||||||
|
|
||||||
|
/// Some Postgres-compatible engines do not expose `pg_stat_user_tables`; fall
|
||||||
|
/// back to the ANALYZE-time estimate rather than dropping the columns entirely.
|
||||||
|
const POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL: &str = "SELECT c.relname, \
|
||||||
|
GREATEST(c.reltuples, 0)::bigint AS estimated_rows, \
|
||||||
|
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
|
||||||
|
FROM pg_catalog.pg_class c \
|
||||||
|
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||||
|
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
|
||||||
|
ORDER BY c.relname";
|
||||||
|
|
||||||
pub async fn list_object_statistics(pool: &Pool, schema: &str) -> Result<Vec<ObjectStatistics>, String> {
|
pub async fn list_object_statistics(pool: &Pool, schema: &str) -> Result<Vec<ObjectStatistics>, String> {
|
||||||
let schema = if schema.is_empty() { "public" } else { schema };
|
let schema = if schema.is_empty() { "public" } else { schema };
|
||||||
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
|
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
|
||||||
let rows = postgres_query_cached(
|
let rows = match postgres_query_cached(&client, POSTGRES_OBJECT_STATISTICS_SQL, &[&schema]).await {
|
||||||
&client,
|
Ok(rows) => rows,
|
||||||
"SELECT c.relname, \
|
Err(error) => {
|
||||||
GREATEST(c.reltuples, 0)::bigint AS estimated_rows, \
|
log::warn!(
|
||||||
pg_catalog.pg_total_relation_size(c.oid)::bigint AS total_bytes \
|
"[postgres][object-statistics] live tuple estimate unavailable, falling back to reltuples: {}",
|
||||||
FROM pg_catalog.pg_class c \
|
pg_error_to_string(error)
|
||||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
);
|
||||||
WHERE n.nspname = $1 AND c.relkind IN ('r','m','f','p') \
|
postgres_query_cached(&client, POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL, &[&schema])
|
||||||
ORDER BY c.relname",
|
.await
|
||||||
&[&schema],
|
.map_err(|e| e.to_string())?
|
||||||
)
|
}
|
||||||
.await
|
};
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
Ok(rows
|
Ok(rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| ObjectStatistics {
|
.map(|row| ObjectStatistics {
|
||||||
@@ -15594,4 +15619,22 @@ mod tests {
|
|||||||
// `IN` must not match the start of `INTO`.
|
// `IN` must not match the start of `INTO`.
|
||||||
assert_eq!(parse_pg_partition_bound("FOR VALUES INTO (1)"), None);
|
assert_eq!(parse_pg_partition_bound("FOR VALUES INTO (1)"), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn object_statistics_prefers_live_tuples_and_keeps_a_reltuples_fallback() {
|
||||||
|
// #10461: `reltuples` lags behind DML, so the live estimate must win when
|
||||||
|
// `pg_stat_user_tables` is available...
|
||||||
|
assert!(POSTGRES_OBJECT_STATISTICS_SQL.contains("pg_catalog.pg_stat_user_tables"));
|
||||||
|
assert!(POSTGRES_OBJECT_STATISTICS_SQL.contains("COALESCE(s.n_live_tup, c.reltuples)"));
|
||||||
|
// ...while an engine without that view still reports counts.
|
||||||
|
assert!(!POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL.contains("pg_stat_user_tables"));
|
||||||
|
assert!(POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL.contains("GREATEST(c.reltuples, 0)"));
|
||||||
|
for sql in [POSTGRES_OBJECT_STATISTICS_SQL, POSTGRES_OBJECT_STATISTICS_FALLBACK_SQL] {
|
||||||
|
// Both queries must keep the same projection so the row mapping stays valid.
|
||||||
|
assert!(sql.contains("AS estimated_rows"));
|
||||||
|
assert!(sql.contains("AS total_bytes"));
|
||||||
|
assert!(sql.contains("c.relkind IN ('r','m','f','p')"));
|
||||||
|
assert!(sql.contains("WHERE n.nspname = $1"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user