-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Doing a btree lookup in `pg_extension` for every query is not free, and an extension introducing one can (based on my experience) impact performance for single row lookups significantly. This introduces a cache which tracks if the extension is registered. I plan to use this cache in later PRs for more things, such as oids of our DuckDB functions. This cache is invalidated whenever an invalidation message is sent for the "duckdb" schema, which just so happens to be whenever `CREATE/DROP EXTENSION pg_duckdb` is executed. These invalidation messages are the mechanism that Postgres uses to let other backends know that some DDL has occured. We'll probably want to do something else for `ALTER EXTENSION pg_duckdb UPDATE`, but for now this is good enough (since there's no upgrades yet). It would have been best if we receive invalidation messages for rows in `pg_extension`. But sadly this is not possible. I submitted a patch to upstream Postgres to hopefully allow for this in the future (but that will only be in PG18+): https://www.postgresql.org/message-id/flat/CAGECzQTWm9sex719Hptbq4j56hBGUti7J9OWjeMobQ1ccRok9w%40mail.gmail.com
- Loading branch information
Showing
4 changed files
with
52 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace pgduckdb { | ||
bool IsExtensionRegistered(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
extern "C" { | ||
#include "postgres.h" | ||
|
||
#include "commands/extension.h" | ||
#include "utils/inval.h" | ||
#include "utils/syscache.h" | ||
} | ||
|
||
namespace pgduckdb { | ||
struct { | ||
bool valid; | ||
bool installed; | ||
} cache = {}; | ||
|
||
bool callback_is_configured = false; | ||
uint32 schema_hash_value; | ||
|
||
static void | ||
InvalidateCaches(Datum arg, int cache_id, uint32 hash_value) { | ||
if (hash_value != schema_hash_value) { | ||
return; | ||
} | ||
cache.valid = false; | ||
} | ||
|
||
bool | ||
IsExtensionRegistered() { | ||
if (cache.valid) { | ||
return cache.installed; | ||
} | ||
|
||
cache.installed = get_extension_oid("pg_duckdb", true) != InvalidOid; | ||
cache.valid = true; | ||
|
||
if (!callback_is_configured) { | ||
callback_is_configured = true; | ||
schema_hash_value = GetSysCacheHashValue1(NAMESPACENAME, CStringGetDatum("duckdb")); | ||
|
||
CacheRegisterSyscacheCallback(NAMESPACENAME, InvalidateCaches, (Datum)0); | ||
} | ||
|
||
return cache.installed; | ||
} | ||
|
||
} // namespace pgduckdb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters