Introduction

PostgreSQL full-text search is commonly used in applications that need to search articles, product descriptions, documentation, support tickets, and other text-based information. It allows developers to search for meaningful words instead of relying only on simple string matching. With the help of tsvector, tsquery, and GIN indexes, PostgreSQL can perform these searches efficiently, even when tables contain a large amount of text.

However, upgrading to a new PostgreSQL major version requires more than checking whether the database starts successfully. Developers and database administrators should also verify that indexes and stored search data continue to produce the expected results.

PostgreSQL 19 introduces an important change to full-text search. It now uses the database's locale provider for character classification when parsing text. Earlier versions used libc for this purpose, regardless of the database's locale provider. This change can affect search results in databases that use ICU or the builtin locale provider, particularly when their locale settings behave differently from the database's LC_CTYPE setting.

For databases upgraded using pg_upgrade, PostgreSQL recommends rebuilding indexes that use full-text search and recomputing stored tsvector values in the affected locale configurations.

In this article, we will explore how to identify full-text search indexes, inspect their definitions, test search behavior, and rebuild indexes safely after upgrading to PostgreSQL 19.

Understanding the PostgreSQL 19 Full-Text Search Change

PostgreSQL uses a text search configuration to determine how text should be processed. A configuration defines the parser and dictionaries used to identify words, remove stop words, and apply stemming.

For example, the English configuration can process different forms of a word so that searches can match related terms. The text is converted into a tsvector value, which stores normalized lexemes that can be searched using a tsquery expression.

PostgreSQL 19 changes how full-text search and pg_trgm classify characters during parsing. Instead of always relying on libc, these functions now use the database's configured locale provider.

This matters when an application uses a locale provider such as ICU or builtin and its character classification differs from the LC_CTYPE setting used by the database.

Consider a database that uses ICU for its locale provider but has an LC_CTYPE setting of C. Text containing non-ASCII characters may be classified differently after the upgrade. As a result, the generated search vector can differ from the value produced by the previous PostgreSQL version.

This can affect two types of data:

  • Indexes that depend on full-text search or pg_trgm operations.

  • Stored tsvector values that were generated before the upgrade.

It is important to understand that this does not mean every PostgreSQL 19 upgrade requires rebuilding every full-text index. The recommendation applies to the affected locale configurations when upgrading with pg_upgrade. Review the database locale settings and the PostgreSQL 19 migration notes before deciding which objects need maintenance.

Identifying the Database Locale Configuration

Before rebuilding indexes, check the locale provider and locale settings of the database.

You can use the following query:

Code
SELECT
    datname,
    datlocprovider,
    datcollate,
    datctype
FROM pg_database
WHERE datname = current_database();

The datlocprovider column identifies the locale provider used by the database. Depending on the configuration, it can indicate libc, ICU, or the builtin provider.

The datcollate and datctype columns show the database's collation and character classification settings.

Pay particular attention to databases that use a non-libc locale provider while having an LC_CTYPE setting that behaves differently from the configured locale provider.

If your application has multiple databases, run this check for each database. Locale settings belong to individual databases, so checking only the default database may leave other application databases unverified.

You should also record the results before upgrading. Comparing the old and new database configurations makes it easier to investigate unexpected search behavior.

Finding Existing Full-Text Search Indexes

The next step is to identify the indexes that depend on full-text search.

PostgreSQL provides system catalogs that contain information about tables, indexes, and index access methods. The pg_indexes view is a convenient starting point because it displays index definitions in readable SQL.

Run the following query:

VB.NET
SELECT
    schemaname,
    tablename,
    indexname,
    indexdef
FROM pg_indexes
WHERE indexdef ILIKE '%to_tsvector%'
   OR indexdef ILIKE '%tsvector%'
ORDER BY schemaname, tablename, indexname;

This query helps identify common full-text search indexes created with to_tsvector or stored tsvector columns.

For example, an application might have an index like this:

SQL
CREATE INDEX idx_articles_search
ON articles
USING GIN (
    to_tsvector(
        'english',
        coalesce(title, '') || ' ' || coalesce(body, '')
    )
);

The index stores searchable information generated from the title and body columns.

