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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…