# A slow count(*) ran 772 s and pushed others' p99 up 6.6×

> Postmortem of a slow count(*) on 30M records: 772 seconds, 66 GB read through a 21 GB page cache, and a 6.6× p99 hit on every other client.

Canonical URL: https://rushdb.com/blog/slow-count-postmortem
Published: 2026-11-29T01:55:00.000Z
Updated: 2026-11-29T08:55:37.406Z
Author: RushDB
Category: Engineering

On 2026-10-20 one slow count(*) over 30M records ran for 772 seconds on a single RushDB node. It used one core, but it read 66 GB through a 21 GB page cache and evicted the pages 32 other clients were reading. Their p99 went from 16.6 s to 110 s and throughput fell 36%. Since 3.0.0 every request can refuse or bound an unindexed scan, but as of 3.0.15 one query's I/O is still not isolated from another's.

## TL;DR

- We ran `SELECT count(*) FROM "user" WHERE age < 25` with no index on `age`, on a 30M-record tenant, beside 32 clients paging an indexed filter.
- The scan took 772 s; the other clients' ok/s fell 36% and p99 rose 6.6× (16.6 s → 110 s), and three minutes later they were still 13% down.
- The cause was the page cache, not CPU. The scan streamed 66 GB from disk through the host's ~21 GB page cache and pushed out the index and record pages everyone else was hitting.
- Unindexed filters now run by default, as in PostgreSQL, and every request can refuse or bound them: `SET disallow_full_table_scans = on`, `statement_timeout`, `work_mem`. An operator can enforce a ceiling for all tenants.
- Nothing isolates one query's I/O from another's, as of 3.0.15. If you don't set a bound, a scan like this can still hurt its neighbours.

## Was this a production incident?

