跳转到主要内容

pg_tre

Native PostgreSQL index access method for approximate regular-expression matching

pg_tre : Native PostgreSQL index access method for approximate regular-expression matching

Overview

ID Extension Package Version Category License Language
2330 pg_tre pg_tre 4.5.2 FTS MIT AND BSD-2-Clause C
Attribute Has Binary Has Library Need Load Has DDL Relocatable Trusted
--s-d-- No Yes No Yes no no
Relationships
See Also pg_trgm pg_textsearch pg_fts vector pg_mentat
4.5.2 fixes newline-mode regex anchors and negated classes; no additional REINDEX from 4.5.1. index_check retains the 4.5.1 search_path fix. PG17-19 upstream; PGSTY RPM and DEB packages target PG17-18. Generic WAL since 4.0 needs no preload. The 3.x to 4.x WAL transition requires clean shutdown and physical standby rebuild. REINDEX indexes built with min_trigram_freq>1; fastupdate=false is refused; pending_list_limit defaults to 0. Formats 6-10 remain readable; 4.4+ writes format 11.

Packages

Type Repo Version PG Major Compatibility Package Pattern Dependencies
EXT PIGSTY 4.5.2 18 17 16 15 14 pg_tre -
RPM PIGSTY 4.5.2 18 17 16 15 14 pg_tre_$v -
DEB PIGSTY 4.5.2 18 17 16 15 14 postgresql-$v-pg-tre -
Linux / PG PG18 PG17 PG16 PG15 PG14
el8.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
el8.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
el9.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
el9.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
el10.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
el10.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
d12.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
d12.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
d13.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
d13.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
u22.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
u22.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
u24.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
u24.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
u26.x86_64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
u26.aarch64 PIGSTY 4.5.2 PIGSTY 4.5.2 N/A N/A N/A
Package Version OS ORG SIZE File URL
pg_tre_18 4.5.2 el8.x86_64 pigsty 591.3 KiB pg_tre_18-4.5.2-1PGSTY.el8.x86_64.rpm
pg_tre_18 4.5.1 el8.x86_64 pigsty 590.3 KiB pg_tre_18-4.5.1-1PGSTY.el8.x86_64.rpm
pg_tre_18 4.5.2 el8.aarch64 pigsty 575.1 KiB pg_tre_18-4.5.2-1PGSTY.el8.aarch64.rpm
pg_tre_18 4.5.1 el8.aarch64 pigsty 574.0 KiB pg_tre_18-4.5.1-1PGSTY.el8.aarch64.rpm
pg_tre_18 4.5.2 el9.x86_64 pigsty 561.9 KiB pg_tre_18-4.5.2-1PGSTY.el9.x86_64.rpm
pg_tre_18 4.5.1 el9.x86_64 pigsty 560.7 KiB pg_tre_18-4.5.1-1PGSTY.el9.x86_64.rpm
pg_tre_18 4.5.2 el9.aarch64 pigsty 552.6 KiB pg_tre_18-4.5.2-1PGSTY.el9.aarch64.rpm
pg_tre_18 4.5.1 el9.aarch64 pigsty 551.8 KiB pg_tre_18-4.5.1-1PGSTY.el9.aarch64.rpm
pg_tre_18 4.5.2 el10.x86_64 pigsty 568.4 KiB pg_tre_18-4.5.2-1PGSTY.el10.x86_64.rpm
pg_tre_18 4.5.1 el10.x86_64 pigsty 567.7 KiB pg_tre_18-4.5.1-1PGSTY.el10.x86_64.rpm
pg_tre_18 4.5.2 el10.aarch64 pigsty 557.2 KiB pg_tre_18-4.5.2-1PGSTY.el10.aarch64.rpm
pg_tre_18 4.5.1 el10.aarch64 pigsty 556.2 KiB pg_tre_18-4.5.1-1PGSTY.el10.aarch64.rpm
postgresql-18-pg-tre 4.5.2 d12.x86_64 pigsty 573.3 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~bookworm_amd64.deb
postgresql-18-pg-tre 4.5.2 d12.aarch64 pigsty 554.0 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~bookworm_arm64.deb
postgresql-18-pg-tre 4.5.2 d13.x86_64 pigsty 575.6 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~trixie_amd64.deb
postgresql-18-pg-tre 4.5.2 d13.aarch64 pigsty 557.3 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~trixie_arm64.deb
postgresql-18-pg-tre 4.5.2 u22.x86_64 pigsty 587.7 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~jammy_amd64.deb
postgresql-18-pg-tre 4.5.2 u22.aarch64 pigsty 577.9 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~jammy_arm64.deb
postgresql-18-pg-tre 4.5.2 u24.x86_64 pigsty 571.6 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~noble_amd64.deb
postgresql-18-pg-tre 4.5.2 u24.aarch64 pigsty 561.5 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~noble_arm64.deb
postgresql-18-pg-tre 4.5.2 u26.x86_64 pigsty 568.5 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~resolute_amd64.deb
postgresql-18-pg-tre 4.5.2 u26.aarch64 pigsty 556.8 KiB postgresql-18-pg-tre_4.5.2-1PGSTY~resolute_arm64.deb
Package Version OS ORG SIZE File URL
pg_tre_17 4.5.2 el8.x86_64 pigsty 592.1 KiB pg_tre_17-4.5.2-1PGSTY.el8.x86_64.rpm
pg_tre_17 4.5.1 el8.x86_64 pigsty 590.9 KiB pg_tre_17-4.5.1-1PGSTY.el8.x86_64.rpm
pg_tre_17 4.5.2 el8.aarch64 pigsty 575.9 KiB pg_tre_17-4.5.2-1PGSTY.el8.aarch64.rpm
pg_tre_17 4.5.1 el8.aarch64 pigsty 574.9 KiB pg_tre_17-4.5.1-1PGSTY.el8.aarch64.rpm
pg_tre_17 4.5.2 el9.x86_64 pigsty 562.5 KiB pg_tre_17-4.5.2-1PGSTY.el9.x86_64.rpm
pg_tre_17 4.5.1 el9.x86_64 pigsty 561.5 KiB pg_tre_17-4.5.1-1PGSTY.el9.x86_64.rpm
pg_tre_17 4.5.2 el9.aarch64 pigsty 553.3 KiB pg_tre_17-4.5.2-1PGSTY.el9.aarch64.rpm
pg_tre_17 4.5.1 el9.aarch64 pigsty 552.4 KiB pg_tre_17-4.5.1-1PGSTY.el9.aarch64.rpm
pg_tre_17 4.5.2 el10.x86_64 pigsty 569.0 KiB pg_tre_17-4.5.2-1PGSTY.el10.x86_64.rpm
pg_tre_17 4.5.1 el10.x86_64 pigsty 568.4 KiB pg_tre_17-4.5.1-1PGSTY.el10.x86_64.rpm
pg_tre_17 4.5.2 el10.aarch64 pigsty 558.1 KiB pg_tre_17-4.5.2-1PGSTY.el10.aarch64.rpm
pg_tre_17 4.5.1 el10.aarch64 pigsty 557.0 KiB pg_tre_17-4.5.1-1PGSTY.el10.aarch64.rpm
postgresql-17-pg-tre 4.5.2 d12.x86_64 pigsty 574.0 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~bookworm_amd64.deb
postgresql-17-pg-tre 4.5.2 d12.aarch64 pigsty 554.5 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~bookworm_arm64.deb
postgresql-17-pg-tre 4.5.2 d13.x86_64 pigsty 576.2 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~trixie_amd64.deb
postgresql-17-pg-tre 4.5.2 d13.aarch64 pigsty 557.9 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~trixie_arm64.deb
postgresql-17-pg-tre 4.5.2 u22.x86_64 pigsty 644.8 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~jammy_amd64.deb
postgresql-17-pg-tre 4.5.2 u22.aarch64 pigsty 631.8 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~jammy_arm64.deb
postgresql-17-pg-tre 4.5.2 u24.x86_64 pigsty 572.7 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~noble_amd64.deb
postgresql-17-pg-tre 4.5.2 u24.aarch64 pigsty 562.1 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~noble_arm64.deb
postgresql-17-pg-tre 4.5.2 u26.x86_64 pigsty 569.2 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~resolute_amd64.deb
postgresql-17-pg-tre 4.5.2 u26.aarch64 pigsty 557.4 KiB postgresql-17-pg-tre_4.5.2-1PGSTY~resolute_arm64.deb

