Move system identifier generation to a common helper

Started by Imran Zaheer3 months ago4 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t139848
psql -h localhost -U postgres

Built from patchset v1 (message #1), August 23, 2026 at 12:57 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t139848_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t139848_1 && git checkout t139848_1

Patchset v1 (message #1) is on t139848_1

Jump to latest
#1Imran Zaheer
imran.zhir@gmail.com

Hi

The code used to generate a new system identifier is duplicated in
multiple locations, including BootStrapXLOG(), pg_createsubscriber, and
pg_resetwal.

Move the generation logic into a common GenerateSystemIdentifier()
helper so that all callers use a single implementation, avoiding
duplication of the same algorithm.

Thanks
Imran Zaheer

Attachments:

t139848_1
v1-0001-Move-system-identifier-generation-to-a-common-hel.patchtext/x-patch; charset=US-ASCII; name=v1-0001-Move-system-identifier-generation-to-a-common-hel.patchDownload+84-37
#2Peter Eisentraut
peter_e@gmx.net
In reply to: Imran Zaheer (#1)
Re: Move system identifier generation to a common helper

On 04.06.26 14:22, Imran Zaheer wrote:

The code used to generate a new system identifier is duplicated in
multiple locations, including BootStrapXLOG(), pg_createsubscriber, and
pg_resetwal.

Move the generation logic into a common GenerateSystemIdentifier()
helper so that all callers use a single implementation, avoiding
duplication of the same algorithm.

Then again, this code is from PG 8.0. We have had pg_strong_random()
required since PG 12. Maybe we should use that now for this.

#3Daniel Gustafsson
daniel@yesql.se
In reply to: Peter Eisentraut (#2)
Re: Move system identifier generation to a common helper

On 10 Jun 2026, at 15:25, Peter Eisentraut <peter@eisentraut.org> wrote:

On 04.06.26 14:22, Imran Zaheer wrote:

The code used to generate a new system identifier is duplicated in
multiple locations, including BootStrapXLOG(), pg_createsubscriber, and
pg_resetwal.
Move the generation logic into a common GenerateSystemIdentifier()
helper so that all callers use a single implementation, avoiding
duplication of the same algorithm.

Then again, this code is from PG 8.0. We have had pg_strong_random() required since PG 12. Maybe we should use that now for this.

One feature of the current scheme is that it's explicitly not random, but can
be reverse-engineered to figure out the init time. Maybe we should have a
better way of doing that regardless, but it doesn't seem like a bad feature to
keep.

--
Daniel Gustafsson

#4Imran Zaheer
imran.zhir@gmail.com
In reply to: Daniel Gustafsson (#3)
Re: Move system identifier generation to a common helper

On Thu, Jun 11, 2026 at 1:29 AM Daniel Gustafsson <daniel@yesql.se> wrote:

On 10 Jun 2026, at 15:25, Peter Eisentraut <peter@eisentraut.org> wrote:

On 04.06.26 14:22, Imran Zaheer wrote:

The code used to generate a new system identifier is duplicated in
multiple locations, including BootStrapXLOG(), pg_createsubscriber, and
pg_resetwal.
Move the generation logic into a common GenerateSystemIdentifier()
helper so that all callers use a single implementation, avoiding
duplication of the same algorithm.

Then again, this code is from PG 8.0. We have had pg_strong_random() required since PG 12. Maybe we should use that now for this.

One feature of the current scheme is that it's explicitly not random, but can
be reverse-engineered to figure out the init time. Maybe we should have a
better way of doing that regardless, but it doesn't seem like a bad feature to
keep.

--
Daniel Gustafsson

Hi,

The ability to extract init time can be useful for debugging purposes.

```
postgres=# SELECT to_timestamp(system_identifier >> 32) AS
cluster_init_time FROM pg_control_system();
cluster_init_time
------------------------
2026-06-05 17:12:07+05
(1 row)

```

To preserve this while improving the uniqueness, we could keep the
upper 32 bit (tv_sec) and replace the lower half (tv_usec + PID) with
pg_strong_random().

```
+ struct timeval tv;
+ uint64 sysidentifier;
+ uint32 random_bits;
+
+ gettimeofday(&tv, NULL);
+ sysidentifier = ((uint64) tv.tv_sec) << 32;
+
+ if (!pg_strong_random(&random_bits, sizeof(random_bits)))
+ {
+ #ifndef FRONTEND
+ elog(PANIC, "could not generate random bytes for system identifier");
+ #else
+ pg_fatal("could not generate random bytes for system identifier");
+ #endif
+ }
+
+ sysidentifier |= (uint64) random_bits;

```

thoughts?