No. It happened in a benchmark run we set up on purpose, on the 30M-record store kept from the large-tenant qualification. The run was set up to measure what one long scan does to everyone else. The public [capacity planning](https://docs.rushdb.com/engine/operations/capacity-planning) page has the method and the 200K numbers; the 30M rows below come from the engine's measurement record.

The host is the same one behind every number in [the capacity envelope](https://rushdb.com/blog/rushdb-capacity-envelope): an i9-12900H with 20 threads, LUKS NVMe, Linux, a pre-release build on storage format v5 (3.0.0 wrote format 6, and 3.0.14 moved to format 7). The tenant held 30M 10-field records in 98.5 GB, with ordered indexes on `status` and `city` and none on `age`.

## Timeline

All dates are from the engine's design notes and measurement log.

| Date | What happened |
|---|---|
| 2026-10-17 | At 10M records the read path refused an unindexed walk after ~2–3 s instead of letting it run for minutes. We added `unindexed-walk-max-owners`, then set its default to 1M records, so a large walk failed up front with the index to declare. |
| 2026-10-19 | The 30M run. Before the indexes existed, every filtered read and facet was refused by that 1M default. |
| 2026-10-20 | `scan_interference` run on the kept 30M store, with the limit set to `0` (off) for the run. 32 clients page `status ∧ city` for 3 minutes, then one SQL `count(*)` on `age < 25` runs, then 3 more minutes. |
| 2026-10-20 | Decision (QRY-3): unindexed filters run by default; requests can only tighten the server's bounds. Session settings land in the PostgreSQL wire adapter. |

The 1M default worked as a seatbelt, but it also meant a correct query on a large tenant failed instead of running. PostgreSQL runs a sequential scan and lets you set `statement_timeout`. We wanted to know what running costs before choosing.

## What did the slow count(*) do to everyone else?

The slow count(*) took 772 s and kept one core busy. The 32 page clients, which already kept 18.9 of 20 cores busy and read nothing from storage, dropped from 2.40 to 1.54 ok/s (−36%) while it ran, and their p99 rose from 16.6 s to 110 s. Storage reads went from 0 to 66 GB (method: [capacity planning](https://docs.rushdb.com/engine/operations/capacity-planning)).

| Phase | Clients ok/s | p50 | p99 | Read from storage | Server CPU |
|---|---|---|---|---|---|
| before (warm) | 2.40 | 14.4 s | 16.6 s | 0 GB | 18.9 cores |
| during the scan (772 s) | **1.54** (−36%) | 16.3 s | **110 s** (6.6×) | **66 GB** | 17.7 cores |
| 3 min after | 2.08 (−13%) | 16.4 s | 19.2 s | 19 GB | 18.5 cores |

Server CPU went *down* during the scan, because the other clients were waiting on disk.

## Why did one query slow down the others?

Because they shared the page cache. Before the scan, the hot index pages and record pages for `status ∧ city` sat in memory, so the 32 clients read 0 bytes from storage. The count walked all 30M records, pulling 66 GB through a cache of about 21 GB. Every page it touched pushed out a page someone else needed next.

```mermaid
flowchart TD
  S["count(*) WHERE age < 25<br/>walks 30M records"] -->|reads 66 GB| PC[("page cache ~21 GB")]
  PC -->|evicts| H["hot status ∧ city<br/>index + record pages"]
  C["32 page clients"] -->|cache miss| D[("NVMe")]
  D --> C
  H -.->|refilled slowly| C
```

The slowest pages suffered most: p50 moved 13%, p99 moved 6.6× (the rest of that 30M run is in [the capacity envelope](https://rushdb.com/blog/rushdb-capacity-envelope)). A page that needs many records is the one most likely to hit an evicted page. And the damage outlasted the query: three minutes after it ended, clients were still 13% down and had read 19 GB more while the cache refilled.

Two smaller findings from the same run:

- The same filter as a native records `$count` stopped after 44 s with `resource_exhausted`. In that build it kept every match in memory, so it hit the 256 MiB `query-memory-max` budget. SQL `count(*)` streams and never keeps the matches, so it ran to the end.
- The scan itself was CPU-light (one core). A CPU quota would not have helped. The resource it took was cache.

## What did we change?

We chose to let unindexed filters run, the way a PostgreSQL sequential scan or a Neo4j label scan runs, and to give every request the means to refuse or bound them. A request can make a bound stricter, never looser, the model [CockroachDB's session settings](https://www.cockroachlabs.com/docs/stable/set-vars) use; the names `disallow_full_table_scans` and `large_full_scan_rows` come from there too. The server keeps the ceiling. The defaults below are those of 3.0.15, unchanged since 3.0.0.

| Bound | Server setting (default) | Request field | pgwire session setting |
|---|---|---|---|
| Time | `request-timeout-ms` (0, none) | `context.deadlineMs` | `statement_timeout` |
| Working memory | `query-memory-max` (256 MiB) | `context.queryMemoryMaxBytes` | `work_mem` |
| Records an unindexed filter may check | `unindexed-walk-max-owners` (0, none) | `context.unindexedWalkMaxOwners` | `disallow_full_table_scans` + `large_full_scan_rows` |

This `psql` session ran against an in-memory node (build 3.0.1) with 100,000 example `user` records on the same laptop model, 2026-10-31 (output copied from `psql`; 3.0.15 gives the same refusal message):

```sql
SELECT count(*) FROM "user" WHERE age < 25;
--  count(*)
-- ----------
--     11001
SET disallow_full_table_scans = on;
SELECT count(*) FROM "user" WHERE age < 25;
-- ERROR:  42601: unindexed walk refused: the filter on `age` would check
-- 100000 records of [user], over the unindexed-walk limit (0); declare an
-- ordered index on the property
SET large_full_scan_rows = 200000;
SELECT count(*) FROM "user" WHERE age < 25;
--  count(*)
-- ----------
--     11001
```

The refusal took 1.6 ms in that session, because the population comes from counters before any record is read. After `CREATE INDEX user_age ON "user"(age);` the same statement runs with the setting on. The [PostgreSQL wire docs](https://docs.rushdb.com/engine/connectivity/postgresql-wire) list the session settings.

`statement_timeout` is checked between batches of a scan. In the same session, `SET statement_timeout = '1ms'` cancelled the count after about 80 ms with `SQL execution cancelled`. Treat the timeout as "stops soon after", not as a hard real-time limit.

## What does the change not fix?

The defaults still let this happen. With no server ceiling and no session setting, the 772 s scan runs again exactly as measured, and its neighbours pay again. The change gives operators and clients a lever; it does not isolate I/O.

What we tell people running RushDB for several tenants or several agents:

- Set `unindexed-walk-max-owners` on the server if one tenant must never scan the whole store.
- Have agents and generated SQL open their sessions with `SET disallow_full_table_scans = on`. An agent gets an immediate error that names the index, which is a better next step than a 13-minute wait.
- Ask the planner first: `describe.explain` reports `property_walk` and the exact `owners` count before running ([resource governance](https://docs.rushdb.com/engine/architecture/resource-governance)).

As of 3.0.15 no per-query I/O priority or cache partitioning exists: tenants on one node share one store file and its page cache ([multitenancy](https://docs.rushdb.com/engine/architecture/multitenancy)), so a big scan still shares the cache with everyone.

Later releases made unindexed filters cheaper, and none of it changes this scenario. Since 3.0.7 a native filter without an index checks its records on several threads, which spreads the same reads over more cores. Since 3.0.14 a native listing filtered on unindexed properties can be answered from in-memory columns, about 10× faster on 100,000 records. SQL `count(*)` does not use those columns, and native reads skip them on a label with a declared ordered index (this tenant had two). A column also takes about 17 bytes per record, about 510 MB for `age` at 30M, over the 256 MiB default `column-cache-bytes`.

Separately, the 32-client page reads at 30M were slow even without a scan (p50 15 s, CPU-bound). 3.0.1 removed a quadratic cost from that path, and it has not been re-measured since.

The broader envelope, and the other things that broke between 200K and 30M records, are in [the capacity envelope post](https://rushdb.com/blog/rushdb-capacity-envelope).

## FAQ

### Why not refuse unindexed scans by default?

A pre-release build did, for three days, with a 1M-record default that never shipped in a release. It failed correct queries on large tenants and made RushDB behave unlike PostgreSQL or Neo4j, where a filter without an index scans. We chose to run by default and make refusal one setting away, at the server or per session.

### Would more CPU cores have helped?

Not much, because the scan held only one core. An ordered index on `age` would have avoided the walk entirely, at a one-off cost: building the two ordered indexes on `status` and `city` over the same 30M-record tenant took 1,268 s. A larger page cache would also have kept more of the other clients' pages resident.

### How do I stop an agent from running a full scan over pgwire?

Start its session with `SET disallow_full_table_scans = on`. A filter that would walk records without a usable index then fails with SQLSTATE 42601 and a message naming the property to index. `large_full_scan_rows` still allows small labels, and `statement_timeout` bounds everything else.

### Is a SQL count(*) without a filter also a scan?

No. `SELECT count(*)` with no filter reads the label's record counter; at 10M records it took 11 ms. An indexed equality filter is counted from index entries without reading records. Only a filter with no usable index walks the label.

## Sources

- [Capacity planning (RushDB engine docs)](https://docs.rushdb.com/engine/operations/capacity-planning)
- [Resource governance and request bounds](https://docs.rushdb.com/engine/architecture/resource-governance)
- [PostgreSQL wire protocol: session settings](https://docs.rushdb.com/engine/connectivity/postgresql-wire)
- [SQL in RushDB](https://docs.rushdb.com/engine/languages/sql)
- [CockroachDB session variables](https://www.cockroachlabs.com/docs/stable/set-vars)
- [PostgreSQL statement_timeout](https://www.postgresql.org/docs/current/runtime-config-client.html)
