Skip to content

Commit

Permalink
Make extension state available through function
Browse files Browse the repository at this point in the history
The extension state is not easily accessible in release builds, which
makes debugging issue with the loader very difficult. This commit
introduces a new schema `_timescaledb_debug` and makes the function
`ts_extension_get_state` available also in release builds as
`_timescaledb_debug.extension_state`.

See #1682
  • Loading branch information
mkindahl committed Jan 10, 2024
1 parent 9c75b03 commit 8bce74c
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 20 deletions.
1 change: 1 addition & 0 deletions .unreleased/fix_6509
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #6509 Make extension state available through function
4 changes: 4 additions & 0 deletions cmake/ScriptFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ set(SOURCE_FILES
osm_api.sql)

if(ENABLE_DEBUG_UTILS AND CMAKE_BUILD_TYPE MATCHES Debug)
list(APPEND SOURCE_FILES debug_build_utils.sql)
endif()

if(ENABLE_DEBUG_UTILS)
list(APPEND SOURCE_FILES debug_utils.sql)
endif()

Expand Down
15 changes: 15 additions & 0 deletions sql/debug_build_utils.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

-- This file contains utility functions that are only used in debug
-- builds for debugging and testing.

CREATE OR REPLACE FUNCTION debug_waitpoint_enable(TEXT) RETURNS VOID LANGUAGE C VOLATILE STRICT
AS '@MODULE_PATHNAME@', 'ts_debug_point_enable';

CREATE OR REPLACE FUNCTION debug_waitpoint_release(TEXT) RETURNS VOID LANGUAGE C VOLATILE STRICT
AS '@MODULE_PATHNAME@', 'ts_debug_point_release';

CREATE OR REPLACE FUNCTION debug_waitpoint_id(TEXT) RETURNS BIGINT LANGUAGE C VOLATILE STRICT
AS '@MODULE_PATHNAME@', 'ts_debug_point_id';
15 changes: 5 additions & 10 deletions sql/debug_utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

-- This file contains utility functions that are only used in debug
-- builds for debugging and testing.
-- This file contains utility functions and views that are used for
-- debugging in release builds. These are all placed in the schema
-- timescaledb_debug.

CREATE OR REPLACE FUNCTION debug_waitpoint_enable(TEXT) RETURNS VOID LANGUAGE C VOLATILE STRICT
AS '@MODULE_PATHNAME@', 'ts_debug_point_enable';

CREATE OR REPLACE FUNCTION debug_waitpoint_release(TEXT) RETURNS VOID LANGUAGE C VOLATILE STRICT
AS '@MODULE_PATHNAME@', 'ts_debug_point_release';

CREATE OR REPLACE FUNCTION debug_waitpoint_id(TEXT) RETURNS BIGINT LANGUAGE C VOLATILE STRICT
AS '@MODULE_PATHNAME@', 'ts_debug_point_id';
CREATE OR REPLACE FUNCTION _timescaledb_debug.extension_state() RETURNS TEXT
AS '@MODULE_PATHNAME@', 'ts_extension_get_state' LANGUAGE C;
11 changes: 10 additions & 1 deletion sql/pre_install/schemas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ CREATE SCHEMA _timescaledb_cache;
CREATE SCHEMA _timescaledb_config;
CREATE SCHEMA timescaledb_experimental;
CREATE SCHEMA timescaledb_information;
CREATE SCHEMA _timescaledb_debug;

GRANT USAGE ON SCHEMA _timescaledb_cache, _timescaledb_catalog, _timescaledb_functions, _timescaledb_internal, _timescaledb_config, timescaledb_information, timescaledb_experimental TO PUBLIC;
GRANT USAGE ON SCHEMA
_timescaledb_cache,
_timescaledb_catalog,
_timescaledb_functions,
_timescaledb_internal,
_timescaledb_config,
timescaledb_information,
timescaledb_experimental
TO PUBLIC;

