@saltcorn/postgres 1.0.0-beta.18 → 1.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/postgres.js +22 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/postgres",
3
- "version": "1.0.0-beta.18",
3
+ "version": "1.0.0-beta.19",
4
4
  "description": "Postgres structures for Saltcorn, open-source no-code platform",
5
5
  "homepage": "https://saltcorn.com",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "main": "index.js",
14
14
  "dependencies": {
15
- "@saltcorn/db-common": "1.0.0-beta.18",
15
+ "@saltcorn/db-common": "1.0.0-beta.19",
16
16
  "pg": "^8.2.1",
17
17
  "pg-copy-streams": "^5.1.1",
18
18
  "replacestream": "4.0.3"
package/postgres.js CHANGED
@@ -134,6 +134,28 @@ const drop_reset_schema = async (schema) => {
134
134
  */
135
135
  const count = async (tbl, whereObj) => {
136
136
  const { where, values } = mkWhere(whereObj);
137
+ if (!where) {
138
+ try {
139
+ // fast count for large table but may be stale
140
+ // https://stackoverflow.com/questions/7943233/fast-way-to-discover-the-row-count-of-a-table-in-postgresql
141
+ //https://www.citusdata.com/blog/2016/10/12/count-performance/
142
+ const sql = `SELECT (CASE WHEN c.reltuples < 0 THEN NULL
143
+ WHEN c.relpages = 0 THEN float8 '0' -- empty table
144
+ ELSE c.reltuples / c.relpages END
145
+ * (pg_catalog.pg_relation_size(c.oid)
146
+ / pg_catalog.current_setting('block_size')::int)
147
+ )::bigint
148
+ FROM pg_catalog.pg_class c
149
+ WHERE c.oid = '"${getTenantSchema()}"."${sqlsanitize(tbl)}"'::regclass`;
150
+ sql_log(sql);
151
+ const tq = await (client || pool).query(sql, []);
152
+ const n = +tq.rows[0].int8;
153
+ if (n && n > 10000) return n;
154
+ } catch {
155
+ //skip fast estimate
156
+ }
157
+ }
158
+
137
159
  const sql = `SELECT COUNT(*) FROM "${getTenantSchema()}"."${sqlsanitize(
138
160
  tbl
139
161
  )}" ${where}`;