server_version and server_version_num are one fact in two formats: which PostgreSQL this is, as a string for people and as an integer for programs. You cannot set either. Nearly all the trouble they cause comes from handing a program the one meant for people, or from forgetting what “this” means: the postmaster that is running right now, whatever the package manager believes is installed. After the November 12, 2026 minor releases, that second mistake is enough to stop PL/pgSQL from loading.

Both are presets, like block_size: the context is internal, and the values are compiled into the server executable. server_version is the build’s PG_VERSION string. server_version_num is PG_VERSION_NUM, which since PostgreSQL 10 is the major version times 10,000 plus the minor, so 18.7 is 180007. (Before 10, versions had three parts and 9.6.24 was 90624, which still sorts below 100000.) pg_settings lists the integer with a min_val and a max_val both equal to itself, a range of exactly one. SET, ALTER SYSTEM, and ALTER ROLE ... SET all get parameter "server_version_num" cannot be changed. Put the line in postgresql.conf and a reload logs the same complaint and carries on; the next restart logs it again, follows it with a FATAL about errors in the configuration file, and the server stays down, so do not leave one there as a joke.

The string looks like a number until you meet one in production:

1=> SHOW server_version;
2 18.7 (Ubuntu 18.7-1.pgdg24.04+5)
3
4=> SELECT current_setting('server_version')::numeric;
5ERROR: invalid input syntax for type numeric: "18.7 (Ubuntu 18.7-1.pgdg24.04+5)"
6
7=> SELECT '17.12' >= '17.9' AS patched, '9.6.24' >= '10.0' AS modern;
8 patched | modern
9---------+--------
10 f | t

Debian and Ubuntu packages append their own package version to it, which is what configure --with-extra-version is for. Prereleases are not numeric at all; the last release candidate of 19 called itself 19rc2. And comparing version strings as text works right up until a branch’s minor number reaches two digits, which every supported branch except 18 and 19 has now done. server_version_num arrived in 8.2, in 2006, so that nobody would have to write that parser. Twenty years is long enough to stop writing it.

The comparison you want is current_setting('server_version_num')::int >= 180007. In psql the value is already sitting in :SERVER_VERSION_NUM, although \if accepts only a boolean, so the arithmetic has to make a round trip: SELECT :SERVER_VERSION_NUM >= 180007 AS patched \gset, then \if :patched.

The integer has blind spots of its own. 19rc2 and 19.0 are both 190000, and so was every beta; two rebuilds of the same minor by the same packager are identical as well. Those are the jobs left for the string. (version() is a third thing: the string plus platform and compiler, and the one a bug report wants.)

The number your driver already has

server_version is one of the parameters (fifteen of them, as of PostgreSQL 18) that the server reports to every client at connection start, through the ParameterStatus machinery described under in_hot_standby. server_version_num is not on that list. libpq builds the integer itself, by running sscanf() over the string, and hands it out as PQserverVersion(). That is where psql gets its variables, where psycopg gets connection.info.server_version, and, by way of psycopg, where Django gets the number it uses to decide which features the server has. Nobody ran a query.

So that number is what the connection was told, and a pooler can tell it something old. PgBouncer keeps the startup parameters from the first server connection in each pool and replays them to every client that arrives afterward. I put PgBouncer 1.26.1 in front of an 18.7 server, connected through it, then stopped that server and started a 19.0 cluster on the same port, which is what an in-place pg_upgrade looks like from the pooler’s side. A brand-new client connection got this:

1ParameterStatus server_version : 18.7 (Ubuntu 18.7-1.pgdg24.04+5)
2PQserverVersion() : 180007
3current_setting('server_version_num') : 190000

A minor update behind the pooler does the same thing on a smaller scale. RELOAD did not clear it. RECONNECT on the admin console did, and so does restarting PgBouncer; put one of those in the upgrade runbook. When the version decides something you care about, ask the backend with SHOW. The handshake is hearsay.

Installed is not running

Because the values are compiled into the postmaster, they describe the process and say nothing about the files. Update the packages under a running server and everything on disk agrees that it is 18.7: dpkg, postgres --version, pg_config. The server goes on answering 18.6, correctly, and on Linux /proc/<pid>/exe shows the binary it is running as (deleted).

Most of the time, the cost of that state is that you do not have the fixes you think you have. The November 12 releases (18.7, 17.12, 16.16, 15.20, and 14’s last, 14.25) made it more expensive. Their security fixes added functions to the server executable, and the new plpgsql.so, hstore.so, postgres_fdw.so, dblink.so, and libpqwalreceiver.so call them. An old postmaster forks a new backend, the backend loads the new library from disk, and:

1=> SHOW server_version;
2 18.6 (Ubuntu 18.6-1.pgdg24.04+2)
3
4=> DO $$ BEGIN PERFORM 1; END $$;
5ERROR: could not load library "/usr/lib/postgresql/18/lib/plpgsql.so": /usr/lib/postgresql/18/lib/plpgsql.so: undefined symbol: heap_datum_update_type

Lukas Fittl wrote this up the day the releases came out. A few things from my own testing on 18.6 and 18.7: plain SQL is fine. A session that loaded PL/pgSQL before the update keeps working, until one of its queries wants a parallel worker, which is a new process and fails the same way. So the errors trickle in as the connection pool turns over, which is a memorable way to spend an afternoon. A standby in the same state keeps streaming until its walreceiver has to reconnect. After that it logs a FATAL about libpqwalreceiver.so every five seconds and receives nothing.

This is an old failure with a new symbol name. The February 2014 security releases set the same trap with a function called CheckFunctionValidatorAccess; a bug report arrived the same day, and Tom Lane answered it with the question that still applies: “Maybe you installed 9.3.3 but forgot to restart the server?”

A stock PGDG package install mostly protects you, because the package restarts the service it manages as part of the update. The apt packages stop the cluster, replace the files, and start it again (its own kind of surprise, if you ran apt upgrade at two in the afternoon). The RPMs replace the files and then try-restart the unit, which leaves a brief window in exactly this state. The lasting exposure is every postmaster the package does not manage: one that Patroni started, one behind a unit file you wrote, one launched by hand. The common rolling procedure, packages onto every node first and restarts afterward at leisure, puts each node in this state from the moment of install until its turn comes. Install and restart one node at a time.

To find a server in this state, ask it and compare the answer with the files. The psql that arrived with the update has been doing that for you all along: its banner prints both versions whenever its own and the server’s differ, as in psql (18.7, server 18.6) plus whatever the packager appended, and most of us stopped reading the banner years ago. Each Patroni member’s REST API reports server_version, taken from Patroni’s own connection to the postmaster. And since nothing in the data directory records a minor version (PG_VERSION holds the major; pg_control holds the catalog and control-file versions), the only history of which minor ran when is the starting PostgreSQL 18.7 ... line each postmaster writes to its log on the way up.

Inventory server_version_num from the servers themselves, never from the package manager, and treat a disagreement between the two as an outage that has not been scheduled yet. Compare it as an integer. The string is for reading.