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

Categories

0 votes
958 views
in Technique[技术] by (71.8m points)

database - postgresql: data type for md5 message digest?

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...