If your PostgreSQL application uses pg_trgm for search, a major PostgreSQL upgrade is a good time to check your trigram indexes. The application may continue to run normally after the upgrade, and your search queries may still return results, but that does not always mean that the existing indexes should automatically be left as they are.

The pg_trgm extension is commonly used when an application needs partial text searches, similarity searches, or searches that can handle spelling differences. It is especially useful for customer names, product names, addresses, document titles, and other text fields where users may not enter an exact value.

PostgreSQL 19 includes changes related to character classification used by pg_trgm. Because of these changes, affected trigram indexes may need to be rebuilt after the upgrade. The important part is understanding which indexes are using pg_trgm, checking whether they are affected, and rebuilding them safely.

This article explains what pg_trgm indexes are, why they may need attention after a PostgreSQL 19 upgrade, how to find them, how to rebuild them, and what to check before doing the same work in production.

What Is pg_trgm?

pg_trgm is a PostgreSQL extension that provides trigram-based text search functionality.

A trigram is a group of three consecutive characters from a piece of text. PostgreSQL can use these groups to compare text values and find strings that are similar or partially match a search value.

For example, consider this customer name:

Code
Rahul Kumar

A trigram-based search breaks the text into overlapping groups of characters. PostgreSQL can then use those groups when evaluating similarity or certain pattern searches.

This is useful when users do not know the exact spelling or when the application needs to search for part of a longer string.

You can enable the extension with:

Code
CREATE EXTENSION IF NOT EXISTS pg_trgm;

The extension is installed at the database level, so make sure it is enabled in the database where your application queries are running.

Why Applications Use pg_trgm

A normal B-tree index is very useful for equality searches such as:

Code
SELECT *
FROM customers
WHERE email = '[email protected]';

However, applications often need queries such as:

Code
SELECT id, name
FROM customers
WHERE name ILIKE '%rahul%';

The search value is not necessarily the complete value stored in the column. The percent signs allow matching text before and after the search value.

This is where pg_trgm can become useful.

For example:

Code
CREATE INDEX customers_name_trgm_idx
ON customers
USING GIN (name gin_trgm_ops);

The index uses the gin_trgm_ops operator class provided by pg_trgm.

The exact benefit depends on the query and the data, so you should always verify the execution plan instead of assuming that every ILIKE query will automatically use the index.

Why PostgreSQL 19 Makes This Important

A PostgreSQL major upgrade can change internal behavior even when application SQL remains unchanged.

PostgreSQL 19 changes character classification behavior used by pg_trgm. This matters because trigram generation depends on how characters are classified.

The practical impact is that existing trigram indexes can contain information based on the behavior of the previous PostgreSQL version.

If the behavior used to generate or interpret those index entries changes after the upgrade, the index should be rebuilt so that it matches the new database behavior.

This does not mean that every application using pg_trgm will immediately experience a visible problem. The correct approach is to identify affected indexes and follow the PostgreSQL upgrade guidance rather than assuming the indexes are automatically correct.

Find pg_trgm Indexes in Your Database

Before rebuilding anything, first find out which indexes actually use trigram operator classes.

You can inspect indexes through PostgreSQL's catalog views.

A useful query is:

Code
SELECT
    schemaname,
    tablename,
    indexname,
    indexdef
FROM pg_indexes
WHERE indexdef ILIKE '%trgm%';

This gives you the index definitions that contain trgm.

You may see results similar to:

Code
public | customers | customers_name_trgm_idx
public | products  | products_name_trgm_idx

The exact output depends on your database.

This step is important because you should not rebuild every index in the database simply because PostgreSQL was upgraded. First identify the indexes that actually depend on the functionality being changed.

Check Whether the pg_trgm Extension Is Installed

You can check installed extensions with:

Code
SELECT
    extname,
    extversion
FROM pg_extension
WHERE extname = 'pg_trgm';

If the query returns a row, the extension is installed in the current database.

You can also check the extension version:

Code
SELECT
    extname,
    extversion
FROM pg_extension
ORDER BY extname;

This is useful when preparing an upgrade because you can record the extension state before changing the PostgreSQL version.

