Subject: pgsql: Replace BackendIds with 0-based ProcNumbers

From: Heikki Linnakangas <[email-masked]>
Date: Sun, 03 Mar 2024 17:39:48 +0000

Replace BackendIds with 0-based ProcNumbers

Now that BackendId was just another index into the proc array, it was
redundant with the 0-based proc numbers used in other places. Replace
all usage of backend IDs with proc numbers.

The only place where the term "backend id" remains is in a few pgstat
functions that expose backend IDs at the SQL level. Those IDs are now
in fact 0-based ProcNumbers too, but the documentation still calls
them "backend ids". That term still seems appropriate to describe what
the numbers are, so I let it be.

One user-visible effect is that pg_temp_0 is now a valid temp schema
name, for backend with ProcNumber 0.

Reviewed-by: Andres Freund
Discussion: https://www.postgresql.org/message-id/[email-masked]

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/024c521117579a6d356050ad3d78fdc95e44eefa

Modified Files
--------------
doc/src/sgml/config.sgml                           |  2 +-
doc/src/sgml/monitoring.sgml                       | 18 ++---
doc/src/sgml/storage.sgml                          |  2 +-
doc/src/sgml/xact.sgml                             |  4 +-
src/backend/access/transam/README                  | 10 +--
src/backend/access/transam/clog.c                  | 18 ++---
src/backend/access/transam/multixact.c             | 64 ++++++++--------
src/backend/access/transam/parallel.c              | 10 +--
src/backend/access/transam/twophase.c              | 43 ++++++-----
src/backend/access/transam/xact.c                  |  9 ++-
src/backend/access/transam/xlogprefetcher.c        |  2 +-
src/backend/access/transam/xlogutils.c             |  6 +-
src/backend/backup/basebackup_incremental.c        |  2 +-
src/backend/catalog/catalog.c                      | 14 ++--
src/backend/catalog/namespace.c                    | 30 ++++----
src/backend/catalog/storage.c                      | 30 ++++----
src/backend/commands/async.c                       | 89 ++++++++++------------
src/backend/commands/dbcommands.c                  |  2 +-
src/backend/commands/indexcmds.c                   |  2 +-
src/backend/commands/sequence.c                    |  2 +-
src/backend/libpq/pqmq.c                           | 10 +--
src/backend/postmaster/pgarch.c                    | 12 +--
src/backend/postmaster/walsummarizer.c             | 16 ++--
.../replication/logical/applyparallelworker.c      |  4 +-
src/backend/replication/slot.c                     |  2 +-
src/backend/replication/walsender.c                |  2 +-
src/backend/storage/buffer/buf_init.c              |  2 +-
src/backend/storage/buffer/bufmgr.c                | 30 ++++----
src/backend/storage/buffer/localbuf.c              |  6 +-
src/backend/storage/ipc/procarray.c                | 44 +++++------
src/backend/storage/ipc/procsignal.c               | 36 ++++-----
src/backend/storage/ipc/sinvaladt.c                | 45 +++++------
src/backend/storage/ipc/standby.c                  |  6 +-
src/backend/storage/lmgr/lmgr.c                    |  2 +-
src/backend/storage/lmgr/lock.c                    | 26 +++----
src/backend/storage/lmgr/predicate.c               | 10 +--
src/backend/storage/lmgr/proc.c                    | 39 +++++-----
src/backend/storage/smgr/md.c                      |  4 +-
src/backend/storage/smgr/smgr.c                    |  2 +-
src/backend/utils/activity/backend_status.c        | 50 ++++++------
src/backend/utils/adt/dbsize.c                     | 14 ++--
src/backend/utils/adt/lockfuncs.c                  | 13 ++--
src/backend/utils/adt/mcxtfuncs.c                  |  7 +-
src/backend/utils/adt/pgstatfuncs.c                | 50 ++++++------
src/backend/utils/cache/inval.c                    |  4 +-
src/backend/utils/cache/relcache.c                 | 30 ++++----
src/backend/utils/error/csvlog.c                   |  4 +-
src/backend/utils/error/elog.c                     |  6 +-
src/backend/utils/error/jsonlog.c                  |  4 +-
src/backend/utils/init/globals.c                   |  6 +-
src/backend/utils/time/snapmgr.c                   |  6 +-
src/common/relpath.c                               | 24 +++---
src/include/access/twophase.h                      |  2 +-
src/include/catalog/namespace.h                    |  3 +-
src/include/common/relpath.h                       |  4 +-
src/include/libpq/pqmq.h                           |  2 +-
src/include/miscadmin.h                            |  5 --
src/include/postmaster/postmaster.h                |  2 +-
src/include/storage/backendid.h                    | 41 ----------
src/include/storage/lock.h                         | 18 ++---
src/include/storage/proc.h                         | 36 ++++-----
src/include/storage/procarray.h                    |  8 +-
src/include/storage/proclist.h                     | 38 ++++-----
src/include/storage/proclist_types.h               | 18 +++--
src/include/storage/procnumber.h                   | 43 +++++++++++
src/include/storage/procsignal.h                   |  4 +-
src/include/storage/relfilelocator.h               | 29 +++----
src/include/storage/sinval.h                       |  4 +-
src/include/storage/smgr.h                         |  2 +-
src/include/utils/backend_status.h                 | 14 ++--
src/include/utils/rel.h                            |  2 +-
71 files changed, 571 insertions(+), 579 deletions(-)

---

From: Thomas Munro <[email-masked]>
Date: Wed, 10 Jul 2024 06:10:09 +1200

--- a/src/backend/libpq/pqmq.c
+++ b/src/backend/libpq/pqmq.c
@@ -26,7 +26,7 @@
 static shm_mq_handle *pq_mq_handle;
 static bool pq_mq_busy = false;
 static pid_t pq_mq_parallel_leader_pid = 0;
-static pid_t pq_mq_parallel_leader_backend_id = InvalidBackendId;
+static pid_t pq_mq_parallel_leader_proc_number = INVALID_PROC_NUMBER;

I guess it was a historical mistake that this variable of type pid_t
instead of BackendId, but now it should be ProcNumber?

---

From: Heikki Linnakangas <[email-masked]>
Date: Mon, 15 Jul 2024 11:13:16 +0300

> +++ b/src/backend/libpq/pqmq.c

Yep, fixed thanks!

-- 
Heikki Linnakangas
Neon (https://neon.tech)

---

From: Peter Eisentraut <[email-masked]>
Date: Sun, 29 Dec 2024 18:43:54 +0100

> Replace BackendIds with 0-based ProcNumbers

There are a couple of declarations that are duplicated between 
src/include/storage/proc.h and src/include/storage/procnumber.h, such as 
MyProcNumber, ParallelLeaderProcNumber, and 
ProcNumberForTempRelations().  Is that intentional, or should that be fixed?

---

From: Heikki Linnakangas <[email-masked]>
Date: Wed, 1 Jan 2025 23:57:23 +0200

On 29/12/2024 19:43, Peter Eisentraut wrote:
> There are a couple of declarations that are duplicated between src/ 
> include/storage/proc.h and src/include/storage/procnumber.h, such as 

It was not intentional. At quick glance, I think we should keep the 
declarations in procnumber.h and remove the duplicates from proc.h. I 
will do that next week when I'm back from vacation. Or feel free to do 
it yourself before that if you want. Thanks for noticing!

- Heikki