The query against pg_indexes is useful, but it should not be treated as a complete dependency analysis. Some applications use custom functions, generated columns, triggers, or extension-provided operators. Their definitions may not contain the exact text used in the filter.

Review application migrations and schema definitions as well. If the database uses pg_trgm, inspect those indexes separately because PostgreSQL 19's locale-related change also applies to that extension.

Checking Index Validity and Definitions

After identifying the relevant indexes, check whether PostgreSQL considers them valid and ready for use.

The pg_index catalog contains useful information about index state.

VB.NET
SELECT
    ns.nspname AS schema_name,
    tbl.relname AS table_name,
    idx.relname AS index_name,
    am.amname AS index_type,
    pi.indisvalid,
    pi.indisready,
    pg_get_indexdef(pi.indexrelid) AS index_definition
FROM pg_index pi
JOIN pg_class idx
    ON idx.oid = pi.indexrelid
JOIN pg_class tbl
    ON tbl.oid = pi.indrelid
JOIN pg_namespace ns
    ON ns.oid = tbl.relnamespace
JOIN pg_am am
    ON am.oid = idx.relam
WHERE am.amname IN ('gin', 'gist')
ORDER BY ns.nspname, tbl.relname, idx.relname;

This query lists GIN and GiST indexes along with their definitions and status.

The indisvalid column indicates whether an index is valid for query planning. The indisready column indicates whether the index is ready to receive new entries from table modifications.

These fields are useful when checking index health, especially after maintenance operations or a failed concurrent index build.

However, a valid index does not automatically mean that its contents match the text search behavior expected after the PostgreSQL 19 upgrade. An index can be structurally valid while still containing entries generated under the previous text classification behavior.

That is why index status checks should be combined with locale checks and search-result validation.

Testing Full-Text Search Results

Before rebuilding indexes, test how PostgreSQL 19 processes representative application text.

The ts_debug function is useful for understanding how a text search configuration parses individual words.

Code
SELECT *
FROM ts_debug(
    'english',
    'PostgreSQL supports multilingual search and indexing.'
);

The result shows how the parser identifies tokens and which dictionaries process them.

You can also inspect the generated tsvector directly:

Code
SELECT
    to_tsvector(
        'english',
        'PostgreSQL supports multilingual search and indexing.'
    ) AS search_vector;

Compare the output with results recorded before the upgrade, using the same database locale configuration and text search configuration.

For an application table, test actual search queries rather than relying only on a small example.

SQL
SELECT
    id,
    title
FROM articles
WHERE to_tsvector(
    'english',
    coalesce(title, '') || ' ' || coalesce(body, '')
) @@ plainto_tsquery(
    'english',
    'postgresql indexing'
);

Check whether the expected records are returned. Include examples containing accented characters, non-English text, punctuation, and other characters that are important to your application.

This is particularly useful for applications that support multiple languages or allow users to search names and descriptions containing non-ASCII characters.

Rebuilding Full-Text Search Indexes

If your database falls within the affected locale configuration and was upgraded using pg_upgrade, rebuild the indexes that use full-text search or pg_trgm.

For an individual index, PostgreSQL provides REINDEX.

Code
REINDEX INDEX idx_articles_search;

For production systems, consider using REINDEX INDEX CONCURRENTLY when the index and database conditions allow it.

Code
REINDEX INDEX CONCURRENTLY idx_articles_search;

Concurrent rebuilding reduces the time during which normal writes are blocked, although it takes longer and requires additional disk space. It also has operational restrictions, including that it cannot run inside an explicit transaction block.

You can rebuild a specific table's indexes when the maintenance plan calls for it:

Code
REINDEX TABLE articles;

Be careful with this command because it rebuilds all indexes associated with the table, not only full-text search indexes. If the table contains several large indexes, rebuilding all of them may consume unnecessary time and resources.

For large production databases, identify the affected indexes first and create a maintenance plan that accounts for disk space, write activity, replication, and application traffic.

After rebuilding, run the index inspection query again and verify that the relevant indexes are valid and ready.

Recomputing Stored tsvector Values

Rebuilding an index is not enough when the application stores tsvector values in table columns.

For example, consider an articles table with a search_vector column that is maintained manually or through a trigger.

