1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Contrib module uuid-ossp for generating UUID values using the OSSP UUID

library.  New configure option --with-ossp-uuid to activate.
This commit is contained in:
Peter Eisentraut
2007-04-21 17:26:18 +00:00
parent 71495f296e
commit 74496bc298
12 changed files with 421 additions and 6 deletions

View File

@ -1,4 +1,4 @@
# $PostgreSQL: pgsql/contrib/Makefile,v 1.74 2007/04/15 12:48:23 adunstan Exp $
# $PostgreSQL: pgsql/contrib/Makefile,v 1.75 2007/04/21 17:26:17 petere Exp $
subdir = contrib
top_builddir = ..
@ -37,6 +37,10 @@ ifeq ($(with_openssl),yes)
WANTED_DIRS += sslinfo
endif
ifeq ($(with_ossp_uuid),yes)
WANTED_DIRS += uuid-ossp
endif
ifeq ($(with_libxml),yes)
ifeq ($(with_libxslt),yes)
WANTED_DIRS += xml2

View File

@ -133,6 +133,10 @@ tsearch2 -
by Teodor Sigaev <teodor@sigaev.ru> and Oleg Bartunov
<oleg@sai.msu.su>.
uuid-ossp -
UUID generation functions
by Peter Eisentraut <peter_e@gmx.net>
vacuumlo -
Remove orphaned large objects
by Peter T Mount <peter@retep.org.uk>

View File

@ -0,0 +1,19 @@
# $PostgreSQL: pgsql/contrib/uuid-ossp/Makefile,v 1.1 2007/04/21 17:26:17 petere Exp $
MODULE_big = uuid-ossp
OBJS = uuid-ossp.o
DATA_built = uuid-ossp.sql
DATA = uninstall_uuid-ossp.sql
DOCS = README.uuid-ossp
SHLIB_LINK += -lossp-uuid
ifdef USE_PGXS
PGXS := $(shell pg_config --pgxs)
include $(PGXS)
else
subdir = contrib/uuid-ossp
top_builddir = ../..
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif

View File

@ -0,0 +1,97 @@
UUID Generation Functions
=========================
Peter Eisentraut <peter_e@gmx.net>
This module provides functions to generate universally unique
identifiers (UUIDs) using one of the several standard algorithms, as
well as functions to produce certain special UUID constants.
Installation
------------
The extra library required can be found at
<http://www.ossp.org/pkg/lib/uuid/>.
UUID Generation
---------------
The relevant standards ITU-T Rec. X.667, ISO/IEC 9834-8:2005, and RFC
4122 specify four algorithms for generating UUIDs, identified by the
version numbers 1, 3, 4, and 5. (There is no version 2 algorithm.)
Each of these algorithms could be suitable for a different set of
applications.
uuid_generate_v1()
~~~~~~~~~~~~~~~~~~
This function generates a version 1 UUID. This involves the MAC
address of the computer and a time stamp. Note that UUIDs of this
kind reveal the identity of the computer that created the identifier
and the time at which it did so, which might make it unsuitable for
certain security-sensitive applications.
uuid_generate_v1mc()
~~~~~~~~~~~~~~~~~~~~
This function generates a version 1 UUID but uses a random multicast
MAC address instead of the real MAC address of the computer.
uuid_generate_v3(namespace uuid, name text)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This function generates a version 3 UUID in the given namespace using
the specified input name. The namespace should be one of the special
constants produced by the uuid_ns_*() functions shown below. (It
should be any UUID in theory.) The name is an identifier in the
selected namespace. For example:
uuid_generate_v3(uuid_ns_url(), 'http://www.postgresql.org')
The name parameter will be MD5-hashed, so the cleartext cannot be
derived from the generated UUID.
The generation of UUIDs by this method has no random or
environment-dependent element and is therefore reproducible.
uuid_generate_v4()
~~~~~~~~~~~~~~~~~~
This function generates a version 4 UUID, which is derived entirely
from random numbers.
uuid_generate_v5(namespace uuid, name text)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This function generates a version 5 UUID, which works like a version 3
UUID except that SHA-1 is used as a hashing method. Version 5 should
be preferred over version 3 because SHA-1 is thought to be more secure
than MD5.
UUID Constants
--------------
uuid_nil()
A "nil" UUID constant, which does not occur as a real UUID.
uuid_ns_dns()
Constant designating the DNS namespace for UUIDs.
uuid_ns_url()
Constant designating the URL namespace for UUIDs.
uuid_ns_oid()
Constant designating the ISO object identifier (OID) namespace for
UUIDs. (This pertains to ASN.1 OIDs, unrelated to the OIDs used in
PostgreSQL.)
uuid_ns_x500()
Constant designating the X.500 distinguished name (DN) namespace for
UUIDs.

View File

@ -0,0 +1,15 @@
/* $PostgreSQL: pgsql/contrib/uuid-ossp/uninstall_uuid-ossp.sql,v 1.1 2007/04/21 17:26:17 petere Exp $ */
SET search_path = public;
DROP FUNCTION uuid_nil();
DROP FUNCTION uuid_ns_dns();
DROP FUNCTION uuid_ns_url();
DROP FUNCTION uuid_ns_oid();
DROP FUNCTION uuid_ns_x500();
DROP FUNCTION uuid_generate_v1();
DROP FUNCTION uuid_generate_v1mc();
DROP FUNCTION uuid_generate_v3(namespace uuid, name text);
DROP FUNCTION uuid_generate_v4();
DROP FUNCTION uuid_generate_v5(namespace uuid, name text);

View File

