Cherry-pick https://github.com/postgres/postgres/commit/2e4229691c6 to https://github.com/postgres/postgres/commit/dfb5ad7cf0d