Source

pig build pkg pg_tre;		# 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 pg_tre;		# install via package name, for the active PG version

pig install pg_tre -v 18;   # install for PG 18
pig install pg_tre -v 17;   # install for PG 17

Create this extension with:

CREATE EXTENSION pg_tre;

Usage

Sources:

pg_tre 4.5.2 provides the tre native index access method for approximate regular-expression matching over text. Upstream supports PostgreSQL 17–19; Pigsty’s 4.5.2 packages target PostgreSQL 17–18. It narrows candidates with codepoint trigrams and postings, then checks matches against the heap. Use it for typo-tolerant identifiers, log messages and lexical patterns with explicit edit costs.

The index write path uses PostgreSQL’s generic WAL facility. Since 4.0.0, CREATE EXTENSION is sufficient: shared_preload_libraries and a restart are not required to enable a new installation. Extension creation requires a superuser, and the control file is untrusted and non-relocatable.

CREATE EXTENSION pg_tre;
CREATE TABLE documents (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  body text NOT NULL
);
INSERT INTO documents (body) VALUES
  ('The PostgreSQL database system'),
  ('The Postgres databse system');
CREATE INDEX documents_body_tre ON documents USING tre (body);
SELECT id, body, body <@> tre_pattern('database', 1) AS distance
FROM documents
WHERE body %~~ tre_pattern('database', 1)
ORDER BY distance ASC NULLS LAST, id;

