How to add uuid() to PostgreSQL?
March 10th, 2006 in Linux, Personal, Programming, SQL, Video Conferencing
Updated
UUID support is already in latest version of PostgreSQL. This is not longer needed.
http://www.postgresql.org/docs/8.3/static/datatype-uuid.html
What is uuid?
UUID stands for Universally Unique Identifier. The UUIDs generated can be reasonably expected to be unique within a system and unique across all systems. They could be used, for instance, to generate unique HTTP cookies across multiple web servers without communication between the servers, and without fear of a name clash.
I really don't like the MySQL uuid() implementation, so I have to create my own uuid() function using libuuid.
This is something similar to uuid() function I created for MySQL which works pretty well in PostgreSQL:
Source code for pguuid.c :
/*
(C) Claudiu RAVEICA
*/
#include <uuid/uuid.h>
#include <pgsql/server/postgres.h>
#include <pgsql/server/fmgr.h>
PG_FUNCTION_INFO_V1(uuid);
Datum
uuid()
{
struct varlena *p;
uuid_t uu;
uuid_generate(uu);
char buffer[37];
uuid_unparse(uu, buffer);
p = palloc(strlen(buffer) + VARHDRSZ);
p->vl_len = strlen(buffer) + VARHDRSZ;
memcpy(VARDATA(p), buffer,strlen(buffer));
PG_RETURN_TEXT_P(p);
}
gcc -fPIC -shared -O2 pguuid.c -luuid -o pguuid.so
Copy pguuid.so to your PostgreSQL lib directory (E.g.: /usr/lib/pgsql)
CREATE FUNCTION uuid() RETURNS text AS
'/usr/lib/pgsql/pguuid.so', 'uuid'
LANGUAGE C STRICT;
Examples:
SELECT UUID();
uuid
————————————–
0cd1954c-8555-4348-b425-afc5a7158ce4
(1 row)
Warning!
This works only on platforms that have libuuid.
Another similar project:
One Comment on “How to add uuid() to PostgreSQL?”
Leave a Reply
You must be logged in to post a comment.




I followed the steps you described and works just fine for me.