timescaledb/src/uuid.c
Mats Kindahl 15d33f0624 Add option to compile without telemetry
Add option `USE_TELEMETRY` that can be used to exclude telemetry from
the compile.

Telemetry-specific SQL is moved, which is only included when extension
is compiled with telemetry and the notice is changed so that the
message about telemetry is not printed when Telemetry is not compiled
in.

The following code is not compiled in when telemetry is not used:
- Cross-module functions for telemetry.
- Checks for telemetry job in job execution.
- GUC variables `telemetry_level` and `telemetry_cloud`.

Telemetry subsystem is not included when compiling without telemetry,
which requires some functions to be moved out of the telemetry
subsystem:
- Metadata handling is moved out of the telemetry module since it is
  used not only with telemetry.
- UUID functions are moved into a separate module instead of being
  part of the telemetry subsystem.
- Telemetry functions are either added or removed when updating from a
  previous version.

Tests are updated to:
- Not use telemetry functions to get UUID or Metadata and instead use
  the moved UUID and metadata functions.
- Not include telemetry information in tests that do not require it.
- Configuration files do not set telemetry variables when telemetry is
  not compiled in.
- Replaced usage of telemetry functions in non-telemetry tests with
  other sources of same information.

Fixes #3931
2022-03-03 12:21:07 +01:00

57 lines
1.5 KiB
C

/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#include <postgres.h>
#include <utils/timestamp.h>
#include <utils/uuid.h>
#include "compat/compat.h"
#include "uuid.h"
/*
* Generates a v4 UUID. Based on function pg_random_uuid() in the pgcrypto contrib module.
*
* Note that clib on Mac has a uuid_generate() function, so we call this ts_uuid_create().
*/
pg_uuid_t *
ts_uuid_create(void)
{
/*
* PG9.6 doesn't expose the internals of pg_uuid_t, so we just treat it as
* a byte array
*/
unsigned char *gen_uuid = palloc0(UUID_LEN);
bool rand_success = false;
rand_success = pg_backend_random((char *) gen_uuid, UUID_LEN);
/*
* If pg_backend_random() cannot find sources of randomness, then we use
* the current timestamp as a "random source".
* Timestamps are 8 bytes, so we copy this into bytes 9-16 of the UUID.
* If we see all 0s in bytes 0-8 (other than version + * variant), we know
* that there is something wrong with the RNG on this instance.
*/
if (!rand_success)
{
TimestampTz ts = GetCurrentTimestamp();
memcpy(&gen_uuid[8], &ts, sizeof(TimestampTz));
}
gen_uuid[6] = (gen_uuid[6] & 0x0f) | 0x40; /* "version" field */
gen_uuid[8] = (gen_uuid[8] & 0x3f) | 0x80; /* "variant" field */
return (pg_uuid_t *) gen_uuid;
}
TS_FUNCTION_INFO_V1(ts_uuid_generate);
Datum
ts_uuid_generate(PG_FUNCTION_ARGS)
{
return UUIDPGetDatum(ts_uuid_create());
}