2 changes: 2 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,5 @@ ALTER TABLE _timescaledb_catalog.dimension
ADD CONSTRAINT dimension_hypertable_id_fkey FOREIGN KEY (hypertable_id) REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE;
ALTER TABLE _timescaledb_catalog.tablespace
ADD CONSTRAINT tablespace_hypertable_id_fkey FOREIGN KEY (hypertable_id) REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE;

CREATE SCHEMA _timescaledb_debug;
2 changes: 2 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,5 @@ ALTER TABLE _timescaledb_catalog.hypertable_data_node
ALTER TABLE _timescaledb_catalog.tablespace
ADD CONSTRAINT tablespace_hypertable_id_fkey FOREIGN KEY (hypertable_id) REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE;

DROP FUNCTION IF EXISTS _timescaledb_debug.extension_state;
DROP SCHEMA IF EXISTS _timescaledb_debug;
16 changes: 7 additions & 9 deletions src/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ static enum ExtensionState extstate = EXTENSION_STATE_UNKNOWN;
*/
static Oid ts_extension_oid = InvalidOid;

static const char *extstate_str[] = {
[EXTENSION_STATE_UNKNOWN] = "unknown",
[EXTENSION_STATE_TRANSITIONING] = "transitioning",
[EXTENSION_STATE_CREATED] = "created",
[EXTENSION_STATE_NOT_INSTALLED] = "not installed",
};

static bool
extension_loader_present()
{
Expand Down Expand Up @@ -334,19 +341,10 @@ ts_extension_is_proxy_table_relid(Oid relid)
return relid == extension_proxy_oid;
}

#ifdef TS_DEBUG
static const char *extstate_str[] = {
[EXTENSION_STATE_UNKNOWN] = "unknown",
[EXTENSION_STATE_TRANSITIONING] = "transitioning",
[EXTENSION_STATE_CREATED] = "created",
[EXTENSION_STATE_NOT_INSTALLED] = "not installed",
};

TS_FUNCTION_INFO_V1(ts_extension_get_state);

Datum
ts_extension_get_state(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text(extstate_str[extstate]));
}
#endif
15 changes: 15 additions & 0 deletions test/expected/debug_utils.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
\c :TEST_DBNAME :ROLE_SUPERUSER
SELECT _timescaledb_debug.extension_state();
extension_state
-----------------
created
(1 row)

SET ROLE :ROLE_DEFAULT_PERM_USER;
\set ON_ERROR_STOP 0
SELECT _timescaledb_debug.extension_state();
ERROR: permission denied for schema _timescaledb_debug at character 8
\set ON_ERROR_STOP 1
1 change: 1 addition & 0 deletions test/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(TEST_FILES
copy.sql
copy_where.sql
ddl_errors.sql
debug_utils.sql
drop_extension.sql
drop_hypertable.sql
drop_owned.sql
Expand Down
12 changes: 12 additions & 0 deletions test/sql/debug_utils.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

\c :TEST_DBNAME :ROLE_SUPERUSER
SELECT _timescaledb_debug.extension_state();

SET ROLE :ROLE_DEFAULT_PERM_USER;

\set ON_ERROR_STOP 0
SELECT _timescaledb_debug.extension_state();
\set ON_ERROR_STOP 1
1 change: 1 addition & 0 deletions tsl/test/shared/expected/extension.out
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ FROM pg_proc p
e.oid = d.refobjid
WHERE proname <> 'get_telemetry_report'
ORDER BY pronamespace::regnamespace::text COLLATE "C", p.oid::regprocedure::text COLLATE "C";
_timescaledb_debug.extension_state()
_timescaledb_functions.alter_job_set_hypertable_id(integer,regclass)
_timescaledb_functions.attach_osm_table_chunk(regclass,regclass)
_timescaledb_functions.bookend_deserializefunc(bytea,internal)
Expand Down

0 comments on commit 8bce74c

Please sign in to comment.