gzip

gzip and gunzip functions.

Overview

MIXED 3rd Party Extension: pg_gzip : gzip and gunzip functions.

Information

Metadata

  • Latest Version: 1.0
  • Postgres Support: 17,16,15,14,13
  • Need Load: Shared library do not need explicit loading
  • Need DDL: Need CREATE EXTENSION DDL
  • Relocatable: Can not install to arbitrary schema
  • Trusted: Untrusted, Require Superuser to Create
  • Schemas: N/A
  • Requires: N/A

RPM / DEB

  • RPM Repo: PGDG
  • RPM Name: pgsql_gzip_$v*
  • RPM Ver : 1.0
  • RPM Deps: N/A
  • DEB Repo: PIGSTY
  • DEB Name: postgresql-$v-gzip
  • DEB Ver : 1.0
  • DEB Deps: N/A

Packages

OS Arch PG17 PG16 PG15 PG14 PG13
el8 x86_64 pgsql_gzip_17
PGDG 1.0.0
pgsql_gzip_16
PGDG 1.0.0
pgsql_gzip_15
PGDG 1.0.0
pgsql_gzip_14
PGDG 1.0.0
pgsql_gzip_13
PGDG 1.0.0
el8 aarch64 pgsql_gzip_17
PGDG 1.0.0
pgsql_gzip_16
PGDG 1.0.0
pgsql_gzip_15
PGDG 1.0.0
pgsql_gzip_14
PGDG 1.0.0
pgsql_gzip_13
PGDG 1.0.0
el9 x86_64 pgsql_gzip_17
PGDG 1.0.0
pgsql_gzip_16
PGDG 1.0.0
pgsql_gzip_15
PGDG 1.0.0
pgsql_gzip_14
PGDG 1.0.0
pgsql_gzip_13
PGDG 1.0.0
el9 aarch64 pgsql_gzip_17
PIGSTY 1.0.0
pgsql_gzip_16
PIGSTY 1.0.0
pgsql_gzip_15
PIGSTY 1.0.0
pgsql_gzip_14
PIGSTY 1.0.0
pgsql_gzip_13
PIGSTY 1.0.0
d12 x86_64 postgresql-17-gzip
PIGSTY 1.0.1
postgresql-16-gzip
PIGSTY 1.0.1
postgresql-15-gzip
PIGSTY 1.0.1
postgresql-14-gzip
PIGSTY 1.0.1
postgresql-13-gzip
PIGSTY 1.0.1
d12 aarch64 postgresql-17-gzip
PIGSTY 1.0.1
postgresql-16-gzip
PIGSTY 1.0.1
postgresql-15-gzip
PIGSTY 1.0.1
postgresql-14-gzip
PIGSTY 1.0.1
postgresql-13-gzip
PIGSTY 1.0.1
u22 x86_64 postgresql-17-gzip
PIGSTY 1.0.1
postgresql-16-gzip
PIGSTY 1.0.1
postgresql-15-gzip
PIGSTY 1.0.1
postgresql-14-gzip
PIGSTY 1.0.1
postgresql-13-gzip
PIGSTY 1.0.1
u22 aarch64 postgresql-17-gzip
PIGSTY 1.0.1
postgresql-16-gzip
PIGSTY 1.0.1
postgresql-15-gzip
PIGSTY 1.0.1
postgresql-14-gzip
PIGSTY 1.0.1
postgresql-13-gzip
PIGSTY 1.0.1
u24 x86_64 postgresql-17-gzip
PIGSTY 1.0.1
postgresql-16-gzip
PIGSTY 1.0.1
postgresql-15-gzip
PIGSTY 1.0.1
postgresql-14-gzip
PIGSTY 1.0.1
postgresql-13-gzip
PIGSTY 1.0.1
u24 aarch64 postgresql-17-gzip
PIGSTY 1.0.1
postgresql-16-gzip
PIGSTY 1.0.1
postgresql-15-gzip
PIGSTY 1.0.1
postgresql-14-gzip
PIGSTY 1.0.1
postgresql-13-gzip
PIGSTY 1.0.1

Installation

Install gzip via the pig CLI tool:


pig ext install pg_gzip; # Extension Namepig ext install gzip; # normalized package name

Install pg_gzip via Pigsty playbook:

./pgsql.yml -t pg_extension -e '{"pg_extensions": ["pg_gzip"]}' # -l <cls>

Install pg_gzip RPM from YUM repo directly:

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*;

Install pg_gzip DEB from APT repo directly:

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;

Enable gzip extension on PostgreSQL cluster:

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]) returns BYTEA
  • gzip(uncompressed TEXT, [compression_level INTEGER]) returns BYTEA
  • gunzip(compressed BYTEA) returns BYTEA

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




Last modified 2025-02-17: add extension part (cfa504b)