Rebuilding a pg_trgm Index

Once you have identified an affected index, you can rebuild it with REINDEX.

For a specific index:

Code
REINDEX INDEX customers_name_trgm_idx;

For all indexes associated with a table:

Code
REINDEX TABLE customers;

However, rebuilding an entire table's indexes may do more work than necessary. If the upgrade only requires attention for a particular trigram index, rebuilding that specific index is usually easier to reason about.

You can also use:

Code
REINDEX INDEX CONCURRENTLY customers_name_trgm_idx;

The concurrent form is useful when reducing blocking is important for a production workload.

However, it has additional operational considerations and can take longer or consume additional resources. It should be tested before being used during a production maintenance window.

REINDEX and REINDEX CONCURRENTLY

These two approaches solve the same basic problem but have different operational characteristics.

Method

Main benefit

Important consideration

REINDEX INDEX

Simple and generally faster

Can require stronger locking

REINDEX TABLE

Rebuilds indexes associated with a table

May rebuild more indexes than needed

REINDEX INDEX CONCURRENTLY

Reduces blocking of normal table activity

Can take longer and requires more planning

Recreate index manually

Gives you control over the definition

Requires more steps and can introduce mistakes

For a development or maintenance environment, a normal REINDEX INDEX may be perfectly reasonable.

For a busy production database, you need to consider application traffic, lock behavior, index size, available disk space, and maintenance duration before choosing the method.

Check the Index Definition Before Rebuilding

Before rebuilding an index, inspect its definition.

Code
SELECT
    indexname,
    indexdef
FROM pg_indexes
WHERE indexname = 'customers_name_trgm_idx';

You should verify that the index is actually using the expected operator class.

For example:

Code
CREATE INDEX customers_name_trgm_idx
ON customers
USING GIN (name gin_trgm_ops);

This also gives you a reference point if you need to recreate the index manually.

Do not change the index definition simply because you are rebuilding it. The goal of the upgrade operation should normally be to rebuild the existing index using the PostgreSQL 19 environment.

Test Search Queries Before Rebuilding

Before making changes, test the queries that depend on the trigram index.

For example:

Code
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, name
FROM customers
WHERE name ILIKE '%rahul%';

Save the output.

Then test representative searches from your application.

Do not test only one search term. If the application supports customer-name searches, test common names, partial names, unusual spellings, short search terms, and values containing characters relevant to your users.

The goal is to establish what normal behavior looks like before the index maintenance.

Test the Index After Rebuilding

After rebuilding the index, run the same query again:

Code
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, name
FROM customers
WHERE name ILIKE '%rahul%';

Compare the results.

You should look at whether the query still uses an appropriate access method, whether the estimated and actual rows are reasonable, and whether execution behavior is consistent with your expectations.

Remember that rebuilding an index does not guarantee that a query will suddenly become faster. PostgreSQL may decide that another execution strategy is better for a particular query.

The purpose of rebuilding is to ensure that the index is consistent with the current database behavior, not to force PostgreSQL to use it.

What About Multiple pg_trgm Indexes?

Large applications can have several trigram indexes.

For example:

SQL
CREATE INDEX customers_name_trgm_idx
ON customers
USING GIN (name gin_trgm_ops);

CREATE INDEX customers_address_trgm_idx
ON customers
USING GIN (address gin_trgm_ops);

CREATE INDEX products_title_trgm_idx
ON products
USING GIN (title gin_trgm_ops);

Do not manually maintain a list of indexes based only on memory.

Find them from the database:

Code
SELECT
    schemaname,
    tablename,
    indexname,
    indexdef
FROM pg_indexes
WHERE indexdef ILIKE '%trgm%';

Then create a maintenance list containing the database name, schema name, table name, index name, index definition, approximate index size, and application feature using the index.

This makes the upgrade process easier to review and repeat.

Check Index Sizes Before Production Maintenance

Index size matters when planning maintenance.

You can check the size of a specific index with:

Code
SELECT
    pg_size_pretty(pg_relation_size('customers_name_trgm_idx'))
    AS index_size;

For multiple indexes:

VB.NET
SELECT
    indexrelname AS index_name,
    pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_stat_user_indexes
WHERE indexrelname ILIKE '%trgm%';

Large indexes can take more time and require additional storage during maintenance operations.

This is one reason production maintenance should be planned instead of treating REINDEX as a harmless background command.

Common Mistakes

Rebuilding Every Index Without Checking

A PostgreSQL upgrade does not mean that every database index must be rebuilt.

Identify the affected functionality first and rebuild only what is required.

Rebuilding Only the Extension

Running:

Code
CREATE EXTENSION IF NOT EXISTS pg_trgm;

does not rebuild existing indexes.

The extension being installed or available and the indexes created using that extension are separate concerns.

Forgetting About Multiple Databases

Extensions are enabled per database.

If your PostgreSQL server contains multiple application databases, checking pg_trgm in one database does not automatically tell you what exists in the others.

Testing Only Whether the Query Returns Data

A query can return the correct result while still having a poor execution plan.

Use EXPLAIN ANALYZE BUFFERS for important search queries and also test the application behavior.

Running REINDEX During Heavy Traffic Without Planning

Index maintenance consumes database resources and can interact with application traffic.

Before rebuilding a large production index, understand the locking behavior, expected maintenance time, disk requirements, and impact on the application.

Troubleshooting After Rebuilding

If search performance is still poor after rebuilding a trigram index, do not immediately rebuild the index again.

First check the execution plan:

Code
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, name
FROM customers
WHERE name ILIKE '%rahul%';

Then check whether PostgreSQL is using the index.

If it is not, consider the query structure, search pattern, table size, statistics, and selectivity of the search condition.

For example, very short search strings may not provide enough useful trigram information. PostgreSQL may also decide that scanning the table is cheaper for a particular query.

The correct solution depends on the actual execution plan.

Best Practices for PostgreSQL 19 pg_trgm Maintenance

The safest approach is to make pg_trgm index maintenance part of your PostgreSQL upgrade checklist.

Before the upgrade, identify all databases using the extension and record the relevant indexes. After the upgrade, verify the extension and rebuild indexes affected by the PostgreSQL 19 changes.

Test important search queries before and after rebuilding. Use realistic data because search behavior can change significantly as the number and distribution of rows increase.

For production databases, consider using REINDEX INDEX CONCURRENTLY when appropriate and after testing its operational behavior. Also check available storage before starting large index maintenance operations.

Keep the original index definitions documented. If an operational problem occurs, having the exact index definition makes troubleshooting much easier.

Advantages and Disadvantages

Area

Advantages

Considerations

Search consistency

Rebuilding ensures the index matches the current PostgreSQL behavior

Requires planned maintenance

Compatibility

Helps address upgrade-related index changes

Not every search problem is caused by the index

Performance

A correctly rebuilt index can provide expected search behavior

Rebuilding alone does not guarantee faster queries

Operations

Specific indexes can be rebuilt individually

Large indexes can consume significant resources

Production safety

Concurrent rebuilding can reduce blocking

It requires additional planning and resources

Troubleshooting

Index definitions and plans are easy to validate

Poor query design can still cause slow searches

Summary

pg_trgm is a useful PostgreSQL extension for applications that need partial, similarity-based, or fuzzy text searches. Applications commonly use it for customer searches, product searches, document searches, and other features where exact matching is not enough.

A PostgreSQL 19 upgrade deserves special attention if your application depends on pg_trgm. The release includes changes related to character classification, which means affected trigram indexes should be reviewed and rebuilt as part of the upgrade process.

Start by finding the databases and indexes that use pg_trgm. Record their definitions and sizes, test important search queries, and then rebuild the required indexes using the appropriate REINDEX approach.

After rebuilding, run the same application searches and execution-plan checks again. If a query is still slow, investigate the actual query plan rather than repeatedly rebuilding the index.

The important part is to treat pg_trgm maintenance as a planned part of the PostgreSQL upgrade instead of an afterthought. A few database checks before and after the upgrade can help you avoid unexpected search problems when the application starts using PostgreSQL 19 in production.