Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL get_current_ts_config Usage Guide

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Connect to your database: Open your preferred SQL interface and connect to the specific database where you are performing full-text search operations.
  2. Check the default configuration: Run the following query to see the current active text search configuration:
    SELECT get_current_ts_config();
    The result will be a single string, such as 'english' or 'simple'.
  3. 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_config function or the SET command. For example, to change the configuration to Spanish for the current session, run:
    SET default_text_search_config = 'spanish';
  4. 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();
  5. Test the effect on search: Use a function like to_tsvector without specifying a configuration to see how the current session setting affects the result:
    SELECT to_tsvector('The foxes jumped over the lazy dog');
    The tokens generated will now follow the rules of the configuration returned by get_current_ts_config().

Common Mistakes

  • Confusing Session vs. Global Settings: Using SET default_text_search_config only 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 what get_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_config system 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 SET is 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_config table. Configuration names are case-sensitive if they were created with double quotes.
  • Restart the Session: If a SET command 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.