Don't excessively look up the extension oid

Use the one from the static variable. This improves performance by
avoiding excessive catalog lookups.
This commit is contained in:
Alexander Kuzmenkov 2021-12-10 17:32:30 +03:00 committed by Alexander Kuzmenkov
parent d437c70df8
commit beb8527def
4 changed files with 48 additions and 12 deletions

View File

@ -57,6 +57,13 @@ static Oid extension_proxy_oid = InvalidOid;
static enum ExtensionState extstate = EXTENSION_STATE_UNKNOWN;
/*
* Looking up the extension oid is a catalog lookup that can be costly, and we
* often need it during the planning, so we cache it here. We update it when
* the extension status is updated.
*/
Oid ts_extension_oid = InvalidOid;
static bool
extension_loader_present()
{
@ -158,7 +165,23 @@ extension_update_state()
return;
in_recursion = true;
extension_set_state(extension_current_state());
enum ExtensionState new_state = extension_current_state();
extension_set_state(new_state);
/*
* Update the extension oid. Note that it is only safe to run
* get_extension_oid() when the extension state is 'CREATED', because
* otherwise we might not be even able to do a catalog lookup because we
* are not in transaction state, and the like.
*/
if (new_state == EXTENSION_STATE_CREATED)
{
ts_extension_oid = get_extension_oid(EXTENSION_NAME, true /* missing_ok */);
Assert(ts_extension_oid != InvalidOid);
}
else
{
ts_extension_oid = InvalidOid;
}
in_recursion = false;
}

View File

@ -19,4 +19,6 @@ extern const char *ts_experimental_schema_name(void);
extern const char *ts_extension_get_so_name(void);
extern TSDLLEXPORT const char *ts_extension_get_version(void);
extern TSDLLEXPORT Oid ts_extension_oid;
#endif /* TIMESCALEDB_EXTENSION_H */

View File

@ -7,6 +7,7 @@
#include <access/tsmapi.h>
#include <access/xact.h>
#include <catalog/namespace.h>
#include <commands/extension.h>
#include <executor/nodeAgg.h>
#include <miscadmin.h>
#include <nodes/makefuncs.h>
@ -348,6 +349,14 @@ timescaledb_planner(Query *parse, int cursor_opts, ParamListInfo bound_params)
context.rootquery = parse;
if (ts_extension_is_loaded())
{
/*
* Some debug checks.
*/
Assert(ts_extension_oid == get_extension_oid(EXTENSION_NAME, /* missing_ok = */ false));
/*
* Preprocess the hypertables in the query and warm up the caches.
*/
preprocess_query((Node *) parse, &context);
/*

View File

@ -18,19 +18,20 @@
#include <extension_constants.h>
#include <planner.h>
#include "remote/connection.h"
#include "option.h"
#include "deparse.h"
#include "relinfo.h"
#include "estimate.h"
#include "chunk_adaptive.h"
#include "cache.h"
#include "chunk.h"
#include "chunk_adaptive.h"
#include "deparse.h"
#include "dimension.h"
#include "errors.h"
#include "estimate.h"
#include "extension.h"
#include "hypercube.h"
#include "hypertable.h"
#include "hypertable_cache.h"
#include "dimension.h"
#include "chunk.h"
#include "hypercube.h"
#include "errors.h"
#include "option.h"
#include "relinfo.h"
#include "remote/connection.h"
#include "scan_exec.h"
/* Default CPU cost to start up a foreign query. */
@ -427,7 +428,8 @@ fdw_relinfo_create(PlannerInfo *root, RelOptInfo *rel, Oid server_oid, Oid local
*/
fpinfo->fdw_startup_cost = DEFAULT_FDW_STARTUP_COST;
fpinfo->fdw_tuple_cost = DEFAULT_FDW_TUPLE_COST;
fpinfo->shippable_extensions = list_make1_oid(get_extension_oid(EXTENSION_NAME, true));
Assert(ts_extension_oid != InvalidOid);
fpinfo->shippable_extensions = list_make1_oid(ts_extension_oid);
fpinfo->fetch_size = DEFAULT_FDW_FETCH_SIZE;
apply_fdw_and_server_options(fpinfo);