Skip to content

Removing the need for Listener #4270

@steve-chavez

Description

@steve-chavez

Problem

The Listener causes several problems:

While it's possible postgres itself improves in some of the above areas, a listen channel is still an extra long lived connection, which adds maintenance burden for the user. And I'm thinking that maybe we can do the schema cache reload in another way..

Solution

The pre_config is a function that just sets some GUCs. What if we set a schema cache version there as:

create or replace function postgrest.pre_config()
returns void as $$
  select
      set_config('pgrst.db_schemas', 'schema1, schema2', true)
    , set_config('pgrst.scache_version', 1, true);
$$ language sql;

An event trigger then can redefine this function and increase the pgrst.scache_version each time a db object changes.

Then during transaction variable setup, we always execute the pre_config, which as a constant cost so it shouldn't affect performance that much:

select
  -- usual set_configs...
  postgrest.pre_config(..);

Then during execution of the main query, we read the scache version:

WITH pgrst_source AS (...)
SELECT
    coalesce(json_agg(_postgrest_t), '[]') AS body,
    nullif(current_setting('response.headers', true), '') AS response_headers,
    nullif(current_setting('response.status', true), '') AS response_status,
    nullif(current_setting('pgrst.scache_version', true), '') AS scache_version,
FROM ( SELECT * FROM pgrst_source ) _postgrest_t;

And cache that version, if it doesn't change we just serve the request as usual. If the version changes, we reload the schema cache.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideaNeeds of discussion to become an enhancement, not ready for implementation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions