PostgreSQL get_current_ts_config Usage Guide
In this article
The get_current_ts_config() function in PostgreSQL returns the name of the text search configuration currently being used by the session. It is used primarily to verify which language-specific rules (such as stemming and stop-word removal) are being applied to full-text search queries and indexes when a specific configuration is not explicitly named in the to_tsvector or to_tsquery functions.
What You'll Need
- A running PostgreSQL database instance.
- A client tool to execute SQL queries (such as psql, pgAdmin, or DBeaver).
- Appropriate permissions to connect to the database.
Step-by-Step
- Connect to your database: Open your preferred SQL interface and connect to the specific database where you are performing full-text search operations.
- Check the default configuration: Run the following query to see the current active text search configuration:
The result will be a single string, such asSELECT get_current_ts_config();'english'or'simple'. - Change the configuration for the session: If the returned value is not the language you need, you can change the current configuration using the
set_configfunction or theSETcommand. For example, to change the configuration to Spanish for the current session, run:SET default_text_search_config = 'spanish'; - Verify the change: Execute the
get_current_ts_config()function again to ensure the session has updated to the new configuration:SELECT get_current_ts_config(); - Test the effect on search: Use a function like
to_tsvectorwithout specifying a configuration to see how the current session setting affects the result:
The tokens generated will now follow the rules of the configuration returned bySELECT to_tsvector('The foxes jumped over the lazy dog');get_current_ts_config().
Common Mistakes
- Confusing Session vs. Global Settings: Using
SET default_text_search_configonly changes the configuration for the current database connection. It does not change the default for all users or future sessions. - Assuming a Configuration Exists: Attempting to set a configuration (e.g.,
'french') that has not been installed or created in the database will result in an error. - Ignoring Explicit Arguments: If you call
to_tsvector('english', 'text'), PostgreSQL uses 'english' regardless of whatget_current_ts_config()returns. The session configuration is only a fallback for when the configuration argument is omitted.
Tips
- Use for Debugging: Use
get_current_ts_config()inside complex functions or triggers to log which configuration was active during a specific search operation to debug inconsistent search results. - The 'simple' Configuration: If you want a configuration that does not perform stemming (simply splitting words by whitespace and converting to lowercase), set your config to
'simple'. - Check Available Configs: To see all available text search configurations in your database, query the
pg_ts_configsystem catalog:SELECT cfgname FROM pg_ts_config;
Troubleshooting
If the function does not return the expected value or you encounter errors, try the following steps:
- Check Permissions: Ensure your user has the right to modify session settings. While
SETis generally available, some restricted environments may limit configuration changes. - Verify Spelling: Ensure the configuration name is spelled exactly as it appears in the
pg_ts_configtable. Configuration names are case-sensitive if they were created with double quotes. - Restart the Session: If a
SETcommand seems to have no effect, disconnect and reconnect to the database to ensure you are not dealing with a cached session state or a connection pooler (like PgBouncer) that might be resetting session variables. - Verify Extension Installation: If a specific language configuration is missing, ensure the necessary dictionary extensions are installed in your PostgreSQL distribution.
Frequently Asked Questions
What exactly does get_current_ts_config do?
It returns the name of the text search configuration that PostgreSQL uses by default for the current session when no specific configuration is provided in full-text search functions.
Can I change the result of this function?
Yes, by using the command SET default_text_search_config = 'config_name';, which updates the configuration for the duration of your current database session.
Does this function affect index performance?
The function itself is a simple lookup and has no impact on performance. However, the configuration it returns determines how to_tsvector processes text, which is critical for matching the configuration used when the GIN or GiST index was originally created.
What happens if I don't set a configuration?
PostgreSQL uses the value defined in the default_text_search_config parameter, which is typically set at the database or cluster level. get_current_ts_config() will return this system default.
Is this function available in all PostgreSQL versions?
Yes, this function is a standard part of PostgreSQL's full-text search capabilities and is available across all modern versions.