Skip to content

vgram

Variable-length gram GIN indexes and statistics for LIKE/ILIKE search

vgram · Variable-length gram GIN indexes and statistics for LIKE/ILIKE search

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2130vgramvgram1.0FTSPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
Relationships
See Alsopg_trgm pg_bigm biscuit pgroonga
Collect corpus-specific q-gram statistics before creating the GIN index; upstream supports PostgreSQL 13+.

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY1.019 18 17 16 15vgram-
RPMPIGSTY1.019 18 17 16 15vgram_$v-
DEBPIGSTY1.019 18 17 16 15postgresql-$v-vgram-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el8.aarch64N/APIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el9.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el9.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el10.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
el10.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d12.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d12.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d13.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
d13.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u22.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u22.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u24.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u24.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u26.x86_64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0
u26.aarch64PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0PIGSTY 1.0

Source

pig build pkg vgram;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install vgram;		# install by package name

pig install vgram -v 19;   # install for PG 19
pig install vgram -v 18;   # install for PG 18
pig install vgram -v 17;   # install for PG 17
pig install vgram -v 16;   # install for PG 16
pig install vgram -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION vgram;

Usage

Sources:

vgram accelerates LIKE and ILIKE substring searches with GIN indexes built from variable-length q-grams. Unlike fixed trigrams, it learns frequent short grams from the target corpus and indexes longer or rarer grams, which can improve selectivity for a particular data distribution.

Build a Corpus-Specific Index

Version 1.0 requires PostgreSQL 13 or later. In psql, collect frequent grams, save the result, and pass it to the vgram_gin_ops options:

CREATE EXTENSION vgram;

SELECT qgram_stat(title, 2, 4, 0.05) AS vgrams
FROM documents
\gset

CREATE INDEX documents_title_vgram_idx
ON documents USING gin (
  title vgram_gin_ops (minq=2, maxq=4, vgrams=:'vgrams')
);

SELECT *
FROM documents
WHERE title ILIKE '%indexing%';

minq and maxq bound extracted gram lengths. The threshold passed to qgram_stat() marks grams at or above that corpus frequency as too common to index. Use get_vgrams() to inspect extraction for a candidate value and learned gram array.

Objects and Advanced Statistics

  • qgram_stat(text, int, int, float8) aggregates frequent q-grams.
  • get_vgrams(text, int, int, text[]) shows the grams selected for text.
  • vgram_gin_ops indexes ordinary text for LIKE and ILIKE.
  • vgram_text is text-compatible but installs custom frequency statistics and selectivity estimation.
  • vgram_gin_ops2 is the GIN operator class for vgram_text.

Maintenance Boundaries

The learned option array is embedded in the index definition and can be very large. It reflects the sample at build time; major language or distribution changes require new statistics, comparison of recall/plans, and usually REINDEX or a replacement index. GIN candidates are rechecked, but overly common or too-short patterns may still fall back to broad scans. Benchmark build time, index size, write amplification, pending-list behavior, VACUUM, collation, multibyte text, case folding, and planner estimates. Keep the training query and parameters reproducible so replicas and rebuilt environments use the same operator-class options.

Table reader