gzip
gzip and gunzip functions.
Overview
Attributes
Yes | No | Yes | No | Yes | Yes | No | - |
Packages
EL | PGDG | pgsql_gzip_$v* | 1.0.0 | - | 17 16 15 14 13 |
Debian | PIGSTY | postgresql-$v-gzip | 1.0.1 | - | 17 16 15 14 13 |
Availability
PG17 | PG16 | PG15 | PG14 | PG13 | |
---|---|---|---|---|---|
el8.x86_64 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 |
el8.aarch64 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 |
el9.x86_64 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 |
el9.aarch64 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 | 1.0.0 |
d12.x86_64 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 |
d12.aarch64 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 |
u22.x86_64 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 |
u22.aarch64 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 |
u24.x86_64 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 |
u24.aarch64 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 | 1.0.1 |
Download
To add the required PGDG / PIGSTY upstream repository, use:
pig repo add pgdg -u # add PGDG repo and update cache (leave existing repos)
pig repo add pigsty -u # add PIGSTY repo and update cache (leave existing repos)
pig repo add pgsql -u # add PGDG + Pigsty repo and update cache (leave existing repos)
pig repo set all -u # set repo to all = NODE + PGSQL + INFRA (remove existing repos)
./node.yml -t node_repo -e node_repo_modules=node,pgsql # -l <cluster>
Or download the latest packages directly:
Install
Install this extension with:
pig ext install gzip; # install by extension name, for the current active PG version
pig ext install pg_gzip; # install via package alias, for the active PG version
pig ext install gzip -v 17; # install for PG 17
pig ext install gzip -v 16; # install for PG 16
pig ext install gzip -v 15; # install for PG 15
pig ext install gzip -v 14; # install for PG 14
pig ext install gzip -v 13; # install for PG 13
dnf install pgsql_gzip_17*;
dnf install pgsql_gzip_16*;
dnf install pgsql_gzip_15*;
dnf install pgsql_gzip_14*;
dnf install pgsql_gzip_13*;
apt install postgresql-17-gzip;
apt install postgresql-16-gzip;
apt install postgresql-15-gzip;
apt install postgresql-14-gzip;
apt install postgresql-13-gzip;
./pgsql.yml -t pg_ext -e '{"pg_extensions": ["pg_gzip"]}' # -l <cls>
Create this extension with:
CREATE EXTENSION gzip;
Usage
Sometimes you just need to compress your bytea
object before you return it to the client.
Sometimes you receive a compressed bytea
from the client, and you have to uncompress it before you can work with it.
This extension is for that.
This extension is not for storage compression. PostgreSQL already does tuple compression on the fly if your tuple gets large enough, manually pre-compressing your data using this function won't make things smaller.
gzip(uncompressed BYTEA, [compression_level INTEGER])
returnsBYTEA
gzip(uncompressed TEXT, [compression_level INTEGER])
returnsBYTEA
gunzip(compressed BYTEA)
returnsBYTEA
Examples
SELECT gzip('this is my this is my this is my this is my text');
gzip
\x1f8b08000000000000132bc9c82c5600a2dc4a851282ccd48a12002e7a22ff30000000
Wait, what, the compressed output is longer?!? No, it only looks that way, because in hex every byte is represented with two hex digits. The original string looks like this in hex:
SELECT 'this is my this is my this is my this is my text'::bytea;
bytea
\x74686973206973206d792074686973206973206d792074686973206973206d792074686973206973206d792074657874
For really long, repetitive things, compression naturally works like a charm:
SELECT gzip(repeat('this is my ', 100));
bytea
\x1f8b08000000000000132bc9c82c5600a2dc4a859251e628739439ca24970900d1341c5c4c040000
To convert a bytea
back into an equivalent text
you must use the encode()
function with the escape
encoding.
SELECT encode('test text'::bytea, 'escape'); encode
test text
SELECT encode(gunzip(gzip('this text has been compressed and then decompressed')), 'escape')
encode
this text has been compressed and then decompressed