From 328032b5ec850e7eacc135e00e8d5f63c23d8058 Mon Sep 17 00:00:00 2001 From: Andrey Borodin Date: Tue, 19 Nov 2024 22:41:32 +0500 Subject: [PATCH v35 2/2] Mix in 2 bits of entropy into timestampt of UUID on MacOS --- src/backend/utils/adt/uuid.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c index f8b8b59021..25cbe62803 100644 --- a/src/backend/utils/adt/uuid.c +++ b/src/backend/utils/adt/uuid.c @@ -455,7 +455,12 @@ static inline uint64 get_real_time_ns_ascending() ns = tmp.tv_sec * 1000000000L + tmp.tv_nsec; /* minimum amount of ns that guarantees step of UUID increased clock precision */ -#define SUB_MILLISECOND_STEP ((NS_PER_MS / (1 << 12)) + 1) +#if defined(__darwin__) || _MSC_VER +#define SUB_MILLISECOND_BITS 10 +#else +#define SUB_MILLISECOND_BITS 12 +#endif +#define SUB_MILLISECOND_STEP ((NS_PER_MS / (1 << SUB_MILLISECOND_BITS)) + 1) if (previous_ns + SUB_MILLISECOND_STEP >= ns) ns = previous_ns + SUB_MILLISECOND_STEP; previous_ns = ns; @@ -503,6 +508,17 @@ generate_uuidv7(uint64 ns) (errcode(ERRCODE_INTERNAL_ERROR), errmsg("could not generate random values"))); +#if defined(__darwin__) || _MSC_VER + /* + * On MacOS real time is truncted to microseconds. Thus, 2 least + * significant bits of increased_clock_precision are neither random + * (CSPRNG), nor time-dependent (in a sense - truly random). These 2 bits + * are dependent on other time-specific bits, thus they do not contribute + * to uniqueness. To make these bit random we mix in two bits from CSPRNG. + */ + uuid->data[7] = uuid->data[7] ^ (uuid->data[8] >> 6); +#endif + /* * Set magic numbers for a "version 7" (pseudorandom) UUID, see * https://www.rfc-editor.org/rfc/rfc9562#name-version-field -- 2.39.5 (Apple Git-154)