<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wojtek Pastyrczyk</title>
    <description>The latest articles on DEV Community by Wojtek Pastyrczyk (@wojtekpastyr).</description>
    <link>https://dev.to/wojtekpastyr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4224502%2Fc9e6a3db-db48-4cfb-9155-66837be174f7.png</url>
      <title>DEV Community: Wojtek Pastyrczyk</title>
      <link>https://dev.to/wojtekpastyr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wojtekpastyr"/>
    <language>en</language>
    <item>
      <title>Getting PostgreSQL ready for Black Friday: a SQL-first checklist</title>
      <dc:creator>Wojtek Pastyrczyk</dc:creator>
      <pubDate>Fri, 13 Nov 2026 15:14:56 +0000</pubDate>
      <link>https://dev.to/guardioz/getting-postgresql-ready-for-black-friday-a-sql-first-checklist-223j</link>
      <guid>https://dev.to/guardioz/getting-postgresql-ready-for-black-friday-a-sql-first-checklist-223j</guid>
      <description>&lt;p&gt;&lt;em&gt;A traffic peak means more connections, more sessions after the same rows and more cleanup work for PostgreSQL. Here is which limits break first, when throwing a bigger server at the problem does not help, and how to check every one of them with plain SQL - before Black Friday, not during it.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Peaks rarely invent new PostgreSQL problems. They surface limits that were already there: a connection budget, a long-running transaction, a cleanup threshold.&lt;/li&gt;
&lt;li&gt;A bigger box helps when you are short on CPU, memory or disk speed. It helps little when sessions wait for each other, and not at all when they wait on a limit that is a setting. For some workloads a bigger machine really is enough - the checks below tell you if yours is one of them.&lt;/li&gt;
&lt;li&gt;Connections are usually the first thing to break. Bumping &lt;code&gt;max_connections&lt;/code&gt; is rarely the fix; a calculated pool size plus a properly configured connection pooler usually is.&lt;/li&gt;
&lt;li&gt;Locks come right after. Under load, one hot row, one schema change or one forgotten open transaction can stall hundreds of sessions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shared_buffers&lt;/code&gt;, &lt;code&gt;max_connections&lt;/code&gt;, &lt;code&gt;max_locks_per_transaction&lt;/code&gt; and minor version upgrades all need a restart. Get them done before your change freeze.&lt;/li&gt;
&lt;li&gt;Every check is a psql query. On the day, four queries tell you most of what is happening.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The short version
&lt;/h2&gt;

&lt;p&gt;More hardware fixes hardware shortages, and sometimes that is all you need. To know whether it is enough for you, review five areas before the peak: connections, locks, autovacuum, the WAL and memory headroom. Ship every restart-only change before the change freeze and test the app with it. In a quiet hour before the freeze, refresh planner statistics and vacuum the tables that need it. No schema changes during the peak. On the day, keep four queries at hand: connections by state, what active sessions wait for, who blocks whom, and the oldest open transactions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Weeks before the peak&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure the pooler and load-test the app through it&lt;/li&gt;
&lt;li&gt;Drop the invalid and duplicate indexes you have verified&lt;/li&gt;
&lt;li&gt;Measure how fast the transaction ID age of your tables grows&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Before your change freeze&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every change that needs a restart&lt;/li&gt;
&lt;li&gt;Timeouts for the application role, and &lt;code&gt;log_lock_waits&lt;/code&gt; on&lt;/li&gt;
&lt;li&gt;In a quiet hour: &lt;code&gt;ANALYZE&lt;/code&gt;, &lt;code&gt;VACUUM&lt;/code&gt; where needed, &lt;code&gt;VACUUM (FREEZE)&lt;/code&gt; on the oldest tables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;On the day&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The four queries, run on demand&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SHOW POOLS&lt;/code&gt;, if you run PgBouncer&lt;/li&gt;
&lt;li&gt;No schema changes and no statistics reset&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;A change freeze takes restarts off the table, which is why restart-only changes come first.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What a peak does to your database
&lt;/h2&gt;

&lt;p&gt;Every customer request ends up as work in some database session. On a regular day most sessions finish before anyone else needs the same row, lock or CPU core. During a peak they overlap, and three things grow together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;connection count&lt;/strong&gt; - the platform runs more copies of the app (servers, containers, pods), and each copy opens its own connections;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;contention for the same data&lt;/strong&gt; - think of the stock count of the product on your front page;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;update and delete rate&lt;/strong&gt; - which means more dead rows for autovacuum and more WAL for checkpoints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So readiness is not just a settings review. Settings matter, but so does how long your transactions stay open, and what else runs on the box during the peak: migrations, reports, backups, bulk imports.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Let's just get a bigger server"
&lt;/h2&gt;

&lt;p&gt;Moving the database to a larger machine - more cores, more RAM, faster disks - is a classic pre-Black-Friday move. It is reasonable, and for some workloads it is enough. It works when the database is genuinely short of hardware: queries saturating every core, a working set that no longer fits in memory, disks that cannot keep up.&lt;/p&gt;

&lt;p&gt;It does little when sessions wait for each other, and nothing when they wait on a setting rather than hardware. Four examples you will meet again below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hot row.&lt;/strong&gt; When many orders update the same product's stock count, each update waits "&lt;a href="https://www.postgresql.org/docs/18/transaction-iso.html" rel="noopener noreferrer"&gt;for the first updating transaction to commit or roll back&lt;/a&gt;". More cores do not shorten that queue. Faster hardware only shortens the in-database part of each transaction, not the time the app keeps it open.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forgotten open transaction.&lt;/strong&gt; A session that is idle in transaction keeps its locks until the app ends the transaction, and if it modified data, it also blocks cleanup. The server is doing nothing for that session meanwhile, so hardware is irrelevant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection limit.&lt;/strong&gt; &lt;code&gt;max_connections&lt;/code&gt; is a setting. On a self-managed server, a box with twice the RAM refuses the next connection at the exact same number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cleanup speed.&lt;/strong&gt; Autovacuum sleeps after a set amount of work, and &lt;a href="https://www.postgresql.org/docs/18/runtime-config-vacuum.html" rel="noopener noreferrer"&gt;both the amount and the sleep length are settings&lt;/a&gt;. Once disks are fast enough, those settings - not the disks - cap cleanup speed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Three orders, one product, one stock-count row

Today
  Order 1  [UPDATE][ app work, tx open ][COMMIT]
  Order 2  [....... waiting for the row .......][UPDATE][ app work ][COMMIT]
  Order 3  [.................. waiting for the row ...................][UPDATE]...

Faster CPUs
  Order 1  [UPD][ app work, tx open ][C]
  Order 2  [..... waiting for the row ....][UPD][ app work ][C]
  Order 3  [............. waiting for the row ..............][UPD]...
                                                            time →
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Each order waits for the previous one to commit. More cores do not change the queue; faster CPUs only shrink the database part, not the time the app holds the transaction. Illustration, not a benchmark.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Also note: on a self-managed server, PostgreSQL does not retune itself on bigger hardware. The OS will use the extra RAM to cache data files, but &lt;code&gt;shared_buffers&lt;/code&gt;, &lt;code&gt;work_mem&lt;/code&gt; and &lt;code&gt;max_connections&lt;/code&gt; stay where they were. Some managed services differ - Amazon RDS for PostgreSQL, for instance, &lt;a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html" rel="noopener noreferrer"&gt;computes its default &lt;code&gt;max_connections&lt;/code&gt; from the memory of the instance class&lt;/a&gt;. Verify with &lt;code&gt;SHOW&lt;/code&gt; after the move. The move itself usually means a restart or failover, so it belongs before the change freeze with the other restart-only changes.&lt;/p&gt;

&lt;p&gt;How do you know which case you are in? Look at what active sessions wait for during your busiest hour today, or during a load test - query #2 in the "on the day" section at the end. An empty &lt;code&gt;wait_event_type&lt;/code&gt; usually means on-CPU or waiting for a CPU; &lt;code&gt;IO&lt;/code&gt; means waiting on disk. A bigger server helps with both. &lt;code&gt;Lock&lt;/code&gt; means waiting for another transaction to finish, and a bigger server only helps as much as it speeds up that other transaction. &lt;code&gt;LWLock&lt;/code&gt; waits deserve a closer look - see the locks section. If you would rather know before the peak than on the day, read on.&lt;/p&gt;

&lt;p&gt;All SQL below can be run from psql or any SQL client you like. The checks are read-only and work with the built-in &lt;code&gt;pg_monitor&lt;/code&gt; role. Anything that changes state is called out and needs a superuser or the object owner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connections break first
&lt;/h2&gt;

&lt;p&gt;PostgreSQL forks &lt;a href="https://www.postgresql.org/docs/18/tutorial-arch.html" rel="noopener noreferrer"&gt;a separate server process for every connection&lt;/a&gt;. Three consequences for a peak:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The ceiling is fixed.&lt;/strong&gt; &lt;code&gt;max_connections&lt;/code&gt; &lt;a href="https://www.postgresql.org/docs/18/runtime-config-connection.html" rel="noopener noreferrer"&gt;changes only with a restart&lt;/a&gt;. Once all slots are used, the next connection gets "&lt;a href="https://www.postgresql.org/message-id/1167859.1608043034@sss.pgh.pa.us" rel="noopener noreferrer"&gt;sorry, too many clients already&lt;/a&gt;" and that customer's request fails. Connecting is not free either - a new process plus authentication before the first query - so connect-per-request apps pay that cost every time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Growth comes from the app side.&lt;/strong&gt; Each running app instance - server, container, pod - has its own pool. When the platform scales out for the peak, every new instance brings a new pool. Do the worst-case math up front:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;largest number of application copies running at once x connections in each copy's pool
+ background workers, scheduled jobs and other services that connect directly
+ your own monitoring and administration tools
= the number that must stay below max_connections, minus the slots reserved for administrators
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the database slows down, requests queue for a pool connection. If the app retries failures instantly, retries add load at exactly the wrong time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. More connections ≠ more throughput.&lt;/strong&gt; A running query needs a core. With far more active sessions than cores, they wait for each other - partly inside PostgreSQL, on internal locks protecting shared memory. In a pgsql-admin thread about a load test where active sessions climbed while CPUs stayed mostly idle, Michael Vitale summed it up as "&lt;a href="https://www.postgresql.org/message-id/41c4a64a-6c74-ba06-a914-c175790a7637@sqlexec.com" rel="noopener noreferrer"&gt;a bunch of concurrent active connections contending over the same resources&lt;/a&gt;". Even idle connections cost something: PostgreSQL committer Andres Freund measured that "&lt;a href="https://www.citusdata.com/blog/2020/10/08/analyzing-connection-scalability/" rel="noopener noreferrer"&gt;a large number of connections clearly reduce the efficiency of other connections, even when idle&lt;/a&gt;". That was 2020; his improvements &lt;a href="https://www.citusdata.com/blog/2020/10/25/improving-postgres-connection-scalability-snapshots/" rel="noopener noreferrer"&gt;landed in PostgreSQL 14&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix: a pooler, configured for real
&lt;/h3&gt;

&lt;p&gt;Put a pooler like PgBouncer between the app and PostgreSQL - and configure it. Out of the box it will not do what you need. PgBouncer's default &lt;code&gt;pool_mode&lt;/code&gt; is session, where a server connection "&lt;a href="https://www.pgbouncer.org/config.html" rel="noopener noreferrer"&gt;is released back to pool after client disconnects&lt;/a&gt;". App-side pools keep connections open, so in session mode each client connection pins a server connection for its whole lifetime, and nothing gets shared.&lt;/p&gt;

