The agent's load on your server
This page gives the cost of the agent on a database server, measured on a test server: how long a collection takes, how much processor time, memory and disk reads it uses, what it did to the latency of a workload that was already busy, and how it behaves around locks. For why the agent collects once an hour and how it keeps its load small, see When the agent collects.
On your server the numbers depend on the number of tables and indexes, on the processor and on the disk. Read them as the size of the cost, not as a promise.
In short
Section titled “In short”- Up to 20 000 tables, the agent’s load on the test server was very small. Over a week, its collections kept one processor core busy, PostgreSQL and the agent together, for about 51 seconds with 2 000 tables and about 200 seconds with 20 000 tables: about 0.03% of one core. 161 of the 168 collections in a week are hourly, and with 20 000 tables an hourly collection took under one second on an idle server. While a collection ran next to a busy workload with 20 004 tables, the workload’s latency rose by about 1 to 2 ms on average, and by at most 3.7 ms in a single collection. On a server whose processors or disk were already fully used, the workload completed about 3% to 7.5% fewer transactions for the 2 to 22 seconds of the collection.
- 400 000 tables is an extreme case, and we measured it only to find the edge. It is 20 databases of 20 000 tables each, the most the agent’s limits cover (see Limits). We measured it to know how the agent behaves there, not because it is a size we expect to meet often. The largest effects on a busy workload described on this page appeared at that size, not at 20 000 tables.
- To see which case is closer to yours, count your tables. In an SQL session, with psql or
any other SQL client you normally use, run
SELECT count(*) FROM pg_stat_user_tables;in each database and add up the results.
How we measured
Section titled “How we measured”Each test server was a virtual machine in Azure with 4 vCPUs and 16 GiB of memory
(Standard_D4ds_v5), with a Premium SSD v2 data disk, Debian 13 and PostgreSQL 18.7, with
shared_buffers set to 4 GB. A smaller server is the stricter case: one processor core is a
quarter of this one.
The agent was installed the way the installer installs it, as the same service with the same restrictions, and ran without any change to its code. A collection was started with the parts we wanted to measure: the hourly datasets alone, with the daily catalog, or with the weekly size measurements as well.
The databases held tables in the style of a multi-tenant application: each table with a primary key, text columns and on average 2.5 indexes, some of them unique, on an expression or partial. The largest case was 20 databases of 20 000 tables each. The busy workload came from pgbench, the benchmark program that ships with PostgreSQL, on a separate machine, with a 45 GB table that does not fit in memory.
What one collection costs
Section titled “What one collection costs”| Tables in the instance | Hourly collection | Daily collection, with the catalog | Weekly collection, with the sizes |
|---|---|---|---|
| 200 | 0.06 s, of it 0.03 s of PostgreSQL processor time | 0.13 s, 0.07 s | 0.21 s, 0.12 s |
| 2 000 | 0.26 s, 0.08 s | 0.87 s, 0.44 s | 1.4 s, 0.78 s |
| 20 000 in one database | 0.91 s, 0.59 s | 6.9 s, 4.1 s | 7.1 s, 4.3 s |
| 100 000 in 5 databases | 4.1 s, 2.5 s | 7.8 s, 4.7 s | 8.8 s, 5.6 s |
| 400 000 in 20 databases | 8.5 s, 5.2 s | 8.4 s, 5.2 s | 12.3 s, 9.1 s |
Each cell gives how long the collection took and how much processor time PostgreSQL spent serving it, with the data already in memory. When the data had to be read from disk first, the weekly collection of 20 000 tables took 9.3 s and read 265 MB from disk. With 20 000 tables in one database the snapshot reached its limit of 48 MB, and above that the agent collected only part of what a collection reads: see Very large instances.
- While a collection runs, it uses about one processor core. The agent sends one query at a time and waits for the result, so either PostgreSQL or the agent is working, never both. The processor time of the two together was equal to the length of the collection. On the test server that is one core of four, for the length of the collection.
- Most of that time is spent by PostgreSQL. PostgreSQL used 55% to 75% of the processor time of a weekly collection, in the session that serves the agent. That session is one of PostgreSQL’s own processes and runs at the same priority as the sessions of your application.
- Most collections are hourly. In a week the agent runs 161 hourly collections, 6 daily ones and 1 weekly one. With 2 000 tables that added up to about 17 seconds of PostgreSQL processor time in a week, with 20 000 tables to about 127 seconds, and with 400 000 tables to about 900 seconds, 0.15% of one processor core.
- The cost grows with the number of tables and indexes, until the snapshot is full. Above that point the daily and the weekly collections stopped growing, because the agent stops collecting when the snapshot is full. They are short at 100 000 and 400 000 tables for that reason, and not because those catalogs are cheap to read.
- Started for a single collection, the agent’s peak memory was at most 320 MB at every catalog size we measured. In a weekly collection it was 46 MB with 200 tables, 89 MB with 2 000 tables, 318 MB with 20 000 tables, 309 MB with 100 000 tables and 267 MB with 400 000 tables. It is largest where the snapshot is almost all catalog. The PostgreSQL session that serves the agent used at most 239 MB of its own memory in any collection we measured, and the agent has one session open at a time.
- Running as a service, the agent keeps part of that memory between collections. To see whether memory builds up over many collections, we ran the installed service for 6 hours at a time and made every collection a weekly one: 23 collections, one every 15 minutes. After a collection the process did not return all of its memory to the operating system. Between collections it held 201 to 234 MB with 20 000 tables and 184 to 217 MB with 100 000 tables, against 37 MB right after it started. Its peak reached 330 MB with 20 000 tables and 313 MB with 100 000 tables. From the second collection to the last, the peak grew by 3.4 MB with 20 000 tables, more slowly toward the end, and by 1.6 MB with 100 000 tables, of which 4 kB came in the last 15 collections. Six hours do not tell whether that growth stops completely.
What a collection did to a busy workload
Section titled “What a collection did to a busy workload”We ran pgbench on a separate machine with 16 client sessions, for one to three hours at a time, and started collections at random moments. Between them we put two kinds of comparison: idle periods of the same length, in which nothing happened, and a control in which a shell loop kept one processor core busy for as long as a weekly collection takes. Each window was compared with the 60 seconds just before it. “Latency” below is the 99th percentile of transaction latency: the time within which 99 of 100 transactions finished.
With one database of 20 004 tables and the 45 GB pgbench table:
- With processor capacity to spare and the data in memory, latency rose by about 1.2 to 1.3 ms during a collection. pgbench ran at a fixed rate of 70% of what the server could do. A weekly collection took 20 s under this workload, and the latency rose from 0.51 ms to 1.79 ms during it; an hourly collection took 2.5 s and raised it by 1.2 ms. One busy core for as long as a weekly collection raised the latency by 0.35 to 1.15 ms in seven of eight runs and by 63 ms once, so in a typical run the collection added more latency than one busy core. The server kept up with the fixed rate, so the throughput did not change.
- With processor capacity to spare and a workload that writes, latency rose by about 1 to 2 ms during a collection. pgbench ran its default scenario, loosely based on TPC-B, in which most statements are writes, on rows that fit in memory, at a fixed rate of 70% of what the server could do. A weekly collection took 13.5 s under this workload and raised the latency by 0.9 ms, from 6.05 ms; an hourly collection took 1.8 s and raised it by 1.8 ms. One busy core raised it by at most 0.8 ms in seven of eight runs and by 28 ms once. The throughput did not change.
- With the processor saturated, the workload completed about 7.5% fewer transactions while a collection ran. pgbench ran as fast as the server allowed, with the data in memory. A weekly collection then took 21.5 s, and during it the workload completed 7.5% fewer transactions than just before it; an hourly collection took 2.5 s and cost 7.5% too. One busy core cost 11.9%. This run lasted one hour, so these numbers come from two weekly and four hourly collections. The session that serves the agent waits for the processor like the sessions of the workload: it has no priority over them and gives none to them.
- With the disk reads below what the server’s disk sustains, there was no measurable effect. pgbench read mostly from disk, at 70% of the rate the disk sustained. Neither the latency nor the throughput changed by more than the measurement could tell apart from zero.
- With the disk saturated, the workload completed about 3% fewer transactions while a collection ran. pgbench read from disk as fast as the server allowed. A weekly collection took 17 s and the workload completed 2.7% fewer transactions during it; an hourly collection took 2.2 s and cost 2.8%. One busy core changed nothing, so here the collection took disk reads from the workload, not processor time.
We repeated the measurements on a second, identical server with 20 databases of 20 000 tables each, 400 004 tables in all: the extreme case described under In short. At this size every snapshot was full after the hourly statistics of about half of the databases, so the agent did not read the catalog or measure the size of a single table (see Very large instances), and the collections below did not include that work:
- With processor capacity to spare and the data in memory, latency rose by about 0.5 to 1 ms during most collections. pgbench ran at a fixed rate of 70% of what the server could do. Five of seven weekly collections took 23 to 25 s under this workload and raised the latency by 0.5 to 0.8 ms, from 0.46 ms. The first two weekly collections of the run took 43 s and 38 s and raised it by 6.8 ms and 12.3 ms. In those two, measuring the database sizes took 25 s and 21 s instead of about 6.5 s, and the latency rose while it ran; we did not investigate why. An hourly collection took 17 s and raised the latency by 0.5 to 1.0 ms, and one busy core by 0.6 to 2.8 ms. The throughput did not change.
- With processor capacity to spare and a workload that writes, latency rose from about 6 ms to about 29 ms during a weekly collection. pgbench ran the same writing scenario at a fixed rate of 70% of what the server could do. A weekly collection took 40 s under this workload. In its first 14 s, while the agent read the hourly statistics, the latency rose by 0.7 to 3.2 ms. In the remaining 22 to 25 s, while the agent measured the size of each database on disk, the latency was 26 to 45 ms, against 5.4 to 7.0 ms in the minute before the collection. This happened in each of the seven weekly collections. The agent measures database sizes once a week. An hourly collection, which does not measure them, took 15.5 s and raised the latency by 1.0 to 3.9 ms, and one busy core by 0.4 to 5.2 ms. The throughput did not change. We did not investigate why the size measurement took this long under this workload or why it slowed the transactions.
- With the processor saturated, the workload completed about 10% fewer transactions while a collection ran. pgbench ran as fast as the server allowed, with the data in memory. A weekly collection took 48 to 54 s, and 87 s the first time, and the workload completed 10.1% fewer transactions during it; an hourly collection took 37.5 s and cost 10.1% too. One busy core cost 20.6%. This run lasted one hour, with four weekly and four hourly collections.
- With the disk reads below what the server’s disk sustains, latency rose by about 0.65 ms during a weekly collection. pgbench read mostly from disk, at 70% of the rate the disk sustained. A weekly collection took 27 s under this workload, and the latency rose from 2.25 ms to 2.88 ms during it; the throughput did not change. An hourly collection took 11 s and raised the latency by 0.07 ms. One busy core changed nothing.
- With the disk saturated, the workload completed about 5.5% fewer transactions during a weekly collection. pgbench read from disk as fast as the server allowed. A weekly collection took 35 s and the workload completed 5.5% fewer transactions during it; an hourly collection took 16 s and cost 3.9%. One busy core changed nothing. The latency did not change by more than the measurement could tell apart from zero.
How long the effect lasted. With 20 004 tables, the effect of an hourly collection ended with the collection. We saw no effect after a weekly collection on a workload whose data was in memory either; with the processor saturated, the run had few collections, so an effect after the collection smaller than about 2% of the throughput would not have shown. After a weekly collection, a workload that read from a saturated disk stayed slower for a while: it completed about 0.9% fewer transactions in the first two minutes after the collection and was back to normal about 150 seconds after it. Counted together, the workload lost more than twice as much after the collection as during it.
With 400 004 tables the effect after a weekly collection lasted longer:
- With the disk saturated, the workload completed about 5% fewer transactions in the first two minutes after a weekly collection. The difference became smaller over time, and 4 minutes after the collection the workload still completed 0.6% fewer transactions. In those 4 minutes it lost more than four times as much as during the collection itself; together, the equivalent of about 10 seconds of the workload’s throughput.
- An hourly collection left a smaller effect: about 0.7% fewer transactions for 90 seconds after it, with the disk saturated.
- With the disk reads below what the disk sustains, the latency stayed about 0.1 ms higher for 2 to 3 minutes after a weekly collection.
- With the data in memory, we saw no effect after a collection. With and without writes, the latency after a collection varied as much as after the idle periods, and so did the throughput with the processor saturated.
Our explanation, which we did not measure directly: a collection reads data from disk, the weekly one also the metadata of the data files when it measures the size of each database, which pushes part of the workload’s data out of the operating system’s cache, and the workload then reads it from disk again.
The daily catalog and the weekly size measurement take ACCESS SHARE on one table or index at a time, while that object is read. On the test server we checked what that does to an operation that needs a table to itself, such as ALTER TABLE:
- While the agent read the catalog and measured sizes, no wait for a schema change lock took longer than 4.1 ms. A session took the lock that every schema change needs, on a random table, about 90 times a second, through 40 weekly collections. The 99th percentile of the wait was 0.25 ms while the agent read the catalog and measured sizes, and about 0.5 ms without the agent. The longest wait while the agent worked, 4.1 ms, happened once in 21 734 attempts. Without the agent the longest wait was 3.5 ms, and waits of a few milliseconds also happened between collections, so one wait does not tell whether the agent caused it.
- When a table is locked, the agent gives up after 5 seconds. With another session holding a
table for the whole collection, the agent waited 5 seconds three times, recorded that table as
not measured and left the catalog of that database out of that collection. The collection took
19.6 s instead of 7.2 s. PostgreSQL logged the cancelled statement as
canceling statement due to lock timeout, and withlog_lock_waitsturned on also the wait itself. - The agent did not lengthen a lock queue. With a long transaction holding a table, an ALTER TABLE waiting for it and an application query waiting behind the ALTER TABLE, a collection joined the queue, gave up after 5 seconds and finished. The ALTER TABLE and the application query both ended 0.1 s after the long transaction did, as they would have without the agent.
What the agent does not do
Section titled “What the agent does not do”- It does not watch the load of your server. It collects on its schedule whether the server is busy or idle. During an incident the hourly collection still runs.
- It does not lower its own priority. The agent’s service sets no processor or disk priority, and no setting on the agent’s service could reach the PostgreSQL session that does most of the work.
- It does not limit its own memory. Neither the agent nor its service sets a memory limit. What
the agent keeps during a collection grows with the snapshot, and the snapshot has a limit, so in
our tests its peak memory did not grow with the number of tables once the snapshot was full.
Running as a service for 6 hours, its peak was 4 to 12 MB higher than in a single collection
(see What one collection costs). On Linux the service
carries
OOMScoreAdjust=500, so when the server runs out of memory, the kernel picks the agent before a process of similar size that keeps the default setting.
Very large instances
Section titled “Very large instances”The limit on one snapshot decides what the agent collects from a large catalog. With 2.5 indexes per table and the short names of the test tables, a complete daily snapshot fit up to about 20 000 tables in one database and a complete weekly one up to about 15 300. With many small databases the limit comes earlier, because each database also brings its hourly statistics: by our calculation, about 14 000 and 11 700 tables in the instance with databases of 2 000 tables. Longer names and index definitions lower these numbers. Above them the agent fills the snapshot in order and stops, as described under One snapshot:
- With one database of 20 000 tables, the daily snapshot carried the whole catalog, and the weekly one the catalog of 19 994 tables and the size of none.
- With 100 000 tables in 5 databases, the daily and weekly snapshots carried the hourly statistics of all 5 databases and the catalog of about 11 600 tables of the first one, and no catalog and no sizes of the other 4.
- With 400 000 tables in 20 databases, every snapshot carried the hourly statistics of 10
databases and part of the eleventh. The agent did not connect to the other 9 databases, and no
snapshot carried a catalog or the size of a table or an index, so the checks that need them had
no data on this instance. The weekly snapshot did carry the size of each of the 20 databases.
The agent wrote, for each dataset it cut, a line such as
Snapshot byte budget reached: relation_catalog not collected from 20 database(s).
At 400 000 tables a collection still sent a snapshot every time. The weekly collection took 12.3 s with the data in memory and 48 s with the data read from disk first, and 37 s of those 48 went to measuring the size of the 20 databases, which reads the metadata of every data file of those databases. Under a workload that writes, that measurement took 22 to 25 s, and the workload’s latency rose to 26 to 45 ms while it ran (see What a collection did to a busy workload). The agent’s peak memory was 267 MB.