7

While testing out PostgreSQL's md5() function I noticed very bizarre behavior:

Works as expected

SELECT md5('abc')
--"900150983cd24fb0d6963f7d28e17f72"

But using the md5() function in a query:

SELECT request_id, md5(request_id)
FROM Request
ORDER BY request_id

results in this error:

ERROR:  function md5(integer) does not exist
LINE 1: SELECT request_id, md5(request_id)
                           ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

********** Error **********

ERROR: function md5(integer) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 20

How can the function not exist if it worked in the first query? What am I doing wrong; what is the correct way to use md5() in a SELECT query?

3
  • 3
    That doesn't seem bizarre at all. In the first query, you passed text, while in the second one you're trying to pass integers.
    – us2012
    Mar 24, 2013 at 17:21
  • @us2012: yup, that's the difference. Mar 24, 2013 at 17:26
  • In general it doesn't make much sense to take the md5 of an integer, so I'd be interested in why you're trying to do this. Mar 24, 2013 at 22:34

4 Answers 4

14

The function expects text as parameter. Cast it:

SELECT request_id, md5(request_id::text)
FROM Request
ORDER BY request_id

A function named md5 accepting an integer parameter does not exist but you can create it:

create function md5(integer)
returns text as $$

select md5($1::text);

$$ language sql immutable;

Then there will be 3 signatures for md5:

=> \df md5
                          List of functions
   Schema   | Name | Result data type | Argument data types |  Type  
------------+------+------------------+---------------------+--------
 pg_catalog | md5  | text             | bytea               | normal
 pg_catalog | md5  | text             | text                | normal
 public     | md5  | text             | integer             | normal

As pointed in the comments to this answer the md5 hash of the integer's text representation may not be what you want. To have the hash of the binary the md5 signature accepting a bytea parameter should be used:

select md5(('\x' || right('0000000' || to_hex(200), 8))::bytea);
               md5                
----------------------------------
 b7b436d004c1cc0501bee9e296d2eaa4

And replace the previously created function:

create or replace function md5(integer)
returns text as $$

select md5(('\x' || right('0000000' || to_hex($1), 8))::bytea);

$$ language sql immutable;
4
  • True story. I figured it out right after I posted the question. Thanks Clodoaldo. Mar 24, 2013 at 17:22
  • 2
    Note that casting the integer to text means you're taking the md5 of the textual representation of the integer. This may not be what you intend when communicating with other systems. If you really want to take the md5 of the integer's binary representation you'll need to use a procedural language like pl/perl, pl/python, etc to take the md5 of the appropriate binary representation (signed 32-bit little endian, for example). Mar 24, 2013 at 22:33
  • 1
    @Craig Good point. But it looks like a procedural language is not necessary. Or am I missing something? Mar 25, 2013 at 13:40
  • @ClodoaldoNeto Clever approach, though you are indeed missing something. to_hex doesn't let you control the representation; it looks like it's always a two's complement little endian 32-bit integer; see SELECT to_hex(-1);. That's fine if that's the format you need, but it might not be. Anyway, this is clearly total pedantry for the OP's actual needs, I raised the original point before the OP had explained what they were trying to achieve. Mar 26, 2013 at 0:04
3

In general it doesn't make much sense to take the md5 of an integer. It seems likely that you trying to obscure a sequence so it appears semi-random in order. If so, there's a much better way:

Use the pseudo_encrypt function listed on the PostgreSQL wiki. It's way saner than trying to take the md5 of an integer then (presumably) truncate it.

The above does not provide strong cryptographic randomness, but neither does your approach. If you need your request IDs to be genuinely unpredictable for security reasons rather than just non-obvious at a casual glance you should be using a strong cryptographic random numbers generator and be prepared to cope with duplicates using time windows, etc.

5
  • I'm just trying to generate a non-obvious, non-repeating, value which can be used to look-up a record. So my bright (?) idea is to do some sort of encryption on the tble's PL. It's security-by-anonymity made necessary because casual users (ticket requesters) will not have logins. Anyway, I landed on using REPLACE(request_id::text,'1234567890','abcdefghij') to do this "encryption". Mar 25, 2013 at 1:13
  • PS--your "Awooga! Awooga!" still cracks me up. Mar 25, 2013 at 1:13
  • 1
    @JeromyFrench What can I say, I love Holly and like making jokes only us oldies will get ;-) . Like a lecturer I had at uni that'd make extremely funny Monkey references in her talks; four or five of us in the theatre would crack up laughing and the everyone else would look at us like we'd gone quite mad. Mar 25, 2013 at 3:52
  • @JeromyFrench The word you're looking for is probably "obfuscation". It sounds like you do want an unpredictable and sparse namespace, in which case I'd certainly consider using random numbers and adding a qualifier like '123121/2' for de-duplication. You could alternately generate a new random number when there's a collision or use a large random value to render collision proabilities near zero. Mar 25, 2013 at 3:54
  • @JeromyFrench For example, SELECT to_hex((random()*10^15)::bigint) || to_hex((random()*10^15)::bigint) would do a pretty decent job. Mar 25, 2013 at 4:04
1

The error is a little misleading; the md5() function exists, just not for working on integers. Use an embedded CAST() function to convert the integer field into text, and it will work:

SELECT request_id, md5(CAST(request_id AS TEXT))
FROM Request
ORDER BY request_id

--1;"c4ca4238a0b923820dcc509a6f75849b"
--2;"c81e728d9d4c2f636f067f89cc14862c"
--etc
0

I'm just trying to generate a non-obvious, non-repeating, value which can be used to look-up a record.

What you need is a bijective hash. I use the CRC CPU function in a custom C function. For CPUs that don't have the function, one can use a lookup table.

This approach is guaranteed to give you a unique "random looking" number for every unique 32-bit input.

If you know how to generate C functions (not trivial), it will be a simple task to look up how to use CRC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.