Skip to content

Commit

Permalink
Add table mappings to compression settings
Browse files Browse the repository at this point in the history
Refactor the compression settings metadata table to include a mapping
from a chunk's relid to its compressed chunk's relid.

Adding this mapping makes compression settings the main metadata table
for compression-related information, while decoupling it from chunk
metadata. This simplifies the code that looks up compression metadata
as it no longer requires first looking up the corresponding compressed
chunk.

The new compression settings is a step towards removing a chunk's
compression table from the chunk metadata. In other words, the
"compressed chunk" will no longer be a chunk, just a relation
associated with the regular chunk via compression settings.
  • Loading branch information
erimatnor committed Jun 4, 2024
1 parent eb548b6 commit a48c167
Show file tree
Hide file tree
Showing 27 changed files with 436 additions and 267 deletions.
4 changes: 3 additions & 1 deletion sql/pre_install/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ CREATE TABLE _timescaledb_catalog.compression_algorithm (
);

CREATE TABLE _timescaledb_catalog.compression_settings (
relid regclass NOT NULL,
relid regclass NOT NULL,
compress_relid regclass NULL,
segmentby text[],
orderby text[],
orderby_desc bool[],
Expand All @@ -443,6 +444,7 @@ CREATE TABLE _timescaledb_catalog.compression_settings (
);

SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.compression_settings', '');
CREATE INDEX compression_settings_compress_relid_idx ON _timescaledb_catalog.compression_settings (compress_relid);

CREATE TABLE _timescaledb_catalog.compression_chunk_size (
chunk_id integer NOT NULL,
Expand Down
55 changes: 55 additions & 0 deletions sql/updates/latest-dev.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- Update compression settings

CREATE TEMP TABLE tempsettings (LIKE _timescaledb_catalog.compression_settings);
INSERT INTO tempsettings SELECT * FROM _timescaledb_catalog.compression_settings;
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.compression_settings;
DROP TABLE _timescaledb_catalog.compression_settings CASCADE;

CREATE TABLE _timescaledb_catalog.compression_settings (
relid regclass NOT NULL,
compress_relid regclass NULL,
segmentby text[],
orderby text[],
orderby_desc bool[],
orderby_nullsfirst bool[],
CONSTRAINT compression_settings_pkey PRIMARY KEY (relid),
CONSTRAINT compression_settings_check_segmentby CHECK (array_ndims(segmentby) = 1),
CONSTRAINT compression_settings_check_orderby_null CHECK ((orderby IS NULL AND orderby_desc IS NULL AND orderby_nullsfirst IS NULL) OR (orderby IS NOT NULL AND orderby_desc IS NOT NULL AND orderby_nullsfirst IS NOT NULL)),
CONSTRAINT compression_settings_check_orderby_cardinality CHECK (array_ndims(orderby) = 1 AND array_ndims(orderby_desc) = 1 AND array_ndims(orderby_nullsfirst) = 1 AND cardinality(orderby) = cardinality(orderby_desc) AND cardinality(orderby) = cardinality(orderby_nullsfirst))
);

-- Insert hypertable settings
INSERT INTO _timescaledb_catalog.compression_settings
SELECT
cs.relid,
NULL,
cs.segmentby,
cs.orderby,
cs.orderby_desc,
cs.orderby_nullsfirst
FROM
tempsettings cs
INNER JOIN
_timescaledb_catalog.hypertable h ON (cs.relid = format('%I.%I', h.schema_name, h.table_name)::regclass);

-- Insert chunk settings
INSERT INTO _timescaledb_catalog.compression_settings
SELECT
format('%I.%I', ch2.schema_name, ch2.table_name)::regclass AS relid,
cs.relid AS compress_relid,
cs.segmentby,
cs.orderby,
cs.orderby_desc,
cs.orderby_nullsfirst
FROM
tempsettings cs
INNER JOIN
_timescaledb_catalog.chunk ch ON (cs.relid = format('%I.%I', ch.schema_name, ch.table_name)::regclass)
INNER JOIN
_timescaledb_catalog.chunk ch2 ON (ch.id = ch2.compressed_chunk_id);

CREATE INDEX compression_settings_compress_relid_idx ON _timescaledb_catalog.compression_settings (compress_relid);

DROP TABLE tempsettings CASCADE; -- not strictly necessary
GRANT SELECT ON _timescaledb_catalog.compression_settings TO PUBLIC;
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.compression_settings', '');
47 changes: 47 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- Update compression settings
CREATE TEMP TABLE tempsettings (LIKE _timescaledb_catalog.compression_settings);
INSERT INTO tempsettings SELECT * FROM _timescaledb_catalog.compression_settings;
ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog.compression_settings;
DROP TABLE _timescaledb_catalog.compression_settings CASCADE;

CREATE TABLE _timescaledb_catalog.compression_settings (
relid regclass NOT NULL,
segmentby text[],
orderby text[],
orderby_desc bool[],
orderby_nullsfirst bool[],
CONSTRAINT compression_settings_pkey PRIMARY KEY (relid),
CONSTRAINT compression_settings_check_segmentby CHECK (array_ndims(segmentby) = 1),
CONSTRAINT compression_settings_check_orderby_null CHECK ((orderby IS NULL AND orderby_desc IS NULL AND orderby_nullsfirst IS NULL) OR (orderby IS NOT NULL AND orderby_desc IS NOT NULL AND orderby_nullsfirst IS NOT NULL)),
CONSTRAINT compression_settings_check_orderby_cardinality CHECK (array_ndims(orderby) = 1 AND array_ndims(orderby_desc) = 1 AND array_ndims(orderby_nullsfirst) = 1 AND cardinality(orderby) = cardinality(orderby_desc) AND cardinality(orderby) = cardinality(orderby_nullsfirst))
);

-- Insert hypertable settings
INSERT INTO _timescaledb_catalog.compression_settings
SELECT
cs.compress_relid,
cs.segmentby,
cs.orderby,
cs.orderby_desc,
cs.orderby_nullsfirst
FROM
tempsettings cs
WHERE
cs.compress_relid IS NOT NULL;

-- Insert chunk settings
INSERT INTO _timescaledb_catalog.compression_settings
SELECT
cs.relid,
cs.segmentby,
cs.orderby,
cs.orderby_desc,
cs.orderby_nullsfirst
FROM
tempsettings cs
WHERE
cs.compress_relid IS NULL;

DROP TABLE tempsettings CASCADE;
GRANT SELECT ON _timescaledb_catalog.compression_settings TO PUBLIC;
SELECT pg_catalog.pg_extension_config_dump('_timescaledb_catalog.compression_settings', '');
3 changes: 1 addition & 2 deletions sql/views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,7 @@ CREATE OR REPLACE VIEW timescaledb_information.chunk_compression_settings AS
un.orderby
FROM _timescaledb_catalog.hypertable ht
INNER JOIN _timescaledb_catalog.chunk ch ON ch.hypertable_id = ht.id
INNER JOIN _timescaledb_catalog.chunk ch2 ON ch2.id = ch.compressed_chunk_id
LEFT JOIN _timescaledb_catalog.compression_settings s ON format('%I.%I',ch2.schema_name,ch2.table_name)::regclass = s.relid
INNER JOIN _timescaledb_catalog.compression_settings s ON (format('%I.%I',ch.schema_name,ch.table_name)::regclass = s.relid AND format('%I.%I',ch.schema_name,ch.table_name)::regclass != s.compress_relid)
LEFT JOIN LATERAL (
SELECT
string_agg(
Expand Down
4 changes: 2 additions & 2 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2865,13 +2865,13 @@ chunk_tuple_delete(TupleInfo *ti, DropBehavior behavior, bool preserve_chunk_cat
{
Chunk *compressed_chunk = ts_chunk_get_by_id(form.compressed_chunk_id, false);

/* The chunk may have been delete by a CASCADE */
/* The chunk may have been deleted by a CASCADE */
if (compressed_chunk != NULL)
{
/* Plain drop without preserving catalog row because this is the compressed
* chunk */
ts_compression_settings_delete(compressed_chunk->table_id);
ts_chunk_drop(compressed_chunk, behavior, DEBUG1);
ts_compression_settings_delete_by_compress_relid(compressed_chunk->table_id);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/dimension.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <utils/timestamp.h>

#include "compat/compat.h"
#include "chunk.h"
#include "cross_module_fn.h"
#include "debug_point.h"
#include "dimension.h"
Expand Down
6 changes: 4 additions & 2 deletions src/process_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,9 @@ process_rename_column(ProcessUtilityArgs *args, Cache *hcache, Oid relid, Rename
* we don't do anything. */
if (ht)
{
ts_compression_settings_rename_column_hypertable(ht, stmt->subname, stmt->newname);
ts_compression_settings_rename_column_recurse(ht->main_table_relid,
stmt->subname,
stmt->newname);
add_hypertable_to_process_args(args, ht);
dim = ts_hyperspace_get_mutable_dimension_by_name(ht->space,
DIMENSION_TYPE_ANY,
Expand Down Expand Up @@ -4276,8 +4278,8 @@ process_drop_table(EventTriggerDropObject *obj)

Assert(obj->type == EVENT_TRIGGER_DROP_TABLE || obj->type == EVENT_TRIGGER_DROP_FOREIGN_TABLE);
ts_hypertable_delete_by_name(table->schema, table->name);
ts_chunk_delete_by_name(table->schema, table->name, DROP_RESTRICT);
ts_compression_settings_delete(table->relid);
ts_chunk_delete_by_name(table->schema, table->name, DROP_RESTRICT);
}

static void
Expand Down
1 change: 1 addition & 0 deletions src/ts_catalog/catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ static const TableIndexDef catalog_table_index_definitions[_MAX_CATALOG_TABLES]
.length = _MAX_COMPRESSION_SETTINGS_INDEX,
.names = (char *[]) {
[COMPRESSION_SETTINGS_PKEY] = "compression_settings_pkey",
[COMPRESSION_SETTINGS_COMPRESS_RELID_IDX] = "compression_settings_compress_relid_idx",
},
},
[COMPRESSION_CHUNK_SIZE] = {
Expand Down
12 changes: 12 additions & 0 deletions src/ts_catalog/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ typedef enum Anum_continuous_aggs_watermark_pkey
typedef enum Anum_compression_settings
{
Anum_compression_settings_relid = 1,
Anum_compression_settings_compress_relid,
Anum_compression_settings_segmentby,
Anum_compression_settings_orderby,
Anum_compression_settings_orderby_desc,
Expand All @@ -1103,6 +1104,7 @@ typedef enum Anum_compression_settings
typedef struct FormData_compression_settings
{
Oid relid;
Oid compress_relid;
ArrayType *segmentby;
ArrayType *orderby;
ArrayType *orderby_desc;
Expand All @@ -1114,6 +1116,7 @@ typedef FormData_compression_settings *Form_compression_settings;
enum
{
COMPRESSION_SETTINGS_PKEY = 0,
COMPRESSION_SETTINGS_COMPRESS_RELID_IDX,
_MAX_COMPRESSION_SETTINGS_INDEX,
};

Expand All @@ -1125,6 +1128,15 @@ typedef enum Anum_compression_settings_pkey

#define Natts_compression_chunk_size_pkey (_Anum_compression_chunk_size_pkey_max - 1)

typedef enum Anum_compression_settings_compress_relid_idx
{
Anum_compression_settings_compress_relid_idx_relid = 1,
_Anum_compression_settings_compress_relid_idx_max,
} Anum_compression_settings_compress_relid_idx;

#define Natts_compression_settings_compress_relid_idx \
(_Anum_compression_settings_compress_relid_idx_max - 1)

#define COMPRESSION_CHUNK_SIZE_TABLE_NAME "compression_chunk_size"
typedef enum Anum_compression_chunk_size
{
Expand Down
Loading

0 comments on commit a48c167

Please sign in to comment.