Hackers,

    While working on an application, the need arose to be able efficiently differentiate v4/v5 UUIDs (for use in partial indexes, among others)

... so please find attached a trivial patch which adds the functionality. The "uuid_version_bits()" function (from the test suite?) seems quite a bit hackish, apart from inefficient :(


    I'm not sure whether this actually would justify a version bump for the OSSP-UUID extension ---a misnomer, BTW, since at least in all the systems I have access to, the extension is actually linked against libuuid from e2fsutils, but I digress --- or not, given that it doesn't change exposed functionality.


    Another matter, which I'd like to propose in a later thread, is whether it'd be interesting to include the main UUID functionality directly in core, with the remaining functions in ossp-uuid (just like it is now, for backwards compatibility): Most current patterns for distributed/sharded databases are based on using UUIDs for many PKs.


Thanks,

    J.L.


diff --git a/contrib/uuid-ossp/uuid-ossp--1.1.sql b/contrib/uuid-ossp/uuid-ossp--1.1.sql
index c9cefd7360..a2eb217fd8 100644
--- a/contrib/uuid-ossp/uuid-ossp--1.1.sql
+++ b/contrib/uuid-ossp/uuid-ossp--1.1.sql
@@ -52,3 +52,8 @@ CREATE FUNCTION uuid_generate_v5(namespace uuid, name text)
 RETURNS uuid
 AS 'MODULE_PATHNAME', 'uuid_generate_v5'
 IMMUTABLE STRICT LANGUAGE C PARALLEL SAFE;
+
+CREATE FUNCTION uuid_version(namespace uuid)
+RETURNS int4
+AS 'MODULE_PATHNAME', 'uuid_version'
+IMMUTABLE STRICT LANGUAGE C PARALLEL SAFE;
diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c
index f5ae915f24..b4997281c0 100644
--- a/contrib/uuid-ossp/uuid-ossp.c
+++ b/contrib/uuid-ossp/uuid-ossp.c
@@ -122,6 +122,7 @@ PG_FUNCTION_INFO_V1(uuid_generate_v1mc);
 PG_FUNCTION_INFO_V1(uuid_generate_v3);
 PG_FUNCTION_INFO_V1(uuid_generate_v4);
 PG_FUNCTION_INFO_V1(uuid_generate_v5);
+PG_FUNCTION_INFO_V1(uuid_version);
 
 #ifdef HAVE_UUID_OSSP
 
@@ -531,3 +532,16 @@ uuid_generate_v5(PG_FUNCTION_ARGS)
 								  VARDATA_ANY(name), VARSIZE_ANY_EXHDR(name));
 #endif
 }
+
+Datum
+uuid_version(PG_FUNCTION_ARGS)
+{
+	pg_uuid_t  *arg = PG_GETARG_UUID_P(0);
+	dce_uuid_t uu;
+
+	/* function is marked STRICT, so arg can't be NULL */
+	memcpy(&uu,arg,UUID_LEN);
+	UUID_TO_NETWORK(uu);
+
+	PG_RETURN_INT32(uu.time_hi_and_version >> 12);
+}
diff --git a/doc/src/sgml/uuid-ossp.sgml b/doc/src/sgml/uuid-ossp.sgml
index b3b816c372..43dd565886 100644
--- a/doc/src/sgml/uuid-ossp.sgml
+++ b/doc/src/sgml/uuid-ossp.sgml
@@ -156,6 +156,22 @@ SELECT uuid_generate_v3(uuid_ns_url(), 'http://www.postgresql.org');
     </tbody>
    </tgroup>
   </table>
+
+  <table id="uuid-ossp-info">
+   <title>Functions Returning UUID attributes</title>
+   <tgroup cols="2">
+    <tbody>
+     <row>
+      <entry><literal>uuid_version()</literal></entry>
+      <entry>
+       <para>
+        Returns the UUID version (1,3,4,5). Assumes variant 1 (RFC4122).
+       </para>
+      </entry>
+     </row>
+    </tbody>
+   </tgroup>
+  </table>
  </sect2>
 
  <sect2>

Reply via email to