|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * sequential_uuids.c |
| 4 | + * generators of sequential UUID values based on sequence/timestamp |
| 5 | + * |
| 6 | + * |
| 7 | + * Currently, this only works on PostgreSQL 10. Adding support for older |
| 8 | + * releases is possible, but it would require solving a couple issues: |
| 9 | + * |
| 10 | + * 1) pg_uuid_t hidden in uuid.c (can be solved by local struct definition) |
| 11 | + * |
| 12 | + * 2) pg_strong_random not available (can fallback to random, probably) |
| 13 | + * |
| 14 | + * 3) functions defined as PARALLEL SAFE, which fails on pre-9.6 releases |
| 15 | + * |
| 16 | + *------------------------------------------------------------------------- |
| 17 | + */ |
| 18 | +#include <sys/time.h> |
| 19 | +#include <sys/types.h> |
| 20 | +#include <unistd.h> |
| 21 | + |
| 22 | +#include "postgres.h" |
| 23 | + |
| 24 | +#include "catalog/namespace.h" |
| 25 | +#include "commands/sequence.h" |
| 26 | +#include "utils/uuid.h" |
| 27 | + |
| 28 | +PG_MODULE_MAGIC; |
| 29 | + |
| 30 | +PG_FUNCTION_INFO_V1(uuid_sequence_nextval); |
| 31 | +PG_FUNCTION_INFO_V1(uuid_time_nextval); |
| 32 | + |
| 33 | +/* |
| 34 | + * uuid_sequence_nextval |
| 35 | + * generate sequential UUID using a sequence |
| 36 | + * |
| 37 | + * The sequence-based sequential UUID generator define the group size |
| 38 | + * and group count based on number of UUIDs generated. |
| 39 | + * |
| 40 | + * The block_size (65546 by default) determines the number of UUIDs with |
| 41 | + * the same prefix, and block_count (65536 by default) determines the |
| 42 | + * number of blocks before wrapping around to 0. This means that with |
| 43 | + * the default values, the generator wraps around every ~2B UUIDs. |
| 44 | + * |
| 45 | + * You may increase (or rather decrease) the parameters if needed, e.g, |
| 46 | + * by lowering the block size to 256, in wich case the cycle interval |
| 47 | + * is only 16M values. |
| 48 | + */ |
| 49 | +Datum |
| 50 | +uuid_sequence_nextval(PG_FUNCTION_ARGS) |
| 51 | +{ |
| 52 | + int i; |
| 53 | + int64 val; |
| 54 | + Oid relid = PG_GETARG_OID(0); |
| 55 | + int32 block_size = PG_GETARG_INT32(1); |
| 56 | + int32 block_count = PG_GETARG_INT32(2); |
| 57 | + int64 prefix_bytes; |
| 58 | + pg_uuid_t *uuid; |
| 59 | + unsigned char *p; |
| 60 | + |
| 61 | + /* some basic sanity checks */ |
| 62 | + if (block_size < 0) |
| 63 | + ereport(ERROR, |
| 64 | + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 65 | + errmsg("block size must be a positive integer"))); |
| 66 | + |
| 67 | + if (block_count < 0) |
| 68 | + ereport(ERROR, |
| 69 | + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 70 | + errmsg("number of blocks must be a positive integer"))); |
| 71 | + |
| 72 | + /* count the number of bytes to keep from the sequence value */ |
| 73 | + prefix_bytes = 0; |
| 74 | + while (block_count > 1) |
| 75 | + { |
| 76 | + block_count /= 256; |
| 77 | + prefix_bytes++; |
| 78 | + } |
| 79 | + |
| 80 | + /* |
| 81 | + * Read the next value from the sequence and get rid of the least |
| 82 | + * significant bytes. |
| 83 | + */ |
| 84 | + val = nextval_internal(relid, true); |
| 85 | + val /= block_size; |
| 86 | + |
| 87 | + p = (unsigned char *) &val; |
| 88 | + |
| 89 | + uuid = palloc(sizeof(pg_uuid_t)); |
| 90 | + |
| 91 | + /* copy the desired number of (least significant) bytes as prefix */ |
| 92 | + for (i = 0; i < prefix_bytes; i++) |
| 93 | + uuid->data[i] = p[prefix_bytes - 1 - i]; |
| 94 | + |
| 95 | + /* generate the remaining bytes as random (use strong generator) */ |
| 96 | + if(!pg_strong_random(uuid->data + prefix_bytes, UUID_LEN - prefix_bytes)) |
| 97 | + ereport(ERROR, |
| 98 | + (errcode(ERRCODE_INTERNAL_ERROR), |
| 99 | + errmsg("could not generate random values"))); |
| 100 | + |
| 101 | + /* |
| 102 | + * Set the UUID version flags according to "version 4" (pseudorandom) |
| 103 | + * UUID, see http://tools.ietf.org/html/rfc4122#section-4.4 |
| 104 | + * |
| 105 | + * This does reduce the randomness a bit, because it determines the |
| 106 | + * value of certain bits, but that should be negligible (certainly |
| 107 | + * compared to the reduction due to prefix). |
| 108 | + * |
| 109 | + * UUID v4 is probably the safest choice here. There is v1 which is |
| 110 | + * time-based, but it includes MAC address (which we don't use) and |
| 111 | + * works with very special timestamp (starting at 1582 etc.). So we |
| 112 | + * just use v4 and claim this is pseudorandom. |
| 113 | + */ |
| 114 | + uuid->data[6] = (uuid->data[6] & 0x0f) | 0x40; /* time_hi_and_version */ |
| 115 | + uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; /* clock_seq_hi_and_reserved */ |
| 116 | + |
| 117 | + PG_RETURN_UUID_P(uuid); |
| 118 | +} |
| 119 | + |
| 120 | +/* |
| 121 | + * uuid_time_nextval |
| 122 | + * generate sequential UUID using current time |
| 123 | + * |
| 124 | + * The timestamp-based sequential UUID generator define the group size |
| 125 | + * and group count based on data extracted from current timestamp. |
| 126 | + * |
| 127 | + * The interval_length (60 seconds by default) is defined as number of |
| 128 | + * seconds where UUIDs share the same prefix). The prefix length is |
| 129 | + * determined by the number of intervals (65536 by default, i.e. 2B). |
| 130 | + * With these parameters the generator wraps around every ~45 days. |
| 131 | + */ |
| 132 | +Datum |
| 133 | +uuid_time_nextval(PG_FUNCTION_ARGS) |
| 134 | +{ |
| 135 | + int i; |
| 136 | + struct timeval tv; |
| 137 | + int64 val; |
| 138 | + pg_uuid_t *uuid; |
| 139 | + int32 interval_length = PG_GETARG_INT32(0); |
| 140 | + int32 interval_count = PG_GETARG_INT32(1); |
| 141 | + int64 prefix_bytes; |
| 142 | + unsigned char *p; |
| 143 | + |
| 144 | + /* some basic sanity checks */ |
| 145 | + if (interval_length < 1) |
| 146 | + ereport(ERROR, |
| 147 | + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 148 | + errmsg("length of interval must be a positive integer"))); |
| 149 | + |
| 150 | + if (interval_count < 1) |
| 151 | + ereport(ERROR, |
| 152 | + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 153 | + errmsg("number of intervals must be a positive integer"))); |
| 154 | + |
| 155 | + if (gettimeofday(&tv, NULL) != 0) |
| 156 | + elog(ERROR, "gettimeofday call failed"); |
| 157 | + |
| 158 | + val = (tv.tv_sec / interval_length); |
| 159 | + |
| 160 | + /* count the number of bytes to keep from the timestamp */ |
| 161 | + prefix_bytes = 0; |
| 162 | + while (interval_count > 1) |
| 163 | + { |
| 164 | + interval_count /= 256; |
| 165 | + prefix_bytes++; |
| 166 | + } |
| 167 | + |
| 168 | + p = (unsigned char *) &val; |
| 169 | + |
| 170 | + uuid = palloc(sizeof(pg_uuid_t)); |
| 171 | + |
| 172 | + /* copy the desired number of (least significant) bytes as prefix */ |
| 173 | + for (i = 0; i < prefix_bytes; i++) |
| 174 | + uuid->data[i] = p[prefix_bytes - 1 - i]; |
| 175 | + |
| 176 | + /* generate the remaining bytes as random (use strong generator) */ |
| 177 | + if(!pg_strong_random(uuid->data + prefix_bytes, UUID_LEN - prefix_bytes)) |
| 178 | + ereport(ERROR, |
| 179 | + (errcode(ERRCODE_INTERNAL_ERROR), |
| 180 | + errmsg("could not generate random values"))); |
| 181 | + |
| 182 | + /* |
| 183 | + * Set the UUID version flags according to "version 4" (pseudorandom) |
| 184 | + * UUID, see http://tools.ietf.org/html/rfc4122#section-4.4 |
| 185 | + * |
| 186 | + * This does reduce the randomness a bit, because it determines the |
| 187 | + * value of certain bits, but that should be negligible (certainly |
| 188 | + * compared to the reduction due to prefix). |
| 189 | + * |
| 190 | + * UUID v4 is probably the safest choice here. There is v1 which is |
| 191 | + * time-based, but it includes MAC address (which we don't use) and |
| 192 | + * works with very special timestamp (starting at 1582 etc.). So we |
| 193 | + * just use v4 and claim this is pseudorandom. |
| 194 | + */ |
| 195 | + uuid->data[6] = (uuid->data[6] & 0x0f) | 0x40; /* time_hi_and_version */ |
| 196 | + uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; /* clock_seq_hi_and_reserved */ |
| 197 | + |
| 198 | + PG_RETURN_UUID_P(uuid); |
| 199 | +} |
0 commit comments