PostgreSQL's hashing function?

Started by Kynn Jonesabout 18 years ago4 messagesgeneral
Jump to latest
#1Kynn Jones
kynnjo@gmail.com

Hi!
Does PostgreSQL expose its hash function? I need a fast way to hash a
string to a short code using characters in the set [A-Za-z0-9_]. (I'm not
sure yet how long this code needs to be, but I think even something as short
as length 2 may be enough.)

TIA!

Kynn

#2Joshua D. Drake
jd@commandprompt.com
In reply to: Kynn Jones (#1)
Re: PostgreSQL's hashing function?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, 22 Feb 2008 16:49:10 -0500
"Kynn Jones" <kynnjo@gmail.com> wrote:

Hi!
Does PostgreSQL expose its hash function? I need a fast way to hash a
string to a short code using characters in the set [A-Za-z0-9_].
(I'm not sure yet how long this code needs to be, but I think even
something as short as length 2 may be enough.)

Is md5 good enough? :)

select md5()

TIA!

Kynn

- --
The PostgreSQL Company since 1997: http://www.commandprompt.com/
PostgreSQL Community Conference: http://www.postgresqlconference.org/
Donate to the PostgreSQL Project: http://www.postgresql.org/about/donate
PostgreSQL SPI Liaison | SPI Director | PostgreSQL political pundit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHv0VjATb/zqfZUUQRAqUNAJ9zejT/pN1uIDtQYEQ7TQvt0LqFjACfelSy
wNa9NNUN8rAfmeqwrKCvUb0=
=H8DS
-----END PGP SIGNATURE-----

#3Bruce Momjian
bruce@momjian.us
In reply to: Joshua D. Drake (#2)
Re: PostgreSQL's hashing function?

Joshua D. Drake wrote:

On Fri, 22 Feb 2008 16:49:10 -0500
"Kynn Jones" <kynnjo@gmail.com> wrote:

Hi!
Does PostgreSQL expose its hash function? I need a fast way to hash a
string to a short code using characters in the set [A-Za-z0-9_].

Is md5 good enough? :)

Probably not. He said fast.

You could use hashtext() which is Postgres's internal hash function.
There's some possibility it could change in future versions of Postgres
though.

#4Kynn Jones
kynnjo@gmail.com
In reply to: Bruce Momjian (#3)
Re: PostgreSQL's hashing function?

On Fri, Feb 22, 2008 at 8:12 PM, Greg Stark <stark@enterprisedb.com> wrote:

You could use hashtext() which is Postgres's internal hash function.

Awesome!

There's some possibility it could change in future versions of Postgres
though.

I can live with that, especially if all that changes is the implementation,
and not its availability. The use I have in mind is strictly internal, so
the possibility that a given string may get a different hashkey if
PostgreSQL gets upgraded is no problem at all.

Thanks for the pointer! It's not in the docs, so I would have never found
it on my own... (It makes me wonder what other "hidden" PostgreSQL goodies
are out there. I know, I know, peeking behind the published API is evil...
I guess I'm a geek "in a state of sin", to use von Neumann's terms...)

kynn