Have BackendXidGetPid return pid_t

Started by Sami Imseihabout 2 months ago2 messages
#1Sami Imseih
samimseih@gmail.com
1 attachment(s)

Hi,

While looking at something nearby, I noticed that BackendXidGetPid()
uses an "int" for "pid", where it should be used "pid_t" like in other
places in the code including in procarray.c.

Here is a small patch to fix this.

Thanks,

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

0001-Change-BackendXidGetPid-return-type-to-pid_t.patchapplication/octet-stream; name=0001-Change-BackendXidGetPid-return-type-to-pid_t.patchDownload
From 0befc8d3ceb7d070207ca1b052e70eed6e9348a9 Mon Sep 17 00:00:00 2001
From: Sami Imseih <simseih@amazon.com>
Date: Fri, 14 Nov 2025 14:37:58 -0600
Subject: [PATCH 1/1] Change BackendXidGetPid return type to pid_t

BackendXidGetPid() in procarray.c was returning an int,
but PIDs are represented as pid_t elsewhere.  This patch
changes the return type of this routine for consistency.
---
 src/backend/storage/ipc/procarray.c | 2 +-
 src/include/storage/procarray.h     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c
index 200f72c6e25..75819477961 100644
--- a/src/backend/storage/ipc/procarray.c
+++ b/src/backend/storage/ipc/procarray.c
@@ -3214,7 +3214,7 @@ BackendPidGetProcWithLock(int pid)
  * Beware that not every xact has an XID assigned.  However, as long as you
  * only call this using an XID found on disk, you're safe.
  */
-int
+pid_t
 BackendXidGetPid(TransactionId xid)
 {
 	int			result = 0;
diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h
index 2f4ae06c279..e2546de68cb 100644
--- a/src/include/storage/procarray.h
+++ b/src/include/storage/procarray.h
@@ -70,7 +70,7 @@ extern void ProcNumberGetTransactionIds(int procNumber, TransactionId *xid,
 										bool *overflowed);
 extern PGPROC *BackendPidGetProc(int pid);
 extern PGPROC *BackendPidGetProcWithLock(int pid);
-extern int	BackendXidGetPid(TransactionId xid);
+extern pid_t BackendXidGetPid(TransactionId xid);
 extern bool IsBackendPid(int pid);
 
 extern VirtualTransactionId *GetCurrentVirtualXIDs(TransactionId limitXmin,
-- 
2.50.1 (Apple Git-155)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Sami Imseih (#1)
Re: Have BackendXidGetPid return pid_t

Sami Imseih <samimseih@gmail.com> writes:

While looking at something nearby, I noticed that BackendXidGetPid()
uses an "int" for "pid", where it should be used "pid_t" like in other
places in the code including in procarray.c.

I don't think this is an amazingly good idea, considering that the
value it's returning is from an "int" field in struct PGPROC.
You didn't change the function's "result" variable either, nor
is it clear what callers like

snprintf(buf, NCHARS, "%d",
BackendXidGetPid(members[j].xid));

should be doing.

Perhaps at some point we should try to uniformly represent PIDs
as pid_t, but it'll require a far larger patch than this.

(I have a vague recollection also that some places expect PIDs in
shared memory to be atomically updatable, so machines where pid_t
is actually different from int might start to have issues.)

regards, tom lane