Code
CREATE TABLE articles (
    id BIGSERIAL PRIMARY KEY,
    title TEXT,
    body TEXT,
    search_vector TSVECTOR
);

The application might populate the search vector whenever an article is inserted or updated.

After the PostgreSQL 19 upgrade, existing values may still represent the text classification behavior of the previous version. They should be recomputed when the database falls within the affected configuration.

For a manually maintained search_vector column, the following statement recalculates the stored values:

SQL
UPDATE articles
SET search_vector = to_tsvector(
    'english',
    coalesce(title, '') || ' ' || coalesce(body, '')
);

For a large table, avoid running a massive update without checking its impact. It can generate substantial WAL, increase replication lag, create table bloat, and affect application performance.

Consider processing records in controlled batches using an appropriate key range. Monitor database storage, replication lag, and application response times while the update runs.

Generated stored columns require a different approach because PostgreSQL does not allow applications to assign values to them directly. Review the generated expression and use a controlled migration or update strategy to force recomputation. Test this process on a copy of the production schema before applying it to a large table.

After recomputing stored values, verify that the associated indexes are built from the refreshed data.

Validating Search Performance After Maintenance

Once the indexes have been rebuilt and stored vectors recomputed, test both correctness and performance.

Start by executing the application's normal search queries and checking whether expected records are returned. Compare the results with the test cases collected before the upgrade.

Next, inspect the query execution plan.

Code
EXPLAIN (ANALYZE, BUFFERS)
SELECT
    id,
    title
FROM articles
WHERE search_vector @@ plainto_tsquery(
    'english',
    'postgresql indexing'
);

The execution plan helps determine whether PostgreSQL is using the expected index and how much work the query performs.

Do not assume that an index must always appear in the plan. PostgreSQL may choose a sequential scan when the table is small or when a query matches a large percentage of rows. The important point is to verify that the plan is reasonable for the application's actual workload.

Also check application-level behavior. A search query may execute successfully but return different records because tokenization or stemming changed. Functional tests should cover these cases, particularly when search results are used in customer-facing features.

Best Practices for Production Upgrades

A reliable PostgreSQL 19 upgrade should include full-text search validation as part of the migration checklist.

Before upgrading, record database locale settings, identify full-text search and pg_trgm indexes, and save representative search results. Include custom text search configurations and stored tsvector columns in the inventory.

After upgrading, compare locale settings and run the same search tests. Rebuild affected indexes and recompute stored vectors where required by the PostgreSQL 19 migration guidance.

For large databases, schedule maintenance carefully. Concurrent index rebuilding can reduce write disruption, but it does not eliminate disk-space requirements or operational overhead. Stored-vector recomputation should also be planned to avoid generating excessive WAL and replication lag.

Finally, keep a rollback plan and test the complete process in a staging environment. A successful database upgrade should preserve both the integrity of stored information and the search behavior expected by the application.

Common Mistakes to Avoid

One common mistake is assuming that every full-text search index must be rebuilt after upgrading. The PostgreSQL 19 recommendation is tied to the locale-provider change and the affected database configurations. Check the actual locale settings before scheduling unnecessary index maintenance.

Another mistake is rebuilding indexes but ignoring stored tsvector columns. An index built from outdated stored values can still produce results that do not match the newly expected text processing behavior.

It is also easy to overlook pg_trgm indexes. PostgreSQL 19 explicitly includes pg_trgm in the locale-related change, so applications using similarity searches should review those indexes as well.

Finally, do not use index validity as the only verification method. A valid index can still contain data generated under earlier text-processing behavior. Test real search results and compare them with expected application behavior.

Summary

PostgreSQL 19 changes how full-text search and pg_trgm classify characters by using the database's locale provider. This can affect search behavior in databases where the configured locale provider differs from the previous libc-based character classification.

When upgrading with pg_upgrade, databases using affected locale configurations should have their full-text search and pg_trgm indexes rebuilt. Stored tsvector values should also be recomputed so that they reflect the new parsing behavior.

By checking locale settings, inspecting index definitions, testing representative queries, rebuilding affected indexes, and refreshing stored search vectors, development teams can reduce the risk of unexpected search results after a PostgreSQL 19 upgrade.