&lt;p&gt;Transaction mode is where sharing happens: "&lt;a href="https://www.pgbouncer.org/features.html" rel="noopener noreferrer"&gt;a server connection is assigned to a client only during a transaction&lt;/a&gt;". Many client connections share far fewer server connections, and excess requests wait in the pooler, not inside the database.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setup&lt;/th&gt;
&lt;th&gt;12 application connections →&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;No pooler&lt;/td&gt;
&lt;td&gt;12 connections, 12 server processes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PgBouncer, &lt;code&gt;pool_mode = session&lt;/code&gt; (default)&lt;/td&gt;
&lt;td&gt;each client keeps its server connection until it disconnects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PgBouncer, &lt;code&gt;pool_mode = transaction&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;4 server connections serve 12 clients; 2 requests wait in PgBouncer, not in the database&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Only transaction mode actually shares server connections.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Check these &lt;code&gt;pgbouncer.ini&lt;/code&gt; settings against your numbers before the peak (defaults from &lt;a href="https://www.pgbouncer.org/config.html" rel="noopener noreferrer"&gt;PgBouncer's configuration reference&lt;/a&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;pool_mode&lt;/code&gt;&lt;/strong&gt; - transaction, as explained above.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;default_pool_size&lt;/code&gt;&lt;/strong&gt; (default 20) - per database+user pair, not per pooler. Multiply by the number of pairs, and sum across all PgBouncer instances. That total, plus every connection that bypasses the pooler, must stay under &lt;code&gt;max_connections&lt;/code&gt; minus admin-reserved slots. &lt;code&gt;max_db_connections&lt;/code&gt; and &lt;code&gt;max_user_connections&lt;/code&gt; cap per-database and per-user totals; both are unlimited by default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max_client_conn&lt;/code&gt;&lt;/strong&gt; (default 100) - how many client connections PgBouncer accepts. Size it from your worst case. Raising it may also require raising the OS open-files limit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;reserve_pool_size&lt;/code&gt;&lt;/strong&gt; (default 0, i.e. off) - extra server connections for a pool once a client has waited longer than &lt;code&gt;reserve_pool_timeout&lt;/code&gt; (default 5 s). Count these in the total.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;query_wait_timeout&lt;/code&gt;&lt;/strong&gt; (default 120 s) - how long a query may wait for a server connection before PgBouncer drops the client. Decide whether your app should wait that long or fail fast and retry.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;max_prepared_statements&lt;/code&gt;&lt;/strong&gt; - must be above 0 for protocol-level prepared statements to work in transaction mode. Some drivers use them automatically, e.g. &lt;a href="https://jdbc.postgresql.org/documentation/use/" rel="noopener noreferrer"&gt;the PostgreSQL JDBC driver&lt;/a&gt; after the app runs the same prepared statement five times. Default is 200 &lt;a href="https://www.pgbouncer.org/changelog.html" rel="noopener noreferrer"&gt;since PgBouncer 1.24.0&lt;/a&gt; (January 2025), 0 before - so check what version your package ships.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PgBouncer is "&lt;a href="https://www.pgbouncer.org/config.html" rel="noopener noreferrer"&gt;single-threaded and uses one CPU core per instance&lt;/a&gt;". At high request rates, watch the CPU of the PgBouncer process itself; the docs explain running several instances on one port with &lt;code&gt;so_reuseport&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Transaction mode does not keep session state. PgBouncer lists &lt;code&gt;SET and RESET&lt;/code&gt;, &lt;code&gt;LISTEN&lt;/code&gt;, &lt;code&gt;PREPARE&lt;/code&gt; and &lt;code&gt;DEALLOCATE&lt;/code&gt;, session-level advisory locks and temp tables that outlive a transaction among features it does not support there. If your app relies on any of these, change it, or route that part through a database entry with its own &lt;code&gt;pool_mode&lt;/code&gt; set to session. Load-test the app with the pooler and final settings before the freeze - not in peak week.&lt;/p&gt;

&lt;p&gt;Set each app instance's pool size on purpose and cap how many instances the platform may spin up. Without a pooler, the sum above must stay under &lt;code&gt;max_connections&lt;/code&gt; minus admin slots; with one, under &lt;code&gt;max_client_conn&lt;/code&gt;. If you do raise &lt;code&gt;max_connections&lt;/code&gt;, remember every extra session can allocate memory for sorts and hashes (see the memory section) - check your memory headroom first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Leave yourself a way in.&lt;/strong&gt; When the app grabs every slot, PostgreSQL refuses you too - unless a slot is reserved. &lt;code&gt;superuser_reserved_connections&lt;/code&gt; (default 3) &lt;a href="https://www.postgresql.org/docs/18/runtime-config-connection.html" rel="noopener noreferrer"&gt;keeps slots that only superusers can use&lt;/a&gt;. If you debug incidents as a superuser, you already have them - as long as the app itself does not connect as a superuser.&lt;/p&gt;

&lt;p&gt;Debugging with a non-superuser account? PostgreSQL 16+ can reserve slots for it via &lt;code&gt;reserved_connections&lt;/code&gt; (default 0), for roles with the privileges of &lt;code&gt;pg_use_reserved_connections&lt;/code&gt;. As a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="no"&gt;SYSTEM&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;reserved_connections&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- takes effect after a restart&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="n"&gt;pg_use_reserved_connections&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;oncall&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;2&lt;/code&gt; and &lt;code&gt;oncall&lt;/code&gt; are examples. This needs a restart, and each reserved slot comes out of &lt;code&gt;max_connections&lt;/code&gt; - one fewer for the app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;max_connections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;superuser_reserved_connections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="n"&gt;reserved_connections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) AS connections,
       count(*)&lt;/span&gt; &lt;span class="k"&gt;FILTER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*) FILTER (WHERE state LIKE 'idle in transaction%') AS idle_in_transaction
FROM pg_stat_activity
WHERE backend_type = 'client backend';
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the SELECT at today's busiest hour. Compare &lt;code&gt;connections&lt;/code&gt; to &lt;code&gt;max_connections&lt;/code&gt; minus reserved slots, and &lt;code&gt;active&lt;/code&gt; to the server's core count. Then ask: how much does each number grow when traffic multiplies?&lt;/p&gt;

&lt;p&gt;On PgBouncer, check the pooler too. Its admin console is a virtual database called &lt;code&gt;pgbouncer&lt;/code&gt;, open to users in &lt;code&gt;admin_users&lt;/code&gt; or &lt;code&gt;stats_users&lt;/code&gt;. From a shell, e.g. &lt;code&gt;psql -p 6432 pgbouncer&lt;/code&gt; (6432 is the default port):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="no"&gt;CONFIG&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;SHOW&lt;/span&gt; &lt;span class="no"&gt;POOLS&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;SHOW CONFIG&lt;/code&gt; shows the values in effect. In &lt;code&gt;SHOW POOLS&lt;/code&gt;, &lt;code&gt;cl_waiting&lt;/code&gt; counts clients that sent a query but have no server connection yet, and &lt;code&gt;maxwait&lt;/code&gt; is how long the oldest has waited. The docs on &lt;code&gt;maxwait&lt;/code&gt;: "&lt;a href="https://www.pgbouncer.org/usage.html" rel="noopener noreferrer"&gt;If this starts increasing, then the current pool of servers does not handle requests quickly enough.&lt;/a&gt;"&lt;/p&gt;

&lt;h2&gt;
  
  
  Then come the locks
&lt;/h2&gt;

&lt;p&gt;Locks stop two sessions from making conflicting changes to the same data. Most are held for milliseconds and nobody notices - until a peak requests the same locks many times per second and every long-held lock grows a queue. Five patterns account for most of it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hot row.&lt;/strong&gt; An &lt;code&gt;UPDATE&lt;/code&gt; locks its row until the transaction ends. If every order touches the same row - the stock count of the sale product - orders run in series, not in parallel. In &lt;code&gt;pg_stat_activity&lt;/code&gt; the waiters show &lt;code&gt;wait_event_type&lt;/code&gt; &lt;code&gt;Lock&lt;/code&gt; with &lt;code&gt;wait_event&lt;/code&gt; &lt;code&gt;transactionid&lt;/code&gt;, "&lt;a href="https://www.postgresql.org/docs/18/monitoring-stats.html" rel="noopener noreferrer"&gt;Waiting for a transaction to finish&lt;/a&gt;", or &lt;code&gt;tuple&lt;/code&gt;. No knob fixes this; the transaction design does. Touch the hot row as late as possible and commit right after. Never wait on anything external - a payment provider, say - while holding that row.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Schema change behind a long query.&lt;/strong&gt; Many &lt;code&gt;ALTER TABLE&lt;/code&gt; forms take an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock, and the docs are precise: "&lt;a href="https://www.postgresql.org/docs/18/explicit-locking.html" rel="noopener noreferrer"&gt;Only an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock blocks a &lt;code&gt;SELECT&lt;/code&gt; (without &lt;code&gt;FOR UPDATE/SHARE&lt;/code&gt;) statement.&lt;/a&gt;" Lock requests queue. While an &lt;code&gt;ALTER TABLE&lt;/code&gt; waits for a long transaction, every new query on the table - plain SELECTs too - queues behind it. As Laurenz Albe put it on pgsql-admin, the &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock is "&lt;a href="https://www.postgresql.org/message-id/e22d27f5770e010a55c8fc5ab20e017e90d8e358.camel@cybertec.at" rel="noopener noreferrer"&gt;waiting in line and blocking everything else (locks have to queue)&lt;/a&gt;". Keep migrations out of the peak. When you do run one, &lt;a href="https://www.citusdata.com/blog/2018/02/22/seven-tips-for-dealing-with-postgres-locks/" rel="noopener noreferrer"&gt;set a short &lt;code&gt;lock_timeout&lt;/code&gt; first&lt;/a&gt; so it fails rather than blocks everyone, and retry later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;lock_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2s'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;gift_note&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The timeout and column are examples. Create indexes with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt;, which does not block writes. If it fails, it leaves an invalid index that "&lt;a href="https://www.postgresql.org/docs/18/sql-createindex.html" rel="noopener noreferrer"&gt;will still consume update overhead&lt;/a&gt;" until dropped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Lock queue on one table

Without lock_timeout
  SELECT (long report, holds lock) ← ALTER TABLE waits for ACCESS EXCLUSIVE ← SELECT waits ← UPDATE waits ← SELECT waits