PostgreSQL 19 uses the core regular-expression character classifier; ICU character-class edge cases on PostgreSQL 17–18 may differ from native PostgreSQL regex semantics, as described upstream.

Patterns and Objects

  • tre_pattern(text) uses pg_tre.default_max_cost; tre_pattern(text, int4) supplies a maximum cost; the five-argument form also sets insertion, deletion and substitution costs. Patterns use POSIX extended regular expressions and TRE approximate budgets.
  • text %~~ tre_pattern is the indexable approximate-match predicate; text <@> tre_pattern returns the edit cost or NULL when no match fits the budget.
  • tre_distance, tre_similarity, tre_amatch, tre_amatch_cost and tre_amatch_detail expose matching and compatibility functions.
  • The index operator family also supports LIKE, ILIKE, regular-expression and equality predicates with heap rechecks. The separately named trigram functions coexist with pg_trgm; pg_tre does not install the colliding bare trigram operators removed in 3.0.

Indexes accept one text column with tre_text_ops. Expression and partial indexes are available, but index-only scans are not. Short patterns, high edit budgets and non-literal regexes can produce broad candidate sets; inspect real plans and keep cancellation and query-time limits enabled.

Maintenance and Limits

fastupdate must be true; setting it false is now rejected because the direct path could not preserve the index format. Existing off settings can still be opened, but inserts use the pending list. pending_list_limit is in KiB and defaults to 0, meaning unlimited. A positive value triggers a synchronous merge when the threshold is crossed; that inserting backend pays the work, and if another merge is active the limit can temporarily be exceeded.

pg_tre.min_trigram_freq must remain 1. Values above 1 formerly omitted rare postings and could lose results; they are now rejected. Keep regex state and compilation/matching limits bounded for untrusted patterns. Index construction uses tuplesort and temporary disk, with memory governed by maintenance_work_mem; estimate large builds before scheduling them.

SELECT * FROM tre_estimate_index_build('documents'::regclass, 2);
SELECT * FROM pg_tre_index_pending('documents_body_tre'::regclass);
SELECT * FROM pg_tre_index_check('documents_body_tre'::regclass, 2);

pg_tre_index_check compares index answers with sequential scans. Each sample performs multiple sequential scans, so bound the sample count on large tables. Version 4.5.1 fixes its search-path handling, pins trusted lookup paths and qualifies calls; use the fixed version before running this helper as a privileged user.

Upgrade Boundaries

Install matching files, ensure sessions use the new library, and update each database:

ALTER EXTENSION pg_tre UPDATE TO '4.5.2';

Crossing from 3.x to 4.x changes WAL record types even though the index page format is unchanged. Shut down cleanly before replacing the library; a physical standby cannot run a different major extension version from its primary and must be rebuilt from a fresh base backup after the primary upgrade. There is no rolling upgrade across this boundary. Remove any old wal_consistency_checking = 'pg_tre' setting; it prevents startup in 4.x. Upstream recommends wal_consistency_checking = 'all' when consistency checking is needed.

Version 4.4 writes format 11 and reads formats 6–10. Upgrading from those formats does not require a routine rebuild; optional REINDEX obtains the newer dense-posting representation. Downgrading to 4.3 after a 4.4+ write requires rebuilding the affected indexes for the older format. Older upgrades may cross additional boundaries described in the changelog.

REINDEX every index built with min_trigram_freq greater than 1: installing the fix cannot restore omitted postings. Version 4.5 also disables stale SuRF acceleration on indexes from earlier affected versions, restoring correctness immediately; a rebuild is optional to regain that acceleration. Upgrading 4.5.0 to 4.5.1 changes neither format nor ordinary query behavior and requires no additional REINDEX. Old definitions using the removed tuple bloom options must still be corrected before rebuilding.

4.5.2 Regex Correction

Version 4.5.2 fixes newline-mode (?n) anchors and negated character classes. It retains the index format and needs no additional REINDEX when upgrading from 4.5.1. The older WAL-transition and omitted-posting rebuild requirements above still apply when crossing those affected versions.

SELECT E'a\nfoo' %~~ tre_pattern('(?n)^foo', 0) AS line_start,
       E'foo\n' %~~ tre_pattern('(?n)foo$', 0) AS line_end,
       E'\n' %~~ tre_pattern('(?n)[^x]', 0) AS negated_class;

The expected results are true, true and false. The fix affects matching and index rechecks alike; patterns without newline mode retain their previous behavior.