summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2014-01-17 21:52:06 +0000
committerTom Lane2014-01-17 21:52:06 +0000
commite6170126fc201052b0ec5fc92177eb181d602d26 (patch)
tree37c4660bd45764fa9737967f6e92a88bee19c5fb
parent708c529c7fdeba9387825d746752fc6f439d781e (diff)
Add gen_random_uuid() to contrib/pgcrypto.
This function provides a way of generating version 4 (pseudorandom) UUIDs based on pgcrypto's PRNG. The main reason for doing this is that the OSSP UUID library depended on by contrib/uuid-ossp is becoming more and more of a porting headache, so we need an alternative for people who can't install that. A nice side benefit though is that this implementation is noticeably faster than uuid-ossp's uuid_generate_v4() function. Oskari Saarenmaa, reviewed by Emre Hasegeli
-rw-r--r--contrib/pgcrypto/Makefile2
-rw-r--r--contrib/pgcrypto/pgcrypto--1.0--1.1.sql9
-rw-r--r--contrib/pgcrypto/pgcrypto--1.1.sql (renamed from contrib/pgcrypto/pgcrypto--1.0.sql)7
-rw-r--r--contrib/pgcrypto/pgcrypto.c27
-rw-r--r--contrib/pgcrypto/pgcrypto.control2
-rw-r--r--contrib/pgcrypto/pgcrypto.h1
-rw-r--r--doc/src/sgml/datatype.sgml2
-rw-r--r--doc/src/sgml/pgcrypto.sgml11
-rw-r--r--doc/src/sgml/uuid-ossp.sgml9
9 files changed, 67 insertions, 3 deletions
diff --git a/contrib/pgcrypto/Makefile b/contrib/pgcrypto/Makefile
index dadec953c2a..1c85c982ff9 100644
--- a/contrib/pgcrypto/Makefile
+++ b/contrib/pgcrypto/Makefile
@@ -26,7 +26,7 @@ MODULE_big = pgcrypto
OBJS = $(SRCS:.c=.o)
EXTENSION = pgcrypto
-DATA = pgcrypto--1.0.sql pgcrypto--unpackaged--1.0.sql
+DATA = pgcrypto--1.1.sql pgcrypto--1.0--1.1.sql pgcrypto--unpackaged--1.0.sql
REGRESS = init md5 sha1 hmac-md5 hmac-sha1 blowfish rijndael \
$(CF_TESTS) \
diff --git a/contrib/pgcrypto/pgcrypto--1.0--1.1.sql b/contrib/pgcrypto/pgcrypto--1.0--1.1.sql
new file mode 100644
index 00000000000..42e0c7fffc8
--- /dev/null
+++ b/contrib/pgcrypto/pgcrypto--1.0--1.1.sql
@@ -0,0 +1,9 @@
+/* contrib/pgcrypto/pgcrypto--1.0--1.1.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION pgcrypto UPDATE TO '1.1'" to load this file. \quit
+
+CREATE FUNCTION gen_random_uuid()
+RETURNS uuid
+AS 'MODULE_PATHNAME', 'pg_random_uuid'
+LANGUAGE C VOLATILE;
diff --git a/contrib/pgcrypto/pgcrypto--1.0.sql b/contrib/pgcrypto/pgcrypto--1.1.sql
index 347825ea07d..a260857d302 100644
--- a/contrib/pgcrypto/pgcrypto--1.0.sql
+++ b/contrib/pgcrypto/pgcrypto--1.1.sql
@@ -1,4 +1,4 @@
-/* contrib/pgcrypto/pgcrypto--1.0.sql */
+/* contrib/pgcrypto/pgcrypto--1.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgcrypto" to load this file. \quit
@@ -63,6 +63,11 @@ RETURNS bytea
AS 'MODULE_PATHNAME', 'pg_random_bytes'
LANGUAGE C VOLATILE STRICT;
+CREATE FUNCTION gen_random_uuid()
+RETURNS uuid
+AS 'MODULE_PATHNAME', 'pg_random_uuid'
+LANGUAGE C VOLATILE;
+
--
-- pgp_sym_encrypt(data, key)
--
diff --git a/contrib/pgcrypto/pgcrypto.c b/contrib/pgcrypto/pgcrypto.c
index a441ca77f12..9917e18d864 100644
--- a/contrib/pgcrypto/pgcrypto.c
+++ b/contrib/pgcrypto/pgcrypto.c
@@ -35,6 +35,7 @@
#include "parser/scansup.h"
#include "utils/builtins.h"
+#include "utils/uuid.h"
#include "px.h"
#include "px-crypt.h"
@@ -443,6 +444,32 @@ pg_random_bytes(PG_FUNCTION_ARGS)
PG_RETURN_BYTEA_P(res);
}
+/* SQL function: gen_random_uuid() returns uuid */
+PG_FUNCTION_INFO_V1(pg_random_uuid);
+
+Datum
+pg_random_uuid(PG_FUNCTION_ARGS)
+{
+ uint8 *buf = (uint8 *) palloc(UUID_LEN);
+ int err;
+
+ /* generate random bits */
+ err = px_get_pseudo_random_bytes(buf, UUID_LEN);
+ if (err < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
+ errmsg("Random generator error: %s", px_strerror(err))));
+
+ /*
+ * Set magic numbers for a "version 4" (pseudorandom) UUID, see
+ * http://tools.ietf.org/html/rfc4122#section-4.4
+ */
+ buf[6] = (buf[6] & 0x0f) | 0x40; /* "version" field */
+ buf[8] = (buf[8] & 0x3f) | 0x80; /* "variant" field */
+
+ PG_RETURN_UUID_P((pg_uuid_t *) buf);
+}
+
static void *
find_provider(text *name,
PFN provider_lookup,
diff --git a/contrib/pgcrypto/pgcrypto.control b/contrib/pgcrypto/pgcrypto.control
index 8375cf9e7bb..7f79d044ab3 100644
--- a/contrib/pgcrypto/pgcrypto.control
+++ b/contrib/pgcrypto/pgcrypto.control
@@ -1,5 +1,5 @@
# pgcrypto extension
comment = 'cryptographic functions'
-default_version = '1.0'
+default_version = '1.1'
module_pathname = '$libdir/pgcrypto'
relocatable = true
diff --git a/contrib/pgcrypto/pgcrypto.h b/contrib/pgcrypto/pgcrypto.h
index 6284ba2406a..04ea696ac38 100644
--- a/contrib/pgcrypto/pgcrypto.h
+++ b/contrib/pgcrypto/pgcrypto.h
@@ -45,5 +45,6 @@ Datum pg_decrypt(PG_FUNCTION_ARGS);
Datum pg_encrypt_iv(PG_FUNCTION_ARGS);
Datum pg_decrypt_iv(PG_FUNCTION_ARGS);
Datum pg_random_bytes(PG_FUNCTION_ARGS);
+Datum pg_random_uuid(PG_FUNCTION_ARGS);
#endif
diff --git a/doc/src/sgml/datatype.sgml b/doc/src/sgml/datatype.sgml
index 03863300d4a..6bf4cf61d86 100644
--- a/doc/src/sgml/datatype.sgml
+++ b/doc/src/sgml/datatype.sgml
@@ -4015,6 +4015,8 @@ a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
suited for every application. The <xref
linkend="uuid-ossp"> module
provides functions that implement several standard algorithms.
+ The <xref linkend="pgcrypto"> 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.
</para>
diff --git a/doc/src/sgml/pgcrypto.sgml b/doc/src/sgml/pgcrypto.sgml
index b99a75c3668..31dba8cb731 100644
--- a/doc/src/sgml/pgcrypto.sgml
+++ b/doc/src/sgml/pgcrypto.sgml
@@ -1084,6 +1084,17 @@ gen_random_bytes(count integer) returns bytea
At most 1024 bytes can be extracted at a time. This is to avoid
draining the randomness generator pool.
</para>
+
+ <indexterm>
+ <primary>gen_random_uuid</primary>
+ </indexterm>
+
+<synopsis>
+gen_random_uuid() returns uuid
+</synopsis>
+ <para>
+ Returns a version 4 (random) UUID.
+ </para>
</sect2>
<sect2>
diff --git a/doc/src/sgml/uuid-ossp.sgml b/doc/src/sgml/uuid-ossp.sgml
index 696cc112dc8..c48106ba0f4 100644
--- a/doc/src/sgml/uuid-ossp.sgml
+++ b/doc/src/sgml/uuid-ossp.sgml
@@ -18,6 +18,15 @@
<ulink url="http://www.ossp.org/pkg/lib/uuid/"></ulink>.
</para>
+ <note>
+ <para>
+ The OSSP UUID library is not well maintained, and is becoming increasingly
+ difficult to port to newer platforms. If you only need randomly-generated
+ (version 4) UUIDs, consider using the <function>gen_random_uuid()</>
+ function from the <xref linkend="pgcrypto"> module instead.
+ </para>
+ </note>
+
<sect2>
<title><literal>uuid-ossp</literal> Functions</title>