UNPKG

@clickup/pg-microsharding

Version:
34 lines 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRowCount = getRowCount; const quote_1 = require("./quote"); const runSql_1 = require("./runSql"); /** * Returns an estimate for the number of rows in the table. * - Method "explain" is good for the source tables (fromDsn). It relies on the * ANALYZE work and is precise enough for the table which exists for a while. * It's not good for the COPYing table, since EXPLAIN severely under-estimates * the number of rows copied so far. * - Method "pg_stat_progress_copy" is good while a COPY query is running. It is * very precise for the destination tables (toDsn). */ async function getRowCount({ dsn, schema, table, method, }) { if (method === "explain") { const firstLine = await runSql_1.runSql.one(dsn, (0, quote_1.sql) `EXPLAIN SELECT 1 FROM ${(0, quote_1.ident)(schema)}.${(0, quote_1.ident)(table)}`); return firstLine.match(/rows=(\d+)/) ? parseInt(RegExp.$1) : null; } else { const firstLine = await runSql_1.runSql.one(dsn, (0, quote_1.sql) ` SELECT tuples_processed FROM pg_stat_progress_copy JOIN pg_class ON pg_class.oid = relid JOIN pg_namespace ON pg_namespace.oid = relnamespace WHERE datname = current_database() AND nspname = ${schema} AND relname = ${table} `); return firstLine.match(/^\d+$/s) ? parseInt(firstLine) : null; } } //# sourceMappingURL=getRowCount.js.map