With SET lock_timeout = '2s' before ALTER TABLE
  SELECT (long report, holds lock) ← ALTER TABLE gives up after 2 s ← SELECT runs ← UPDATE runs ← SELECT runs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;With &lt;code&gt;lock_timeout&lt;/code&gt;, the schema change bails out after 2 seconds, the queue behind it drains, and you try again later.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Forgotten open transaction.&lt;/strong&gt; An idle-in-transaction session keeps all its locks for as long as the app leaves the transaction open. The docs pitch &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; as a way "&lt;a href="https://www.postgresql.org/docs/18/runtime-config-client.html" rel="noopener noreferrer"&gt;to ensure that idle sessions do not hold locks for an unreasonable amount of time&lt;/a&gt;". Set it, and &lt;code&gt;statement_timeout&lt;/code&gt;, on your app's role rather than server-wide - the docs advise against &lt;code&gt;statement_timeout&lt;/code&gt; in postgresql.conf "because it would affect all sessions". As a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;app_user&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;idle_in_transaction_session_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'60s'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="no"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;app_user&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;statement_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'30s'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Role name and values are examples. Choose a &lt;code&gt;statement_timeout&lt;/code&gt; above your slowest legitimate request, and make sure the app copes with a cancelled statement and a closed session. Role settings only reach new sessions, so pooled connections keep the old values until they reconnect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inconsistent lock order.&lt;/strong&gt; PostgreSQL detects deadlocks and resolves them "&lt;a href="https://www.postgresql.org/docs/18/explicit-locking.html" rel="noopener noreferrer"&gt;by aborting one of the transactions involved&lt;/a&gt;"; the log says &lt;code&gt;deadlock detected&lt;/code&gt;. An ordering that rarely collides on a normal day collides much more under peak load. The fix lives in the app: the docs recommend that "all applications using a database acquire locks on multiple objects in a consistent order".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Waiting inside PostgreSQL.&lt;/strong&gt; Not every wait is on your data. &lt;code&gt;wait_event_type&lt;/code&gt; &lt;code&gt;LWLock&lt;/code&gt; is a &lt;a href="https://www.postgresql.org/docs/18/monitoring-stats.html" rel="noopener noreferrer"&gt;lightweight lock&lt;/a&gt; guarding a shared-memory structure. Lots of these at peak mean too many active sessions competing for the same internals - the fix is fewer active sessions (hello, pooler), not more. One has its own cause: &lt;code&gt;LockManager&lt;/code&gt;. Each session can record a small number of regular table/index locks in a fast private area; beyond that it falls back to the shared lock table, which the commit enlarging the fast area calls "&lt;a href="https://www.postgresql.org/message-id/E1ss4gX-000IvX-63@gemulon.postgresql.org" rel="noopener noreferrer"&gt;considerably more expensive, and may be subject to contention (especially on many-core systems)&lt;/a&gt;". Queries over many partitions or indexes get there quickly. In PostgreSQL 18 the fast area size follows &lt;code&gt;max_locks_per_transaction&lt;/code&gt; (default 64), which needs a restart. If load tests show many &lt;code&gt;LockManager&lt;/code&gt; waits, schedule that restart before the freeze.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;

&lt;p&gt;Enable lock-wait logging before the peak. &lt;code&gt;log_lock_waits&lt;/code&gt; &lt;a href="https://www.postgresql.org/docs/18/runtime-config-logging.html" rel="noopener noreferrer"&gt;is off by default&lt;/a&gt;. With it on, every lock wait longer than &lt;code&gt;deadlock_timeout&lt;/code&gt; (default 1 s) gets logged, with the holder and the queue. After the peak, that is your audit trail of who blocked whom. As a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="no"&gt;SYSTEM&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;log_lock_waits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nf"&gt;pg_reload_conf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The live who-blocks-whom query is in the "on the day" section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can autovacuum keep pace?
&lt;/h2&gt;

&lt;p&gt;Every &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; leaves a dead row version behind; autovacuum reclaims that space. At peak, dead rows accumulate faster. Three factors decide whether cleanup keeps up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When a table qualifies.&lt;/strong&gt; By default, after 50 rows + 20% of the table have been updated or deleted. PostgreSQL 18 adds a cap, &lt;code&gt;autovacuum_vacuum_max_threshold&lt;/code&gt;, &lt;a href="https://www.postgresql.org/docs/18/runtime-config-vacuum.html" rel="noopener noreferrer"&gt;at 100 million rows&lt;/a&gt;. For big, busy tables both are too late. Tune just that table, not the whole server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;autovacuum_vacuum_scale_factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Table and value are examples. This takes a &lt;code&gt;SHARE UPDATE EXCLUSIVE&lt;/code&gt; lock, &lt;a href="https://www.postgresql.org/docs/18/sql-altertable.html" rel="noopener noreferrer"&gt;which does not block reads or writes&lt;/a&gt;, and autovacuum uses the new value on its next check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How fast it runs.&lt;/strong&gt; Autovacuum pauses regularly to avoid hogging the disks; &lt;code&gt;autovacuum_vacuum_cost_limit&lt;/code&gt; sets how much work happens between pauses. The default -1 means "use &lt;code&gt;vacuum_cost_limit&lt;/code&gt;", i.e. 200. And that budget is shared - per the docs, it "is distributed proportionally among the running autovacuum workers, if there is more than one, so that the sum of the limits for each worker does not exceed the value of this variable". More workers without a higher cost limit = more tables in parallel, each slower. If several tables lag, raise the cost limit; raising only the worker count will not help. Extra cleanup competes with customer traffic for disk, so change it before the peak and watch disk afterwards. Both settings reload without restart in PostgreSQL 18. &lt;code&gt;autovacuum_max_workers&lt;/code&gt; can go up to &lt;code&gt;autovacuum_worker_slots&lt;/code&gt; (default 16), which needs a restart.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Running workers&lt;/th&gt;
&lt;th&gt;Budget per worker (default limit 200)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;~67 each&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;One shared budget: more workers, more tables at once, each one slower.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Whether it can remove anything.&lt;/strong&gt; Dead rows that an open transaction might still see cannot be removed. The docs: "&lt;a href="https://www.postgresql.org/docs/18/runtime-config-client.html" rel="noopener noreferrer"&gt;an open transaction prevents vacuuming away recently-dead tuples that may be visible only to this transaction&lt;/a&gt;". An hour-long report during the peak blocks cleanup on every table in its database for that hour, no matter how fast autovacuum is. Same for a data-modifying transaction left idle in transaction. A replication slot, or a replica reporting its queries back to the primary (&lt;code&gt;hot_standby_feedback&lt;/code&gt;), can block cleanup server-wide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;

&lt;p&gt;Dead rows and last cleanup per table (&lt;code&gt;pg_stat_user_tables&lt;/code&gt; is per database, so run it in each one):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;schemaname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_live_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_dead_tup&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_autovacuum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_autoanalyze&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;n_dead_tup&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dead row counts are estimates. Start with large tables near the top whose &lt;code&gt;last_autovacuum&lt;/code&gt; is old or empty. Then the server-wide open transactions holding cleanup back, oldest first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;datname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;application_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;xact_start&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;transaction_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_activity&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;backend_xid&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;backend_xmin&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nf"&gt;greatest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;backend_xid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;backend_xmin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And replication slots:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;slot_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xmin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;xmin_age&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_replication_slots&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Transactions open for hours hold back cleanup, and so do slots with a large &lt;code&gt;xmin_age&lt;/code&gt;. Inactive slots are the usual culprit - nothing advances them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wraparound vacuum at the worst possible moment
&lt;/h2&gt;

&lt;p&gt;Every writing transaction consumes a transaction ID, and PostgreSQL must periodically freeze old rows so IDs can be reused. Once a table's oldest unfrozen XID is older than &lt;code&gt;autovacuum_freeze_max_age&lt;/code&gt; (default 200 million), autovacuum runs a "to prevent wraparound" vacuum on it - even if autovacuum is otherwise disabled. Unlike regular autovacuum, the docs say, this one "&lt;a href="https://www.postgresql.org/docs/18/routine-vacuuming.html" rel="noopener noreferrer"&gt;is not automatically interrupted&lt;/a&gt;" when another command wants a conflicting lock. On a big table it can read most of the table and generate lots of WAL, and any schema change on that table waits for it - with the lock queue piling up behind.&lt;/p&gt;

&lt;p&gt;Peaks consume XIDs faster than normal days. If a big table will hit that age mid-peak, do the work yourself beforehand in a quiet hour. Oldest tables per database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relfrozenxid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;xid_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;mxid_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relminmxid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;multixact_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pg_table_size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;oid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_class&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relkind&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'m'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;relfrozenxid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it twice, a day apart, to get the growth rate, and compare it with the headroom to &lt;code&gt;autovacuum_freeze_max_age&lt;/code&gt;. &lt;code&gt;multixact_age&lt;/code&gt; works the same way with its own limit (below). For tables that will cross either limit during the peak, run &lt;code&gt;VACUUM (FREEZE, VERBOSE)&lt;/code&gt; before the change freeze, as owner or superuser. It is I/O-heavy - pick a quiet hour.&lt;/p&gt;

&lt;p&gt;Server-wide distance to the hard limit, for both XIDs and multixact IDs (used when multiple transactions lock the same row):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;datname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;datfrozenxid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;xid_age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;mxid_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;datminmxid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;multixact_age&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_database&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;xid_age&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;autovacuum_multixact_freeze_max_age&lt;/code&gt; (default 400 million) is the multixact equivalent. Ignore these long enough and the server "&lt;a href="https://www.postgresql.org/docs/18/routine-vacuuming.html" rel="noopener noreferrer"&gt;will refuse to assign new XIDs once there are fewer than three million transactions left until wraparound&lt;/a&gt;" - read-only transactions only. Not a number you want to meet during a peak.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should you VACUUM and ANALYZE by hand first?
&lt;/h2&gt;

&lt;p&gt;Yes - in a quiet hour a few days before the freeze - but treat them differently. A manual run clears accumulated dead rows, refreshes planner stats and updates the visibility map, which lets PostgreSQL "&lt;a href="https://www.postgresql.org/docs/18/routine-vacuuming.html" rel="noopener noreferrer"&gt;answer some queries using only the index, without reference to the underlying table&lt;/a&gt;". Since a table qualifies for autovacuum when its "&lt;a href="https://www.postgresql.org/docs/18/routine-vacuuming.html" rel="noopener noreferrer"&gt;tuples obsoleted since the last &lt;code&gt;VACUUM&lt;/code&gt;&lt;/a&gt;" cross the threshold, a freshly vacuumed table enters the peak with a low count.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database-wide &lt;code&gt;ANALYZE&lt;/code&gt; is cheap.&lt;/strong&gt; "For large tables, &lt;code&gt;ANALYZE&lt;/code&gt; takes a random sample of the table contents, rather than examining every row", and it "&lt;a href="https://www.postgresql.org/docs/18/sql-analyze.html" rel="noopener noreferrer"&gt;can run in parallel with other non-DDL activity on the table&lt;/a&gt;". New stats can change plans, so run it early enough to see the effect before the freeze - not the night before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database-wide &lt;code&gt;VACUUM&lt;/code&gt; is expensive.&lt;/strong&gt; Three things to weigh:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It runs flat out. Autovacuum throttles; a manual &lt;code&gt;VACUUM&lt;/code&gt; does not - &lt;code&gt;vacuum_cost_delay&lt;/code&gt; is 0 by default, "&lt;a href="https://www.postgresql.org/docs/18/runtime-config-vacuum.html" rel="noopener noreferrer"&gt;which disables the cost-based vacuum delay feature&lt;/a&gt;". Great in a quiet hour, bad if it spills into busy hours.&lt;/li&gt;
&lt;li&gt;It takes as long as your biggest tables, and changed pages go to the WAL too. Never run it on this database? Then you do not know how long it takes. Watch the &lt;code&gt;pg_wal&lt;/code&gt; disk and &lt;code&gt;pg_stat_progress_vacuum&lt;/code&gt; while it runs.&lt;/li&gt;
&lt;li&gt;It cannot remove rows an open transaction still holds back. Check the oldest transactions and replication slots first (autovacuum section).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So: refresh stats everywhere, vacuum where it matters - the top of the dead-row list and the tables closest to &lt;code&gt;autovacuum_freeze_max_age&lt;/code&gt;. &lt;a href="https://www.postgresql.org/docs/18/app-vacuumdb.html" rel="noopener noreferrer"&gt;&lt;code&gt;vacuumdb&lt;/code&gt;&lt;/a&gt;, shipped with PostgreSQL, handles both from a shell. As a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vacuumdb --all --analyze-only
vacuumdb --all --freeze --min-xid-age=100000000
vacuumdb --dbname=shop --table=orders --analyze
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line one refreshes stats in every database. Line two vacuums and freezes only tables with XID age ≥ 100 million. Line three vacuums and analyzes one table. Database, table and age are examples. If a previous quiet hour proved the whole database fits the window, &lt;code&gt;vacuumdb --all --analyze&lt;/code&gt; does it all at once.&lt;/p&gt;

