-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If a lot of chunks are involved then the current pl/pgsql function to compute the size of each chunk via a nested loop is pretty slow. Additionally, the current functionality makes a system call to get the file size on disk for each chunk everytime this function is called. That again slows things down. We now have an approximate function which is implemented in C to avoid the issues in the pl/pgsql function. Additionally, this function also uses per backend caching using the smgr layer to compute the approximate size cheaply. The PG cache invalidation clears off the cached size for a chunk when DML happens into it. That size cache is thus able to get the latest size in a matter of minutes. Also, due to the backend caching, any long running session will only fetch latest data for new or modified chunks and can use the cached data (which is calculated afresh the first time around) effectively for older chunks.
- Loading branch information
Showing
12 changed files
with
589 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Implements: #6463 Support approximate hypertable size |
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 |
---|---|---|
|
@@ -9,6 +9,10 @@ CREATE OR REPLACE FUNCTION _timescaledb_functions.relation_size(relation REGCLAS | |
RETURNS TABLE (total_size BIGINT, heap_size BIGINT, index_size BIGINT, toast_size BIGINT) | ||
AS '@MODULE_PATHNAME@', 'ts_relation_size' LANGUAGE C VOLATILE; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_functions.relation_approximate_size(relation REGCLASS) | ||
RETURNS TABLE (total_size BIGINT, heap_size BIGINT, index_size BIGINT, toast_size BIGINT) | ||
AS '@MODULE_PATHNAME@', 'ts_relation_approximate_size' LANGUAGE C STRICT VOLATILE; | ||
|
||
CREATE OR REPLACE VIEW _timescaledb_internal.hypertable_chunk_local_size AS | ||
SELECT | ||
h.schema_name AS hypertable_schema, | ||
|
@@ -169,6 +173,29 @@ $BODY$ | |
FROM @[email protected]_detailed_size(hypertable); | ||
$BODY$ SET search_path TO pg_catalog, pg_temp; | ||
|
||
-- Get approximate relation size of hypertable | ||
-- | ||
-- hypertable - hypertable to get approximate size of | ||
-- | ||
-- Returns: | ||
-- table_bytes - Approximate disk space used by hypertable | ||
-- index_bytes - Approximate disk space used by indexes | ||
-- toast_bytes - Approximate disk space of toast tables | ||
-- total_bytes - Total approximate disk space used by the specified table, including all indexes and TOAST data | ||
CREATE OR REPLACE FUNCTION @[email protected]_approximate_detailed_size(relation REGCLASS) | ||
RETURNS TABLE (table_bytes BIGINT, index_bytes BIGINT, toast_bytes BIGINT, total_bytes BIGINT) | ||
AS '@MODULE_PATHNAME@', 'ts_hypertable_approximate_size' LANGUAGE C VOLATILE; | ||
|
||
--- returns approximate total-bytes for a hypertable (includes table + index) | ||
CREATE OR REPLACE FUNCTION @[email protected]_approximate_size( | ||
hypertable REGCLASS) | ||
RETURNS BIGINT | ||
LANGUAGE SQL VOLATILE STRICT AS | ||
$BODY$ | ||
SELECT sum(total_bytes)::bigint | ||
FROM @[email protected]_approximate_detailed_size(hypertable); | ||
$BODY$ SET search_path TO pg_catalog, pg_temp; | ||
|
||
CREATE OR REPLACE FUNCTION _timescaledb_functions.chunks_local_size( | ||
schema_name_in name, | ||
table_name_in name) | ||
|
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 |
---|---|---|
|
@@ -427,3 +427,21 @@ ALTER EXTENSION timescaledb ADD TABLE _timescaledb_internal.job_errors; | |
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.hypertable; | ||
ALTER EXTENSION timescaledb ADD TABLE _timescaledb_catalog.hypertable; | ||
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.hypertable', 'WHERE id >= 1'); | ||
|
||
CREATE FUNCTION _timescaledb_functions.relation_approximate_size(relation REGCLASS) | ||
RETURNS TABLE (total_size BIGINT, heap_size BIGINT, index_size BIGINT, toast_size BIGINT) | ||
AS '@MODULE_PATHNAME@', 'ts_relation_approximate_size' LANGUAGE C STRICT VOLATILE; | ||
|
||
CREATE FUNCTION @[email protected]_approximate_detailed_size(relation REGCLASS) | ||
RETURNS TABLE (table_bytes BIGINT, index_bytes BIGINT, toast_bytes BIGINT, total_bytes BIGINT) | ||
AS '@MODULE_PATHNAME@', 'ts_hypertable_approximate_size' LANGUAGE C VOLATILE; | ||
|
||
--- returns approximate total-bytes for a hypertable (includes table + index) | ||
CREATE FUNCTION @[email protected]_approximate_size( | ||
hypertable REGCLASS) | ||
RETURNS BIGINT | ||
LANGUAGE SQL VOLATILE STRICT AS | ||
$BODY$ | ||
SELECT sum(total_bytes)::bigint | ||
FROM @[email protected]_approximate_detailed_size(hypertable); | ||
$BODY$ SET search_path TO pg_catalog, pg_temp; |
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 |
---|---|---|
|
@@ -788,3 +788,6 @@ ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.hypertable; | |
ALTER EXTENSION timescaledb ADD TABLE _timescaledb_catalog.hypertable; | ||
-- include this now in the config | ||
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.hypertable', ''); | ||
DROP FUNCTION IF EXISTS _timescaledb_functions.relation_approximate_size(relation REGCLASS); | ||
DROP FUNCTION IF EXISTS @[email protected]_approximate_detailed_size(relation REGCLASS); | ||
DROP FUNCTION IF EXISTS @[email protected]_approximate_size(hypertable REGCLASS); |
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
Oops, something went wrong.