Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to use the MD5 message digest of some string as the primary key of a table. What datatype should I use for such a field? What select and insert statements should I write for the field?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
909 views
Welcome To Ask or Share your Answers For Others

1 Answer

The md5 hash as bytea will use only 16 bytes in instead of 32 for the hexa representation:

create table t (d bytea);
insert into t (d) values
    (digest('my_string', 'md5')),
    (decode(md5('my_string'), 'hex'));

Both forms above will work but to use the simpler digest function it is necessary to install the pgcrypto extension as superuser:

create extension pgcrypto;

Use the digest function or the combination of decode and md5 as above to search for a certain string:

select
    octet_length(d) ba_length,
    pg_column_size(d) ba_column,
    encode(d, 'hex') hex_representation,
    octet_length(encode(d, 'hex')) h_length,
    pg_column_size(encode(d, 'hex')) h_column
from t
where d = digest('my_string', 'md5')
;
 ba_length | ba_column |        hex_representation        | h_length | h_column 
-----------+-----------+----------------------------------+----------+----------
        16 |        17 | 3d212b21fad7bed63c1fb560c6a5c5d0 |       32 |       36
        16 |        17 | 3d212b21fad7bed63c1fb560c6a5c5d0 |       32 |       36

The pg_column_size value is the storage size. It is less than half for the bytea compared to the hexa representation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...