@ -0,0 +1,198 @@
/*-------------------------------------------------------------------------
*
* UUID generation functions using the OSSP UUID library
*
* Copyright (c) 2007 PostgreSQL Global Development Group
*
* $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.c,v 1.1 2007/04/21 17:26:17 petere Exp $
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "utils/uuid.h"
#include <ossp/uuid.h>
/* better both be 16 */
#if (UUID_LEN != UUID_LEN_BIN)
#error UUID length mismatch
#endif
PG_MODULE_MAGIC;
Datum uuid_nil(PG_FUNCTION_ARGS);
Datum uuid_ns_dns(PG_FUNCTION_ARGS);
Datum uuid_ns_url(PG_FUNCTION_ARGS);
Datum uuid_ns_oid(PG_FUNCTION_ARGS);
Datum uuid_ns_x500(PG_FUNCTION_ARGS);
Datum uuid_generate_v1(PG_FUNCTION_ARGS);
Datum uuid_generate_v1mc(PG_FUNCTION_ARGS);
Datum uuid_generate_v3(PG_FUNCTION_ARGS);
Datum uuid_generate_v4(PG_FUNCTION_ARGS);
Datum uuid_generate_v5(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(uuid_nil);
PG_FUNCTION_INFO_V1(uuid_ns_dns);
PG_FUNCTION_INFO_V1(uuid_ns_url);
PG_FUNCTION_INFO_V1(uuid_ns_oid);
PG_FUNCTION_INFO_V1(uuid_ns_x500);
PG_FUNCTION_INFO_V1(uuid_generate_v1);
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);
static char *
uuid_to_string(const uuid_t *uuid)
{
char *buf = palloc(UUID_LEN_STR + 1);
void *ptr = buf;
size_t len = UUID_LEN_STR + 1;
uuid_export(uuid, UUID_FMT_STR, &ptr, &len);
return buf;
}
static void
string_to_uuid(const char *str, uuid_t *uuid)
{
uuid_import(uuid, UUID_FMT_STR, str, UUID_LEN_STR + 1);
}
static Datum
special_uuid_value(const char *name)
{
uuid_t *uuid;
char *str;
uuid_create(&uuid);
uuid_load(uuid, name);
str = uuid_to_string(uuid);
uuid_destroy(uuid);
return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
}
Datum
uuid_nil(PG_FUNCTION_ARGS)
{
return special_uuid_value("nil");
}
Datum
uuid_ns_dns(PG_FUNCTION_ARGS)
{
return special_uuid_value("ns:DNS");
}
Datum
uuid_ns_url(PG_FUNCTION_ARGS)
{
return special_uuid_value("ns:URL");
}
Datum
uuid_ns_oid(PG_FUNCTION_ARGS)
{
return special_uuid_value("ns:OID");
}
Datum
uuid_ns_x500(PG_FUNCTION_ARGS)
{
return special_uuid_value("ns:X500");
}
static Datum
uuid_generate_internal(int mode, const uuid_t *ns, const char *name)
{
uuid_t *uuid;
char *str;
uuid_create(&uuid);
uuid_make(uuid, mode, ns, name);
str = uuid_to_string(uuid);
uuid_destroy(uuid);
return DirectFunctionCall1(uuid_in, CStringGetDatum(str));
}
Datum
uuid_generate_v1(PG_FUNCTION_ARGS)
{
return uuid_generate_internal(UUID_MAKE_V1, NULL, NULL);
}
Datum
uuid_generate_v1mc(PG_FUNCTION_ARGS)
{
return uuid_generate_internal(UUID_MAKE_V1 | UUID_MAKE_MC, NULL, NULL);
}
static Datum
uuid_generate_v35_internal(int mode, pg_uuid_t *ns, text *name)
{
uuid_t *ns_uuid;
Datum result;
uuid_create(&ns_uuid);
string_to_uuid(DatumGetCString(DirectFunctionCall1(uuid_out, UUIDPGetDatum(ns))),
ns_uuid);
result = uuid_generate_internal(mode,
ns_uuid,
DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(name))));
uuid_destroy(ns_uuid);
return result;
}
Datum
uuid_generate_v3(PG_FUNCTION_ARGS)
{
pg_uuid_t *ns = PG_GETARG_UUID_P(0);
text *name = PG_GETARG_TEXT_P(1);
return uuid_generate_v35_internal(UUID_MAKE_V3, ns, name);
}
Datum
uuid_generate_v4(PG_FUNCTION_ARGS)
{
return uuid_generate_internal(UUID_MAKE_V4, NULL, NULL);
}
Datum
uuid_generate_v5(PG_FUNCTION_ARGS)
{
pg_uuid_t *ns = PG_GETARG_UUID_P(0);
text *name = PG_GETARG_TEXT_P(1);
return uuid_generate_v35_internal(UUID_MAKE_V5, ns, name);
}

View File

@ -0,0 +1,15 @@
/* $PostgreSQL: pgsql/contrib/uuid-ossp/uuid-ossp.sql.in,v 1.1 2007/04/21 17:26:17 petere Exp $ */
SET search_path = public;
CREATE FUNCTION uuid_nil() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_nil';
CREATE FUNCTION uuid_ns_dns() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_dns';
CREATE FUNCTION uuid_ns_url() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_url';
CREATE FUNCTION uuid_ns_oid() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_oid';
CREATE FUNCTION uuid_ns_x500() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_ns_x500';
CREATE FUNCTION uuid_generate_v1() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v1';
CREATE FUNCTION uuid_generate_v1mc() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v1mc';
CREATE FUNCTION uuid_generate_v3(namespace uuid, name text) RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v3';
CREATE FUNCTION uuid_generate_v4() RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v4';
CREATE FUNCTION uuid_generate_v5(namespace uuid, name text) RETURNS uuid IMMUTABLE STRICT LANGUAGE C AS 'MODULE_PATHNAME', 'uuid_generate_v5';