- suited for every application. The
- linkend="uuid-ossp"/> module
- provides functions that implement several standard algorithms.
- The module also provides a generation
- function for random UUIDs.
- Alternatively, UUIDs could be generated by client applications or
- other libraries invoked through a server-side function.
+ See for how to generate a UUID in
+
+
UUID Functions
+
+
+ generating
+
+
+
+
+
+
PostgreSQL includes one function to generate a UUID:
+
+gen_random_uuid() returns uuid
+
+ This function returns a version 4 (random) UUID. This is the most commonly
+ used type of UUID and is appropriate for most applications.
+
+
+ The module provides additional functions that
+ implement other standard algorithms for generating UUIDs.
+
+
The uuid-ossp module provides functions to generate universally
unique identifiers (UUIDs) using one of several standard algorithms. There
are also functions to produce certain special UUID constants.
+ This module is only necessary for special requirements beyond what is
+ available in core
PostgreSQL. See
+ linkend="functions-uuid"/> for built-in ways to generate UUIDs.
More than one of these libraries might be available on a particular
machine, so configure does not automatically choose one.
-
-
- If you only need randomly-generated (version 4) UUIDs,
- consider using the gen_random_uuid() function
- from the module instead.
-
-
return hash_any_extended(key->data, UUID_LEN, PG_GETARG_INT64(1));
}
+
+Datum
+gen_random_uuid(PG_FUNCTION_ARGS)
+{
+ pg_uuid_t *uuid = palloc(UUID_LEN);
+
+ if (!pg_strong_random(uuid, UUID_LEN))
+ ereport(ERROR,
+ (errcode(ERRCODE_INTERNAL_ERROR),
+ errmsg("could not generate random values")));
+
+ /*
+ * Set magic numbers for a "version 4" (pseudorandom) UUID, see
+ * http://tools.ietf.org/html/rfc4122#section-4.4
+ */
+ uuid->data[6] = (uuid->data[6] & 0x0f) | 0x40; /* time_hi_and_version */
+ uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; /* clock_seq_hi_and_reserved */
+
+ PG_RETURN_UUID_P(uuid);
+}
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 201907141
+#define CATALOG_VERSION_NO 201907142
#endif
{ oid => '3412', descr => 'hash',
proname => 'uuid_hash_extended', prorettype => 'int8',
proargtypes => 'uuid int8', prosrc => 'uuid_hash_extended' },
+{ oid => '3432', descr => 'generate random UUID',
+ proname => 'gen_random_uuid', proleakproof => 't', prorettype => 'uuid',
+ proargtypes => '', prosrc => 'gen_random_uuid' },
# pg_lsn
{ oid => '3229', descr => 'I/O',
sha256(bytea)
sha384(bytea)
sha512(bytea)
+gen_random_uuid()
starts_with(text,text)
macaddr8_eq(macaddr8,macaddr8)
macaddr8_lt(macaddr8,macaddr8)
1
(1 row)
+-- generation test
+TRUNCATE guid1;
+INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
+INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
+SELECT count(DISTINCT guid_field) FROM guid1;
+ count
+-------
+ 2
+(1 row)
+
-- clean up
DROP TABLE guid1, guid2 CASCADE;
SELECT COUNT(*) FROM guid1 g1 INNER JOIN guid2 g2 ON g1.guid_field = g2.guid_field;
SELECT COUNT(*) FROM guid1 g1 LEFT JOIN guid2 g2 ON g1.guid_field = g2.guid_field WHERE g2.guid_field IS NULL;
+-- generation test
+TRUNCATE guid1;
+INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
+INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
+SELECT count(DISTINCT guid_field) FROM guid1;
+
-- clean up
DROP TABLE guid1, guid2 CASCADE;