&lt;p&gt;Do &lt;strong&gt;not&lt;/strong&gt; run &lt;code&gt;VACUUM FULL&lt;/code&gt; before the peak. It "rewrites the entire contents of the table into a new disk file" and "&lt;a href="https://www.postgresql.org/docs/18/sql-vacuum.html" rel="noopener noreferrer"&gt;requires an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock on each table while it is being processed&lt;/a&gt;" - every query on the table waits, and it needs space for a full second copy. Plain &lt;code&gt;VACUUM&lt;/code&gt; runs alongside reads and writes, locking only briefly at the end, and only if it can hand empty trailing pages back to the OS.&lt;/p&gt;

&lt;h2&gt;
  
  
  WAL sizing for peak write volume
&lt;/h2&gt;

&lt;p&gt;Every change hits the WAL first. Checkpoints periodically flush changed data to data files so older WAL is no longer needed for crash recovery. Per the docs, "&lt;a href="https://www.postgresql.org/docs/18/wal-configuration.html" rel="noopener noreferrer"&gt;A checkpoint is begun every &lt;code&gt;checkpoint_timeout&lt;/code&gt; seconds, or if &lt;code&gt;max_wal_size&lt;/code&gt; is about to be exceeded, whichever comes first.&lt;/a&gt;" At peak the WAL grows faster, so a &lt;code&gt;max_wal_size&lt;/code&gt; that was fine yesterday can force early checkpoints. Each one costs extra writes, since with the default &lt;code&gt;full_page_writes&lt;/code&gt; "the first modification of a data page after each checkpoint results in logging the entire page content".&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;num_timed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num_requested&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num_done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stats_reset&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_checkpointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;num_timed&lt;/code&gt; = timer-triggered checkpoints, &lt;code&gt;num_requested&lt;/code&gt; = requested for other reasons. In PostgreSQL 18 &lt;a href="https://www.postgresql.org/docs/18/release-18.html" rel="noopener noreferrer"&gt;both also count skipped checkpoints&lt;/a&gt;; &lt;code&gt;num_done&lt;/code&gt; counts those actually performed. Counters are server-wide and cumulative since &lt;code&gt;stats_reset&lt;/code&gt;, so snapshot right before and right after your busiest hour and diff them. PostgreSQL 17 has no &lt;code&gt;num_done&lt;/code&gt;; 16 and older use &lt;code&gt;checkpoints_timed&lt;/code&gt; and &lt;code&gt;checkpoints_req&lt;/code&gt; in &lt;code&gt;pg_stat_bgwriter&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Requested checkpoints are not only about WAL size - a manual &lt;code&gt;CHECKPOINT&lt;/code&gt; or a backup start counts too. Confirm in the server log: &lt;code&gt;log_checkpoints&lt;/code&gt; is on by default since PostgreSQL 15, and &lt;code&gt;checkpoint starting: wal&lt;/code&gt; means WAL size. If they come too close together you will also see &lt;code&gt;checkpoints are occurring too frequently&lt;/code&gt; with a hint to raise &lt;code&gt;max_wal_size&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;max_wal_size&lt;/code&gt; reloads without a restart - no maintenance window needed. Check free space on the &lt;code&gt;pg_wal&lt;/code&gt; disk first, because the limit is soft: "&lt;code&gt;max_wal_size&lt;/code&gt; is never a hard limit anyway, so you should always leave plenty of headroom to avoid running out of disk space." A higher value also raises worst-case disk usage and crash recovery time. We walk through the measurement and the change step by step in &lt;a href="https://guardioz.com/blog/max-wal-size-explained?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=blog_syndication&amp;amp;utm_content=postgresql-before-black-friday" rel="noopener noreferrer"&gt;&lt;code&gt;max_wal_size&lt;/code&gt;, explained&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory headroom for all those sessions
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;shared_buffers&lt;/code&gt; (PostgreSQL's own cache) only changes with a restart. &lt;code&gt;work_mem&lt;/code&gt; is different - it limits each sort or hash &lt;em&gt;operation&lt;/em&gt;, not each session. The docs explain the catch: "a complex query might perform several sort and hash operations at the same time", and "&lt;a href="https://www.postgresql.org/docs/18/runtime-config-resource.html" rel="noopener noreferrer"&gt;several running sessions could be doing such operations concurrently. Therefore, the total memory used could be many times the value of &lt;code&gt;work_mem&lt;/code&gt;&lt;/a&gt;". Hashes can use &lt;code&gt;work_mem&lt;/code&gt; × &lt;code&gt;hash_mem_multiplier&lt;/code&gt; (default 2). Parallel queries add more, as each worker gets its own &lt;code&gt;work_mem&lt;/code&gt;. A peak multiplies how many sessions do this concurrently.&lt;/p&gt;

&lt;p&gt;If a Linux box runs out of memory, the kernel may kill a PostgreSQL process. If it kills the &lt;code&gt;postmaster&lt;/code&gt;, the docs describe the aftermath: "&lt;a href="https://www.postgresql.org/docs/18/kernel-resources.html" rel="noopener noreferrer"&gt;Although existing database connections will continue to function normally, no new connections will be accepted. To recover, PostgreSQL will need to be restarted.&lt;/a&gt;"&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix
&lt;/h3&gt;

&lt;p&gt;Do not bump &lt;code&gt;work_mem&lt;/code&gt; server-wide in the week before the peak. If a known report or job needs more, set it per session or per role. To see whether sorts and hashes spill to temp files today:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;datname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temp_files&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pg_size_pretty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temp_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;temp_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stats_reset&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_database&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;datname&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These counters are cumulative too, so diff two readings. &lt;code&gt;log_temp_files&lt;/code&gt; logs every temp file above a size you pick; it is off by default and a superuser enables it with a reload.&lt;/p&gt;

&lt;h2&gt;
  
  
  Indexes and planner stats
&lt;/h2&gt;

&lt;p&gt;Indexes rarely fail on the day, but each adds work to every &lt;code&gt;INSERT&lt;/code&gt; and to every &lt;code&gt;UPDATE&lt;/code&gt; that cannot be a &lt;a href="https://www.postgresql.org/docs/18/storage-hot.html" rel="noopener noreferrer"&gt;HOT update&lt;/a&gt; (one that touches no indexed column and fits on the same page). Do this a few weeks out - dropping an index is a change you want to test too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Invalid indexes&lt;/strong&gt; - usually leftovers from a failed &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; - help no query and may still cost work on every write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;indexrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;index_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indrelid&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;regclass&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_index&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="n"&gt;indisvalid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An index currently being built with &lt;code&gt;CREATE INDEX CONCURRENTLY&lt;/code&gt; is also invalid until done, so check &lt;code&gt;pg_stat_progress_create_index&lt;/code&gt; before dropping anything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Duplicate indexes&lt;/strong&gt; - same table, same columns in the same order, same type, same expressions, same WHERE clause - do the same work twice. The PostgreSQL wiki has &lt;a href="https://wiki.postgresql.org/wiki/Index_Maintenance" rel="noopener noreferrer"&gt;a query that finds candidates&lt;/a&gt;. Compare the full &lt;code&gt;indexdef&lt;/code&gt; in &lt;code&gt;pg_indexes&lt;/code&gt; before dropping: lookalikes can differ in, say, the operator class, and one may back a unique constraint.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unused indexes&lt;/strong&gt; show low &lt;code&gt;idx_scan&lt;/code&gt;; &lt;code&gt;last_idx_scan&lt;/code&gt; (PostgreSQL 16+) shows the last use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;schemaname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indexrelname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idx_scan&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_idx_scan&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_indexes&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;idx_scan&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Four caveats before dropping: counters start at the last stats reset; an index used only during peaks or by a monthly report can look unused today; an index used only by replica queries shows zero scans on the primary; and a primary key or unique constraint index enforces the constraint regardless of scans. Use &lt;code&gt;DROP INDEX CONCURRENTLY&lt;/code&gt;, weeks ahead.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Planner statistics&lt;/strong&gt; drive every plan. Loading a new catalog or price list before the peak? &lt;code&gt;ANALYZE&lt;/code&gt; those tables afterwards so plans match the data. Changes since the last &lt;code&gt;ANALYZE&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;schemaname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n_mod_since_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_analyze&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_autoanalyze&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_user_tables&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;n_mod_since_analyze&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Restart or reload? Plan accordingly
&lt;/h2&gt;

&lt;p&gt;Everything in the top half of this table needs a restart - which is why this review goes in the calendar, not the backlog.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Change (PostgreSQL 18)&lt;/th&gt;
&lt;th&gt;Applied by&lt;/th&gt;
&lt;th&gt;When&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;shared_buffers&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restart&lt;/td&gt;
&lt;td&gt;Before the change freeze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;max_connections&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restart&lt;/td&gt;
&lt;td&gt;Before the change freeze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;reserved_connections&lt;/code&gt;, &lt;code&gt;superuser_reserved_connections&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Restart&lt;/td&gt;
&lt;td&gt;Before the change freeze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;max_locks_per_transaction&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restart&lt;/td&gt;
&lt;td&gt;Before the change freeze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;autovacuum_worker_slots&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restart&lt;/td&gt;
&lt;td&gt;Before the change freeze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Minor version update&lt;/td&gt;
&lt;td&gt;Restart&lt;/td&gt;
&lt;td&gt;Before the change freeze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;max_wal_size&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reload&lt;/td&gt;
&lt;td&gt;Any time, once you know the disk reserve&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;autovacuum_max_workers&lt;/code&gt; (PostgreSQL 18)&lt;/td&gt;
&lt;td&gt;Reload&lt;/td&gt;
&lt;td&gt;Any time, up to &lt;code&gt;autovacuum_worker_slots&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;autovacuum_vacuum_cost_limit&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Reload&lt;/td&gt;
&lt;td&gt;Any time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;log_lock_waits&lt;/code&gt;, &lt;code&gt;log_temp_files&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Reload, as superuser&lt;/td&gt;
&lt;td&gt;Any time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;work_mem&lt;/code&gt;, &lt;code&gt;statement_timeout&lt;/code&gt;, &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt;, &lt;code&gt;lock_timeout&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Per session or role&lt;/td&gt;
&lt;td&gt;Any time; role settings reach new sessions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Per-table autovacuum settings&lt;/td&gt;
&lt;td&gt;&lt;code&gt;ALTER TABLE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Any time; used at autovacuum's next check&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;On your own server: &lt;code&gt;SELECT version();&lt;/code&gt; shows what you run, and &lt;a href="https://www.postgresql.org/support/versioning/" rel="noopener noreferrer"&gt;PostgreSQL's versioning page&lt;/a&gt; lists the latest minor for each major. The &lt;code&gt;context&lt;/code&gt; column in &lt;code&gt;pg_settings&lt;/code&gt; tells you how a setting applies (&lt;code&gt;postmaster&lt;/code&gt; = restart, &lt;code&gt;sighup&lt;/code&gt; = reload), and &lt;code&gt;pending_restart&lt;/code&gt; flags changes already in the config but waiting for a restart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pending_restart&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_settings&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'shared_buffers'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'max_connections'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'max_locks_per_transaction'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="s1"&gt;'autovacuum_max_workers'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'max_wal_size'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'work_mem'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setting&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_settings&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;pending_restart&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any row in the second result is a change that will take effect at the next restart - planned or not. Find out before the peak.&lt;/p&gt;

&lt;p&gt;If your team has a pre-Black-Friday freeze, do the restart-only changes before it. The rest technically do not need a restart, but a change you make for the first time mid-peak is an untested change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Game day: four queries to keep open
&lt;/h2&gt;

&lt;p&gt;These cover most of what happens during the peak. Run them on demand, not in a tight loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#1 Connections by state&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state
ORDER BY count(*)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;#2 What active sessions are waiting for&lt;/strong&gt; - an empty &lt;code&gt;wait_event_type&lt;/code&gt; means nothing PostgreSQL tracks: usually on-CPU or waiting for a CPU, so compare with server CPU usage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;wait_event_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wait_event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="cm"&gt;(*)
FROM pg_stat_activity
WHERE state = 'active' AND backend_type = 'client backend'
GROUP BY wait_event_type, wait_event
ORDER BY count(*)&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;#3 Who blocks whom&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;usename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wait_event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;query_start&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;query_running_for&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;blocked_by&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pg_blocking_pids&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;blocked_by&lt;/span&gt;
      &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_activity&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
      &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_event_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Lock'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;query_running_for&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;blocked_by&lt;/code&gt; lists sessions holding a conflicting lock plus those ahead in the queue. The docs caution that "&lt;a href="https://www.postgresql.org/docs/18/functions-info.html" rel="noopener noreferrer"&gt;frequent calls to this function could have some impact on database performance&lt;/a&gt;" - another reason to run it only when needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;#4 Oldest open transactions&lt;/strong&gt; - the query from the autovacuum section.&lt;/p&gt;

&lt;p&gt;Running PgBouncer? Keep &lt;code&gt;SHOW POOLS&lt;/code&gt; open too. A request queued in the pooler has no database session yet, so it is invisible in &lt;code&gt;pg_stat_activity&lt;/code&gt;; &lt;code&gt;cl_waiting&lt;/code&gt; and &lt;code&gt;maxwait&lt;/code&gt; show it.&lt;/p&gt;

&lt;p&gt;And one thing &lt;em&gt;not&lt;/em&gt; to do: do not reset stats for a "clean slate" on the day. "&lt;a href="https://www.postgresql.org/docs/18/monitoring-stats.html" rel="noopener noreferrer"&gt;Using &lt;code&gt;pg_stat_reset()&lt;/code&gt; also resets counters that autovacuum uses to determine when to trigger a vacuum or an analyze.&lt;/a&gt;" Take two readings and diff them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Guardioz fits
&lt;/h2&gt;

&lt;p&gt;Guardioz automates part of this checklist, roughly hourly, from the same statistics. You install the agent on the database server with one command, and the first snapshot is analysed on arrival. Checks that follow activity over time - like WAL volume - also use later snapshots, so they get more certain as data accumulates. From this article, it covers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;max_wal_size&lt;/code&gt;, computed from your server's own WAL volume and the share of requested vs. timed checkpoints, checked against free space on the data disk;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;shared_buffers&lt;/code&gt;, raised only under measured cache pressure, and &lt;code&gt;work_mem&lt;/code&gt;, never recommended above a limit derived from machine memory, &lt;code&gt;shared_buffers&lt;/code&gt; and &lt;code&gt;max_connections&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;tables where autovacuum only kicks in after a huge number of dead rows, with a per-table statement to lower the threshold where a safe value can be computed;&lt;/li&gt;
&lt;li&gt;open transactions blocking autovacuum;&lt;/li&gt;
&lt;li&gt;whether autovacuum is enabled;&lt;/li&gt;
&lt;li&gt;invalid and duplicate indexes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every finding shows the measurements it is based on, and settings recommendations say whether a restart is needed. If there is a statement to run, you get it - running it is your call. The agent is read-only, connects locally and sends data out over HTTPS.&lt;/p&gt;

&lt;p&gt;What it does &lt;strong&gt;not&lt;/strong&gt; cover: locks and deadlocks, connection counts and limits, timeouts, distance to XID wraparound, unused indexes, autovacuum worker/cost settings, and query performance (query analysis is planned). It collects about once an hour, so it is not a live peak monitor - on the day, use the four queries above.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Does Guardioz work with managed PostgreSQL (Amazon RDS, Aurora, Cloud SQL, Azure Database for PostgreSQL)?&lt;/strong&gt;&lt;br&gt;
No. It supports PostgreSQL 18 on your own servers - Linux with systemd, and FreeBSD. The agent runs on the database host, connects over the local socket and measures that machine, and managed services do not give you a shell there. Azure Database for PostgreSQL flexible server support is planned. The read-only statistics queries in this article do work on managed services; there you change settings in the provider's console rather than with &lt;code&gt;ALTER SYSTEM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which PostgreSQL versions?&lt;/strong&gt;&lt;br&gt;
PostgreSQL 18. The queries here target 18, and where older versions differ, it is noted next to the query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Does Guardioz change anything on my server?&lt;/strong&gt;&lt;br&gt;
Not your settings and not your data. If a finding comes with a statement, you decide whether to run it. The installer creates a monitoring role and a system account for the agent, and &lt;a href="https://docs.guardioz.com/install/what-the-installer-changes/" rel="noopener noreferrer"&gt;the docs list every change it makes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What leaves my network?&lt;/strong&gt;&lt;br&gt;
Snapshots, sent over HTTPS on a connection the agent opens itself - no inbound port. &lt;a href="https://docs.guardioz.com/reference/data-collected/" rel="noopener noreferrer"&gt;The docs list what a snapshot contains&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do I need a DBA?&lt;/strong&gt;&lt;br&gt;
No - it is built for teams running PostgreSQL in production without one. You do need someone who reads SQL and understands what a restart means for your service; findings are written for that person.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pricing?&lt;/strong&gt;&lt;br&gt;
Guardioz is in beta, and early - we say so up front. Always free for one user, no credit card to sign up. The Team plan is $49 per user per month.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How often should I redo this review?&lt;/strong&gt;&lt;br&gt;
Before every planned traffic increase, and after any big change in table sizes or write volume. Settings that fit a database at one size stop fitting as it grows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Laurenz Albe, &lt;a href="https://www.cybertec-postgresql.com/en/tuning-max_connections-in-postgresql/" rel="noopener noreferrer"&gt;Tuning &lt;code&gt;max_connections&lt;/code&gt; in PostgreSQL&lt;/a&gt; (CYBERTEC)&lt;/li&gt;
&lt;li&gt;Andres Freund, &lt;a href="https://www.citusdata.com/blog/2020/10/08/analyzing-connection-scalability/" rel="noopener noreferrer"&gt;Analyzing the Limits of Connection Scalability in Postgres&lt;/a&gt; (Citus Data)&lt;/li&gt;
&lt;li&gt;Marco Slot, &lt;a href="https://www.citusdata.com/blog/2018/02/22/seven-tips-for-dealing-with-postgres-locks/" rel="noopener noreferrer"&gt;When Postgres blocks: 7 tips for dealing with locks&lt;/a&gt; (Citus Data)&lt;/li&gt;
&lt;li&gt;Lukas Fittl, &lt;a href="https://pganalyze.com/blog/5mins-postgres-LWLock-lock-manager-contention" rel="noopener noreferrer"&gt;GitLab's challenge with Postgres &lt;code&gt;LWLock&lt;/code&gt; lock_manager contention&lt;/a&gt; (pganalyze)&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://guardioz.com/blog/max-wal-size-explained?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=blog_syndication&amp;amp;utm_content=postgresql-before-black-friday" rel="noopener noreferrer"&gt;&lt;code&gt;max_wal_size&lt;/code&gt;, explained&lt;/a&gt; (Guardioz)&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;This article was originally published on the &lt;a href="https://guardioz.com/blog/postgresql-before-black-friday?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=blog_syndication&amp;amp;utm_content=postgresql-before-black-friday" rel="noopener noreferrer"&gt;Guardioz blog&lt;/a&gt;. Guardioz turns your PostgreSQL server's own statistics into prioritised findings with the evidence attached. It is in beta and always free for one user: &lt;a href="https://guardioz.com/?utm_source=devto&amp;amp;utm_medium=referral&amp;amp;utm_campaign=blog_syndication&amp;amp;utm_content=postgresql-before-black-friday" rel="noopener noreferrer"&gt;guardioz.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>performance</category>
      <category>devops</category>
    </item>
    <item>
      <title>The logging setting that slows down new connections</title>
      <dc:creator>Wojtek Pastyrczyk</dc:creator>
      <pubDate>Wed, 04 Nov 2026 08:19:27 +0000</pubDate>
      <link>https://dev.to/guardioz/the-logging-setting-that-slows-down-new-connections-261f</link>
      <guid>https://dev.to/guardioz/the-logging-setting-that-slows-down-new-connections-261f</guid>
      <description>&lt;p&gt;&lt;em&gt;The name log_hostname suggests that this setting puts the client address into PostgreSQL's server log. It does not: the address comes from &lt;code&gt;%h&lt;/code&gt; in &lt;code&gt;log_line_prefix&lt;/code&gt;. What log_hostname adds is a DNS lookup at the start of every new connection. We explain how the two settings work together, and we show what that lookup cost on PostgreSQL 18 when DNS was healthy, slow and down.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PostgreSQL 18.6 server log&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2026-10-02 10:06:26.730 UTC [7207] [unknown]@[unknown]/[unknown] 10.42.1.5 LOG:  connection received: host=10.42.1.5 port=60038
2026-10-02 10:06:26.742 UTC [7207] bench@postgres/[unknown] 10.42.1.5 LOG:  connection authorized: user=bench database=postgres application_name=pgbench SSL enabled (protocol=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384, bits=256)
2026-10-02 10:06:26.742 UTC [7207] bench@postgres/pgbench 10.42.1.5 LOG:  connection ready: setup total=13.170 ms, fork=0.513 ms, authentication=6.245 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Three lines from one connection in our test. The log line prefix ends with &lt;code&gt;%h&lt;/code&gt;, and the circled address is what &lt;code&gt;%h&lt;/code&gt; prints: the IP address of the client that connected, not of the server. PostgreSQL logged it without any DNS lookup.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Two settings that work together
&lt;/h2&gt;

&lt;p&gt;The server log can show where each connection came from. Two settings decide how, and they do different jobs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;%h&lt;/code&gt; is an escape in &lt;code&gt;log_line_prefix&lt;/code&gt; - the text PostgreSQL writes at the start of every log line. The documentation describes it as "Remote host name or IP address". On every line written for a client session, it prints where that client connected from.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;log_hostname&lt;/code&gt; is an on/off setting, off by default. It decides what &lt;code&gt;%h&lt;/code&gt; prints: with the setting off, the client's IP address; with it on, a host name that PostgreSQL looks up in DNS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;a href="https://www.postgresql.org/docs/18/runtime-config-logging.html" rel="noopener noreferrer"&gt;PostgreSQL 18 documentation&lt;/a&gt; describes log_hostname like this: "By default, connection log messages only show the IP address of the connecting host. Turning this parameter on causes logging of the host name as well. Note that depending on your host name resolution setup this might impose a non-negligible performance penalty."&lt;/p&gt;

&lt;p&gt;Two details matter in practice. First, log_hostname adds no field of its own. Without &lt;code&gt;%h&lt;/code&gt; - or &lt;code&gt;%r&lt;/code&gt;, which adds the port - in &lt;code&gt;log_line_prefix&lt;/code&gt;, ordinary log lines do not show the client at all, whatever log_hostname says, and the upstream default prefix, &lt;code&gt;'%m [%p] '&lt;/code&gt;, has neither. The setting also changes the &lt;code&gt;host=&lt;/code&gt; field of the "connection received" message, which appears when connection logging is on, and it fills the &lt;code&gt;client_hostname&lt;/code&gt; column of &lt;code&gt;pg_stat_activity&lt;/code&gt;. Second, in our tests the host name replaced the IP address in the log. It did not appear next to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the lookup is, and when it happens
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Reverse_DNS_lookup" rel="noopener noreferrer"&gt;reverse DNS lookup&lt;/a&gt; asks DNS which name belongs to an IP address (a PTR record). With log_hostname on, PostgreSQL makes this lookup for every new TCP connection, right after the server process for that connection starts - before the client's first message is read and before authentication. The connection waits until DNS answers, or until the system's resolver gives up. Connections over a local Unix socket make no lookup.&lt;/p&gt;

&lt;p&gt;The lookup runs whenever the setting is on, also when nothing in your configuration prints the name. Its time does not count against &lt;code&gt;authentication_timeout&lt;/code&gt;, either. A comment next to the lookup in the &lt;a href="https://github.com/postgres/postgres/blob/REL_18_STABLE/src/backend/tcop/backend_startup.c" rel="noopener noreferrer"&gt;PostgreSQL 18 source code&lt;/a&gt; says so directly: "PreAuthDelay and any DNS interactions above don't count against the time limit."&lt;/p&gt;

&lt;h2&gt;
  
  
  What it cost with healthy DNS
&lt;/h2&gt;

&lt;p&gt;We measured the cost on two virtual machines in Microsoft Azure, in one private network: PostgreSQL 18.6 on Ubuntu 24.04 (Standard_D4s_v5, 4 vCPUs) and a separate client machine (Standard_D2s_v5). Connections used TLS 1.3 and SCRAM-SHA-256 password authentication, as many production setups do. The client ran pgbench, the benchmark tool that ships with PostgreSQL, with a one-line custom script (&lt;code&gt;SELECT 1&lt;/code&gt;) and the &lt;code&gt;-C&lt;/code&gt; option, which opens a new connection for every transaction. That makes connection setup most of the work, which is what this test is about. It is not a throughput test of a real workload.&lt;/p&gt;

&lt;p&gt;Azure's DNS service answered the lookup for the client's address with the name vm-logtalk-hostname-cli.internal.cloudapp.net. Ubuntu runs a local DNS cache by default (systemd-resolved). Without such a cache, every lookup goes to the DNS server, so we tested the database server both ways.&lt;/p&gt;

&lt;p&gt;For each setting, the table shows two numbers that pgbench prints at the end of a run. Connection time is how long it took, on average, to open one connection. Connections per second is how many times per second the client connected, ran &lt;code&gt;SELECT 1&lt;/code&gt; and disconnected; pgbench reports it as transactions per second, and with &lt;code&gt;-C&lt;/code&gt; every transaction is one connection. With one client, each connection starts only after the previous one has finished, so the two numbers move in opposite directions: when every connection takes longer, fewer of them fit into a second. Connections per second here is the pace of a single client connecting again and again, not the most the server could accept.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;log_hostname&lt;/th&gt;
&lt;th&gt;DNS lookups&lt;/th&gt;
&lt;th&gt;Connection time (lower is better)&lt;/th&gt;
&lt;th&gt;Connections per second (higher is better)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;off (the default)&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;14.1 ms (14.1-14.2)&lt;/td&gt;
&lt;td&gt;67.8 (baseline)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;on&lt;/td&gt;
&lt;td&gt;through a local cache&lt;/td&gt;
&lt;td&gt;14.4 ms (14.3-14.5)&lt;/td&gt;
&lt;td&gt;66.6 (-1.8%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;on&lt;/td&gt;
&lt;td&gt;straight to Azure DNS, no local cache&lt;/td&gt;
&lt;td&gt;20.2 ms (19.9-20.2)&lt;/td&gt;
&lt;td&gt;48.1 (-29%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Median of three 30-second pgbench runs per setting, run in alternating order: one client, a new connection for every transaction (pgbench -C), PostgreSQL 18.6 on Azure, Azure DNS answering normally. Connection time is pgbench's "average connection time"; the range shows the lowest and the highest of the three runs. Connections per second is pgbench's "tps (including reconnection times)".&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Without a local cache, every new connection took about 6 ms longer: 43% more time per connection, and 29% fewer connections per second in this test. The server's own timing agrees. PostgreSQL 18 can log how long each connection took to set up (&lt;code&gt;log_connections&lt;/code&gt; with the &lt;code&gt;setup_durations&lt;/code&gt; option): the average grew from 10.4 ms to 16.3 ms, while the authentication part stayed at 6.0 ms in all three variants. The extra time is spent before authentication, where the lookup happens.&lt;/p&gt;

&lt;p&gt;With the local cache, the difference was 0.3 ms. Azure DNS gave this answer a lifetime (TTL) of 10 seconds, so most lookups came from the cache. The 6 ms is what one round trip to Azure DNS cost from this server. On your network it is a different number: the time your DNS server needs to answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it cost with slow or failing DNS
&lt;/h2&gt;

&lt;p&gt;Healthy DNS is the best case. We then slowed DNS down, and then made it unreachable - only for the database server and only for DNS traffic - and ran the same test with one client.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;DNS&lt;/th&gt;
&lt;th&gt;log_hostname = off&lt;/th&gt;
&lt;th&gt;log_hostname = on&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;answers 100 ms late&lt;/td&gt;
&lt;td&gt;14.0 ms&lt;/td&gt;
&lt;td&gt;122.6 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;does not answer&lt;/td&gt;
&lt;td&gt;14.2 ms&lt;/td&gt;
&lt;td&gt;20,036 ms (20 seconds)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;does not answer, local cache&lt;/td&gt;
&lt;td&gt;not measured - no lookup&lt;/td&gt;
&lt;td&gt;10,027 ms (10 seconds)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;One run per scenario: 30 seconds, 60 seconds for the run with DNS down and log_hostname on. One client, a new connection for every transaction, no local DNS cache unless the row says so. "DNS does not answer" means that the DNS server's answers never reached the database server, as in an outage. Connection time is pgbench's "average connection time"; in each run, the server's timing of the individual connections stayed close to this value.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With log_hostname off, DNS problems made no difference, because there was no lookup. With it on, every new connection waited for DNS. When DNS answered 100 ms late, each connection took about 109 ms longer. About 106 ms of that was the lookup itself: the 100 ms we added, on top of the 6 ms a lookup cost with healthy DNS. The remaining 2-3 ms were spread over the rest of the connection setup, and one run is not enough to explain them. When DNS did not answer, each connection waited until the resolver gave up: 20 seconds when the system resolver (glibc 2.39 on Ubuntu 24.04) asked DNS directly - it sent the question four times, five seconds apart - and 10 seconds through systemd-resolved. Resolver options can change these numbers, but not the pattern: a new connection takes as long as DNS takes to answer or to fail. With eight clients connecting at the same time and DNS down, the server completed 0.2 new connections per second. With log_hostname off and DNS healthy, the same eight clients completed 160 per second.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the lookup fails, the IP address is lost too
&lt;/h2&gt;

&lt;p&gt;These are three lines from one of those 20-second connections, with &lt;code&gt;%h&lt;/code&gt; in the log line prefix. The timestamp and process ID at the start of each line are replaced with ..., and long lines are wrapped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... [unknown]@[unknown]/[unknown]  WARNING:  pg_getnameinfo_all() failed:
      Temporary failure in name resolution
... [unknown]@[unknown]/[unknown] ??? LOG:  connection received:
      host=??? port=???
... bench@postgres/pgbench ??? LOG:  connection ready:
      setup total=20033.681 ms, fork=0.540 ms, authentication=5.641 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the lookup fails, PostgreSQL logs a WARNING and writes &lt;code&gt;???&lt;/code&gt; in place of the client's host and port. The IP address the server already had does not appear anywhere in the log. It stays visible only in &lt;code&gt;pg_stat_activity.client_addr&lt;/code&gt;, and only while the session is open. The last line also shows where the time went: 20 seconds of setup, of which authentication took 5.6 ms.&lt;/p&gt;

&lt;p&gt;The cost does not depend on what you log. In one run we used the upstream default &lt;code&gt;log_line_prefix&lt;/code&gt; and turned connection logging off, so nothing in the log could show a host name. Each connection still waited 20 seconds, and the only trace in the log was one WARNING line per connection.&lt;/p&gt;

&lt;h2&gt;
  
  
  The name in the log is not checked
&lt;/h2&gt;

&lt;p&gt;With log_hostname on, PostgreSQL logs the name from the PTR record as it receives it. For the log, it does not check whether that name leads back to the same IP address. A PTR record belongs to the IP address, not to the domain in the name: it is set by whoever manages the reverse DNS for that address, usually the owner of the network the client connects from, or that network's provider. The owner of the domain in the name has no say in it. So whoever controls the reverse DNS of the client's address decides which name your log shows - even a name that looks like a different IP address.&lt;/p&gt;

&lt;p&gt;We checked this on PostgreSQL 18.6 with a small DNS server that we controlled. We set the PTR record of the client address 127.0.0.5 to 10.0.0.5, and the PTR record of 127.0.0.6 to db-backup-01.corp.example, a name with no address behind it. These are five log lines from three connections. The log line prefix ends with &lt;code&gt;%h&lt;/code&gt;; the timestamp and process ID are replaced with ..., and long lines are wrapped:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;... [unknown]@[unknown] 10.0.0.5 LOG:  connection received:
      host=10.0.0.5 port=35803
... postgres@postgres 10.0.0.5 LOG:  connection authorized:
      user=postgres database=postgres application_name=psql
... intruder@postgres 10.0.0.5 FATAL:  no pg_hba.conf entry for
      host "127.0.0.5", user "intruder", database "postgres", no encryption
... backup@postgres db-backup-01.corp.example FATAL:  no pg_hba.conf entry
      for host "127.0.0.6", user "backup", database "postgres", no encryption
... backup@postgres db-backup-01.corp.example DETAIL:  Could not translate
      client host name "db-backup-01.corp.example" to IP address: Temporary
      failure in name resolution.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every prefix and the "connection received" message show the name from the PTR record. In this test the real address appeared only in the text of a FATAL message, which PostgreSQL writes when it refuses a connection, and in &lt;code&gt;pg_stat_activity.client_addr&lt;/code&gt; while a session was open.&lt;/p&gt;

&lt;p&gt;PostgreSQL itself does not trust such a name for access control. A host name in &lt;code&gt;pg_hba.conf&lt;/code&gt; matches only when the name also resolves back to the client's address, as the &lt;a href="https://www.postgresql.org/docs/18/auth-pg-hba-conf.html" rel="noopener noreferrer"&gt;PostgreSQL 18 documentation&lt;/a&gt; describes. That is why the last connection was refused: our &lt;code&gt;pg_hba.conf&lt;/code&gt; line for db-backup-01.corp.example did not match, because the name had no address. The log prefix showed the name anyway.&lt;/p&gt;

&lt;p&gt;For an investigation, this means that a host name in the log is what the reverse DNS said, not proof of where a connection came from. With log_hostname off, &lt;code&gt;%h&lt;/code&gt; logs the IP address the connection came from. If you need a name later, look the address up, and check that the name resolves back to the same address - the check that &lt;code&gt;pg_hba.conf&lt;/code&gt; makes and the log does not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connection pools hide the cost until they reconnect
&lt;/h2&gt;

&lt;p&gt;The lookup happens once per connection, not once per query. In one 30-second run per setting with eight connections that stayed open, the server answered 19,291 queries per second with log_hostname off and 19,165 with it on. One run per setting cannot show whether a 0.7% difference is real, but the mechanism explains why it should be small: once a connection exists, it makes no more lookups.&lt;/p&gt;

&lt;p&gt;This is why a connection pool can hide the setting for a long time. The cost comes back when the pool opens new connections - for example after a database restart or a failover, when every connection in the pool is opened again at about the same time, and each one makes its own lookup.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to check your server
&lt;/h2&gt;

&lt;p&gt;Connect to your database with psql or any other SQL client you normally use, and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;source&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_settings&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'log_hostname'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'log_line_prefix'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'log_connections'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;log_hostname is off?&lt;/strong&gt; There is no lookup and nothing to change. If you want the client address in the log, add &lt;code&gt;%h&lt;/code&gt; to &lt;code&gt;log_line_prefix&lt;/code&gt;, as shown below.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;log_hostname is on?&lt;/strong&gt; Check whether anything uses the names: the log line prefix (&lt;code&gt;%h&lt;/code&gt; or &lt;code&gt;%r&lt;/code&gt;), connection logging, or reports that read &lt;code&gt;client_hostname&lt;/code&gt; from &lt;code&gt;pg_stat_activity&lt;/code&gt;. If nothing does, the setting costs a lookup per connection and shows nothing in return. If something does, weigh what the names give you against the waiting time described above.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To turn it off - in an SQL session, the same way as above, connected as a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;SYSTEM&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;log_hostname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;off&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;pg_reload_conf&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The change needs a reload, not a restart, and applies to connections opened after the reload. To get the client address on every log line, add &lt;code&gt;%h&lt;/code&gt; to &lt;code&gt;log_line_prefix&lt;/code&gt; after &lt;code&gt;%q&lt;/code&gt;. &lt;code&gt;%q&lt;/code&gt; itself prints nothing: it tells background processes, which have no client, to stop at that point of the prefix. For example, in an SQL session as a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;SYSTEM&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;log_line_prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'%m [%p] %q%u@%d/%a %h '&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;pg_reload_conf&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prefix only demonstrates where &lt;code&gt;%h&lt;/code&gt; goes; it is not a recommendation for your whole prefix. Keep the prefix you have and add &lt;code&gt;%h&lt;/code&gt; to it, and update every tool that reads your log - log analyzers, alert rules - so it knows the new format.&lt;/p&gt;

&lt;p&gt;To confirm the result, open a new connection and check that its log lines show the IP address in the prefix. On PostgreSQL 18 you can also measure connection setup on your own server: when &lt;code&gt;log_connections&lt;/code&gt; includes &lt;code&gt;setup_durations&lt;/code&gt;, the server logs "connection ready: setup total=..." for every new connection. That is one more log line per connection, so use it for a measurement window, not forever, on servers that open many connections.&lt;/p&gt;

&lt;p&gt;On managed services, PostgreSQL settings live in the provider's console - a parameter group on Amazon RDS, server parameters on Azure Database for PostgreSQL, database flags on Google Cloud SQL - and the provider decides which of them you can change. Check what your server actually runs with the query above.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you need host names
&lt;/h2&gt;

&lt;p&gt;Sometimes a name is useful - for example, when client IP addresses change often and you keep logs for a long time. In that case, resolve the addresses when you read the log, not while clients wait to connect: an IP address in the log can be looked up later, during an investigation. Keep in mind that the answer then reflects DNS at the time you ask, not at the time of the connection.&lt;/p&gt;

&lt;p&gt;Two more notes. Behind a connection pooler, &lt;code&gt;%h&lt;/code&gt; shows the pooler's address, whatever log_hostname says. And to see which application a log line belongs to, use &lt;code&gt;application_name&lt;/code&gt;: the client sets it, &lt;code&gt;%a&lt;/code&gt; prints it, and it needs no lookup.&lt;/p&gt;

&lt;h2&gt;
  
  
  How we tested
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Two Azure virtual machines in the Poland Central region, in their own private network: the database server on Standard_D4s_v5 (Ubuntu 24.04, glibc 2.39, PostgreSQL 18.6 from the PostgreSQL project's packages) and the client on Standard_D2s_v5. PostgreSQL settings as installed by the packages, apart from the log line prefix, connection logging, log file handling and the listen address. Client-side numbers come from pgbench 18.6; server-side numbers from the PostgreSQL 18 log message "connection ready: setup total=..." of the benchmark's own connections. Slow DNS: 100 ms of delay added to every packet from the database server to Azure DNS (tc netem). DNS down: Azure DNS answers dropped at the database server's firewall (iptables), so the questions left and no answer came back. The healthy-DNS variants ran three times each, in alternating order; the slow, failing, eight-client and persistent-connection scenarios ran once each. The local DNS cache was emptied before every run. The test of unchecked names ran separately, on one Linux machine: PostgreSQL 18.6 and a dnsmasq DNS server in an isolated network namespace, with the system resolver set to use DNS only and clients connecting from 127.0.0.5 and 127.0.0.6.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnquvm8gfybzehgeqnws0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnquvm8gfybzehgeqnws0.webp" alt="Animation in five rows. Each row shows a client, the PostgreSQL server and a DNS server. Off: %h prints the client's IP address and there is no DNS lookup; 14 ms per connection. On: one reverse DNS lookup for each new connection, even if no log line prints %h, and %h prints the name; 20 ms per connection. DNS down: each new connection waits until DNS gives up, 20 seconds, and %h prints ???. Connection pool: one lookup when a connection opens, none for each query; after a failover, every connection asks again. Reload: a session that is already open keeps the old %h, and only new sessions print the IP address." width="720" height="860"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The cases from this article in one picture. Times are from our test: pgbench -C, no local DNS cache.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;%h&lt;/code&gt; in &lt;code&gt;log_line_prefix&lt;/code&gt; logs the client address. It needs no lookup.&lt;/li&gt;
&lt;li&gt;log_hostname changes what &lt;code&gt;%h&lt;/code&gt; prints, from the IP address to a name from DNS. It costs one DNS lookup per new connection, also when nothing prints the name.&lt;/li&gt;
&lt;li&gt;When DNS is slow, every new connection is slow. When DNS is down, every new connection waits until the resolver gives up, and the log shows &lt;code&gt;???&lt;/code&gt; instead of the address.&lt;/li&gt;
&lt;li&gt;The name log_hostname writes to the log is not checked. Whoever controls the reverse DNS of the client's address chooses it.&lt;/li&gt;
&lt;li&gt;Leaving log_hostname off, the default, avoids all of this. If you need names, resolve them when you read the log.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;This article was originally published on the &lt;a href="https://guardioz.com/blog/log-hostname-explained" rel="noopener noreferrer"&gt;Guardioz blog&lt;/a&gt;. Guardioz makes PostgreSQL tuning end in a decision, not a research project. It is in beta and always free for one user: &lt;a href="https://guardioz.com" rel="noopener noreferrer"&gt;guardioz.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>performance</category>
      <category>devops</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Wojtek Pastyrczyk</dc:creator>
      <pubDate>Wed, 04 Nov 2026 08:17:38 +0000</pubDate>
      <link>https://dev.to/wojtekpastyr/-994</link>
      <guid>https://dev.to/wojtekpastyr/-994</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/guardioz/maxwalsize-explained-484h" class="crayons-story__hidden-navigation-link"&gt;max_wal_size, explained&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;
          &lt;a class="crayons-logo crayons-logo--l" href="/guardioz"&gt;
            &lt;img alt="Guardioz logo" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F15820%2F53d5daa9-9d1f-40db-9e59-8d721ea6a4a8.png" class="crayons-logo__image" width="721" height="516"&gt;
          &lt;/a&gt;

          &lt;a href="/wojtekpastyr" class="crayons-avatar  crayons-avatar--s absolute -right-2 -bottom-2 border-solid border-2 border-base-inverted  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4224502%2Fc9e6a3db-db48-4cfb-9155-66837be174f7.png" alt="wojtekpastyr profile" class="crayons-avatar__image" width="420" height="420"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/wojtekpastyr" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Wojtek Pastyrczyk
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Wojtek Pastyrczyk
                
                
              
              &lt;div id="story-author-preview-content-4970085" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/wojtekpastyr" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4224502%2Fc9e6a3db-db48-4cfb-9155-66837be174f7.png" class="crayons-avatar__image" alt="" width="420" height="420"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Wojtek Pastyrczyk&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

            &lt;span&gt;
              &lt;span class="crayons-story__tertiary fw-normal"&gt; for &lt;/span&gt;&lt;a href="/guardioz" class="crayons-story__secondary fw-medium"&gt;Guardioz&lt;/a&gt;
            &lt;/span&gt;
          &lt;/div&gt;
          &lt;a href="https://dev.to/guardioz/maxwalsize-explained-484h" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Nov 3&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/guardioz/maxwalsize-explained-484h" id="article-link-4970085"&gt;
          max_wal_size, explained
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/postgres"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;postgres&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/database"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;database&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/performance"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;performance&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/devops"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;devops&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/guardioz/maxwalsize-explained-484h#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            8 min read
          &lt;/small&gt;
          
          &lt;div class="only-sidebar-menu-item trusted-visible-block print-hidden align-center"&gt;
                
                    
                
          &lt;/div&gt;

        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
    </item>
    <item>
      <title>max_wal_size, explained</title>
      <dc:creator>Wojtek Pastyrczyk</dc:creator>
      <pubDate>Tue, 03 Nov 2026 15:34:25 +0000</pubDate>
      <link>https://dev.to/guardioz/maxwalsize-explained-484h</link>
      <guid>https://dev.to/guardioz/maxwalsize-explained-484h</guid>
      <description>&lt;p&gt;&lt;em&gt;max_wal_size gives PostgreSQL's transaction log a size budget. Some write-heavy servers outgrow the 1 GB default - and when that happens, PostgreSQL is forced to write the same data to disk again and again. We explain what the setting really does, how to reduce that wasted disk work using your server's own counters, and when to leave it alone.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The setting that is easy to overlook
&lt;/h2&gt;

&lt;p&gt;Every busy PostgreSQL server writes more to disk than the application asks it to. Some of that extra work is normal and healthy. But on write-heavy systems, part of it can be repeated, avoidable work - and how much of that happens is heavily influenced by one setting: &lt;code&gt;max_wal_size&lt;/code&gt;. Nothing in daily operation points at it, and the advice that is easy to find online is generic - a fixed number, or a calculator that cannot see your workload. PostgreSQL ships a general-purpose upstream default of 1 GB - the project cannot know how large a server it will run on - and a genuinely busy server can produce that much transaction log in under a minute.&lt;/p&gt;

&lt;p&gt;To see why that matters, you need two concepts. Both are simpler than they sound.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two ideas: the transaction log, and the checkpoint
&lt;/h2&gt;

&lt;p&gt;Before PostgreSQL changes anything in your data files - the files on disk that hold your tables and indexes - it first writes a description of the change to its &lt;strong&gt;transaction log&lt;/strong&gt; (in PostgreSQL language: the WAL, or write-ahead log). The log is the database's insurance against crashes. If the server loses power in the middle of the work, PostgreSQL replays the log on startup and no committed data is lost.&lt;/p&gt;

&lt;p&gt;The log cannot grow forever. So from time to time PostgreSQL saves its progress: it takes all the modified data pages - the 8 KB blocks PostgreSQL stores your tables in - that it holds in memory, and writes them out to the data files. This is a &lt;strong&gt;checkpoint&lt;/strong&gt;. After a checkpoint completes, the log written before it is no longer needed for crash recovery, and PostgreSQL can reuse its disk space - as soon as backups and replication no longer need that part of the log either.&lt;/p&gt;

&lt;p&gt;In normal operation, a checkpoint starts automatically for one of two reasons, whichever comes first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a timer - every 5 minutes by default (&lt;code&gt;checkpoint_timeout&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;volume - the log since the last checkpoint approaches its size budget, 1 GB by default (&lt;code&gt;max_wal_size&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On a quiet server the timer always wins, and there is nothing to tune. On a busy one, 1 GB fills quickly - and that is where the real cost begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why frequent checkpoints are so expensive
&lt;/h2&gt;

&lt;p&gt;Here is the less obvious part. After each checkpoint, the first change to any data page is logged in full: with PostgreSQL's default &lt;code&gt;full_page_writes&lt;/code&gt; setting, the entire 8 KB page is copied into the transaction log, not just the few bytes that changed. This is intentional - it protects you from partially written pages if the server crashes during a write.&lt;/p&gt;

&lt;p&gt;Now combine the two mechanisms. Each checkpoint resets that rule: after it, the first change to each page is logged in full again. When checkpoints run every few seconds, frequently changed pages are copied into the log in full over and over. That inflated log reaches its 1 GB budget even sooner, which forces the next checkpoint even earlier. The server ends up writing the same busy pages to disk again and again, and logging more data than the workload truly requires.&lt;/p&gt;

&lt;p&gt;PostgreSQL does warn you: it prints &lt;a href="https://www.postgresql.org/docs/18/wal-configuration.html" rel="noopener noreferrer"&gt;"checkpoints are occurring too frequently"&lt;/a&gt; into the server log. It is easy to miss unless you already watch the server log. And the warning has a blind spot: by default it only appears when checkpoints are less than 30 seconds apart. A checkpoint every 40 seconds is still many times more frequent than the five-minute schedule, but the log stays silent.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this looks like in numbers
&lt;/h2&gt;

&lt;p&gt;We test every Guardioz tuning rule in an internal benchmark environment before we trust it with anyone's database. Here is one of those experiments: the same 30-minute run of pgbench, the benchmark tool that ships with PostgreSQL, using its default scenario - loosely based on TPC-B, with most statements being writes - repeated on PostgreSQL 18 with four different values of &lt;code&gt;max_wal_size&lt;/code&gt;. The experiment compares PostgreSQL's own counters of transaction log volume, data-file write volume, and checkpoints started ahead of the timer - PostgreSQL counts those as "requested" checkpoints. It is not a storage-performance comparison. And because each run completes a different number of transactions, the write volumes are shown per completed transaction, so the settings can be compared fairly.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;max_wal_size&lt;/th&gt;
&lt;th&gt;Requested checkpoints&lt;/th&gt;
&lt;th&gt;Transaction log per completed transaction&lt;/th&gt;
&lt;th&gt;Data-file write volume per completed transaction&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1 GB (the default)&lt;/td&gt;
&lt;td&gt;189&lt;/td&gt;
&lt;td&gt;14.51 KiB (baseline)&lt;/td&gt;
&lt;td&gt;15.04 KiB (baseline)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8 GB&lt;/td&gt;
&lt;td&gt;19&lt;/td&gt;
&lt;td&gt;14.24 KiB (-1.8%)&lt;/td&gt;
&lt;td&gt;14.35 KiB (-4.6%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;32 GB&lt;/td&gt;
&lt;td&gt;0 (range: 0-1)&lt;/td&gt;
&lt;td&gt;13.74 KiB (-5.3%)&lt;/td&gt;
&lt;td&gt;13.99 KiB (-6.9%)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;128 GB&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;13.64 KiB (-6.0%)&lt;/td&gt;
&lt;td&gt;13.97 KiB (-7.1%)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Median results from three 30-minute pgbench runs per setting: one 194 GB database (scale 13 300), 64 concurrent clients, PostgreSQL 18, 30 GB of RAM and local SSD storage. For each run, transaction log and data-file write volumes were divided by the number of completed transactions before taking the median. Log volume measured with &lt;code&gt;pg_stat_wal&lt;/code&gt;, data-file write volume with &lt;code&gt;pg_stat_io&lt;/code&gt;, checkpoints with &lt;code&gt;pg_stat_checkpointer&lt;/code&gt;. KiB is 1024 bytes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;max_wal_size&lt;/code&gt; at 1 GB, the server recorded a median of 189 requested checkpoints per 30-minute run - on this isolated benchmark server, all of them demanded by log growth: roughly one every ten seconds instead of one every five minutes. At 32 GB the median fell to zero, with a single requested checkpoint in one of the three runs. And after normalizing for completed work, PostgreSQL generated 5.3% less transaction log and recorded 6.9% less data-file write volume per transaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;max_wal_size = 1 GB (the default)  ||||||||||||||||||||||||||||||||  about 32 checkpoints
max_wal_size = 32 GB (tuned)       ·······························|  1 checkpoint
                                   0 min                      5 min
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Five-minute illustration derived from the median checkpoint rates above; it is not a trace of an individual run.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Raising the setting from 32 GB to 128 GB changed these counters only slightly. Once frequent requested checkpoints had disappeared, a larger limit produced little additional benefit in this experiment, while increasing the worst case for disk usage and recovery time. The goal is enough, not maximum.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trade-off
&lt;/h2&gt;

&lt;p&gt;A larger &lt;code&gt;max_wal_size&lt;/code&gt; has a real price. Disk space: PostgreSQL does not reserve the space up front, but under heavy write load the log can genuinely grow to the limit you set - so plan for it. Recovery time: after a crash, PostgreSQL replays the log written since the last checkpoint, so checkpoints spaced further apart can make that restart take longer. For most systems this is an acceptable trade - but make the choice on purpose, not by accident.&lt;/p&gt;

&lt;p&gt;Also know that &lt;code&gt;max_wal_size&lt;/code&gt; is a soft limit. The log can grow beyond it: under heavy load, when transaction log archiving (copying the transaction log to backup storage) is failing, or when the server is told to keep the log around for a replica that has fallen behind or disappeared (a replication slot, or a high &lt;code&gt;wal_keep_size&lt;/code&gt;). Never size it so close to disk capacity that going past the limit could fill the disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to tune it yourself
&lt;/h2&gt;

&lt;p&gt;PostgreSQL exposes separate counters for checkpoints scheduled by the timer and checkpoints requested for other reasons - comparing them is where the diagnosis starts. Connect to your database with psql or any other SQL client you normally use, and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;num_timed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num_requested&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stats_reset&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;pg_stat_checkpointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(On PostgreSQL 16 and older, the same counters are &lt;code&gt;checkpoints_timed&lt;/code&gt; and &lt;code&gt;checkpoints_req&lt;/code&gt; in &lt;code&gt;pg_stat_bgwriter&lt;/code&gt;.) Two things to know: the counters cover the whole PostgreSQL instance - every database in it, not only the one you are connected to - and they add up from the last statistics reset, whose time the &lt;code&gt;stats_reset&lt;/code&gt; column shows. Take one reading immediately before a representative busy period and another immediately after it, then compare the differences.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nearly all timed?&lt;/strong&gt; Leave the setting alone - even the 1 GB default is correct for that server. A change without a signal has nothing to gain, and something to risk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Many requested?&lt;/strong&gt; That is often log-size pressure, but not only: manual &lt;code&gt;CHECKPOINT&lt;/code&gt; commands, backups starting, and a few other events are counted here too. Confirm the cause first - with &lt;code&gt;log_checkpoints&lt;/code&gt; enabled (the default since PostgreSQL 15), the server log names it for every checkpoint, and &lt;code&gt;checkpoint starting: wal&lt;/code&gt; means log size. When log size is the cause, raise &lt;code&gt;max_wal_size&lt;/code&gt; and check the counters again. The widely used rule among PostgreSQL experts is to raise it until checkpoints are almost always started by the timer - but never further than your disk can safely absorb. The log must have room to grow to the new limit, and somewhat past it (the limit is soft), and the disk still needs a healthy reserve of free space on top of that.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Counters jump only during a nightly import or a restore?&lt;/strong&gt; Then do not tune the whole server around a single short burst. If that job's speed genuinely matters to you, treat it as its own task; for everything else, tune for how the server works on a normal day.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The change itself is cheap: no restart, no downtime. For example, to set 8 GB - again in an SQL session, this time connected as a superuser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;SYSTEM&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;max_wal_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'8GB'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;pg_reload_conf&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 8 GB here only demonstrates the command - it is not a recommended value. The right number for your server comes from its own transaction log volume, and you confirm it the same way you found the problem: read the counters again over a busy period.&lt;/p&gt;

&lt;p&gt;On managed services it is the same setting in a different place - a parameter group on Amazon RDS, server parameters on Azure Database for PostgreSQL, database flags on Google Cloud SQL - and all three apply it without a restart. Some of them also pick their own starting value - Azure, for example, sizes it from your disk - so check what your server actually runs: open an SQL session, the same way as above, and run &lt;code&gt;SHOW max_wal_size;&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  On cloud storage, these writes are on your bill
&lt;/h2&gt;

&lt;p&gt;On your own hardware, avoidable writes use up disk performance you need for real work, and they wear the disks. On cloud storage they can also cost you money, because cloud disks are billed for &lt;strong&gt;provisioned&lt;/strong&gt; performance: you reserve a ceiling of disk operations per second (IOPS), and you pay for that reservation every month whether you use it or not. AWS (gp3, io1, io2), Azure (Premium SSD, Premium SSD v2, Ultra) and Google Cloud all price disk performance this way, in slightly different shapes - included baselines, per-IOPS charges, or IOPS that grow with the disk size you buy.&lt;/p&gt;

&lt;p&gt;A database with frequent checkpoints keeps its disks busier than the workload requires, so the performance ceiling you reserve - and pay for - has to cover wasted writes too. Reduce them, then measure the workload again: when the need has dropped, the same database can fit into a smaller tier, or inside the baseline you already pay for. The saving arrives when you lower what you provision, not by itself. This is not only our view: Microsoft's own &lt;a href="https://learn.microsoft.com/en-us/azure/postgresql/troubleshoot/how-to-high-io-utilization" rel="noopener noreferrer"&gt;troubleshooting guide for high I/O&lt;/a&gt; on Azure PostgreSQL recommends exactly this - raise &lt;code&gt;max_wal_size&lt;/code&gt; until checkpoints are started by the timer.&lt;/p&gt;

&lt;p&gt;One exception worth knowing: Amazon Aurora replaces PostgreSQL's storage layer, so the write mechanics and the tuning procedure described here do not carry over to it directly - what &lt;code&gt;max_wal_size&lt;/code&gt; does there depends on your Aurora engine version, so check its documentation and parameter group. Everything else in this article applies to RDS for PostgreSQL, Azure, Cloud SQL, and any server you run yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  You do not have to do this by hand
&lt;/h2&gt;

&lt;p&gt;Everything above is what Guardioz automates. It starts with measurement: how much transaction log your server produces under its normal, everyday load, and whether checkpoints are being forced ahead of schedule by that log growth. When the measurements say the setting is fine, Guardioz tells you exactly that - no change needed. When there is a real signal, it recommends a &lt;code&gt;max_wal_size&lt;/code&gt; fitted to what your server actually does - not a calculator average - ignores one-off bursts, and first checks that the data disk keeps a safe reserve of free space. The recommendation arrives with the evidence next to it, and applies without a restart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your PostgreSQL deserves better.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article was originally published on the &lt;a href="https://guardioz.com/blog/max-wal-size-explained" rel="noopener noreferrer"&gt;Guardioz blog&lt;/a&gt;. Guardioz makes PostgreSQL tuning end in a decision, not a research project. It is in beta and always free for one user: &lt;a href="https://guardioz.com" rel="noopener noreferrer"&gt;guardioz.com&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>performance</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
