diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2014-04-26 15:11:10 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2014-04-26 15:11:10 -0400 |
commit | 5035701e07e8bd395aa878465a102afd7b74e8c3 (patch) | |
tree | 0e35eeba44df8079d0ed0c46c5d75aaa0e42b586 /src/backend/access/transam/xlog.c | |
parent | 528c454b2ada89ca0f0cd9a64f939e775b55b879 (diff) | |
download | postgresql-5035701e07e8bd395aa878465a102afd7b74e8c3.tar.gz postgresql-5035701e07e8bd395aa878465a102afd7b74e8c3.zip |
Improve generation algorithm for database system identifier.
As noted some time ago, the original coding had a typo ("|" for "^")
that made the result less unique than intended. Even the intended
behavior is obsolete since it was based on wanting to produce a
usable value even if we didn't have int64 arithmetic --- a limitation
we stopped supporting years ago. Instead, let's redefine the system
identifier as tv_sec in the upper 32 bits (same as before), tv_usec
in the next 20 bits, and the low 12 bits of getpid() in the remaining
bits. This is still hardly guaranteed-universally-unique, but it's
noticeably better than before. Per my proposal at
<29019.1374535940@sss.pgh.pa.us>
Diffstat (limited to 'src/backend/access/transam/xlog.c')
-rw-r--r-- | src/backend/access/transam/xlog.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 88ad51f9e7d..a636bb6d2b0 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -4889,15 +4889,16 @@ BootStrapXLOG(void) * field, as being about as unique as we can easily get. (Think not to * use random(), since it hasn't been seeded and there's no portable way * to seed it other than the system clock value...) The upper half of the - * uint64 value is just the tv_sec part, while the lower half is the XOR - * of tv_sec and tv_usec. This is to ensure that we don't lose uniqueness - * unnecessarily if "uint64" is really only 32 bits wide. A person - * knowing this encoding can determine the initialization time of the - * installation, which could perhaps be useful sometimes. + * uint64 value is just the tv_sec part, while the lower half contains the + * tv_usec part (which must fit in 20 bits), plus 12 bits from our current + * PID for a little extra uniqueness. A person knowing this encoding can + * determine the initialization time of the installation, which could + * perhaps be useful sometimes. */ gettimeofday(&tv, NULL); sysidentifier = ((uint64) tv.tv_sec) << 32; - sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec); + sysidentifier |= ((uint64) tv.tv_usec) << 12; + sysidentifier |= getpid() & 0xFFF; /* First timeline ID is always 1 */ ThisTimeLineID = 1; |