gzip
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
pg_gzip | 1.0.0 | UTIL | MIT | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 4010 | gzip | No | Yes | No | Yes | No | Yes | - |
| Related | bzip zstd http pg_net pg_curl pgjq pgjwt pg_smtp_client |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | MIXED | 1.0.0 | 1817161514 | pg_gzip | - |
| RPM | PGDG | 1.0.0 | 1817161514 | pg_gzip_$v | - |
| DEB | PIGSTY | 1.0.0 | 1817161514 | postgresql-$v-gzip | - |
Build
You can build the RPM / DEB packages for pg_gzip using pig build:
pig build pkg pg_gzip # build RPM / DEB packages
Install
You can install pg_gzip directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:
pig repo add pgsql -u # Add repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install pg_gzip; # Install for current active PG version
pig ext install -y pg_gzip -v 18 # PG 18
pig ext install -y pg_gzip -v 17 # PG 17
pig ext install -y pg_gzip -v 16 # PG 16
pig ext install -y pg_gzip -v 15 # PG 15
pig ext install -y pg_gzip -v 14 # PG 14
dnf install -y pg_gzip_18 # PG 18
dnf install -y pg_gzip_17 # PG 17
dnf install -y pg_gzip_16 # PG 16
dnf install -y pg_gzip_15 # PG 15
dnf install -y pg_gzip_14 # PG 14
apt install -y postgresql-18-gzip # PG 18
apt install -y postgresql-17-gzip # PG 17
apt install -y postgresql-16-gzip # PG 16
apt install -y postgresql-15-gzip # PG 15
apt install -y postgresql-14-gzip # PG 14
Create Extension:
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])returnsBYTEAgzip(uncompressed TEXT, [compression_level INTEGER])returnsBYTEAgunzip(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
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.