Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2991,21 +2991,21 @@ chunk_tuple_delete(TupleInfo *ti, Oid relid, DropBehavior behavior, bool detach)
ts_chunk_rewrite_delete(relid, false);
}

if (form.compressed_chunk_id != INVALID_CHUNK_ID)
if (ts_flags_are_set_32(DatumGetInt32(form.status), CHUNK_STATUS_COMPRESSED))
{
Chunk *compressed_chunk = ts_chunk_get_by_id(form.compressed_chunk_id, false);
Oid compressed_relid = ts_relation_get_compressed_relid(relid);

if (OidIsValid(relid))
{
ts_compression_settings_delete(relid);
}

/* The chunk may have been deleted by a CASCADE */
if (compressed_chunk != NULL)
if (OidIsValid(compressed_relid))
{
/* Plain drop without preserving catalog row because this is the compressed
* chunk */
ts_chunk_drop(compressed_chunk, behavior, DEBUG1);
ts_chunk_drop_by_relid(compressed_relid, behavior, DEBUG1);
}
}
else if (OidIsValid(relid))
Expand Down Expand Up @@ -3825,6 +3825,32 @@ ts_chunk_drop(const Chunk *chunk, DropBehavior behavior, int32 log_level)
ts_stats_chunk_evict(chunk->table_id);
}

void
ts_chunk_drop_by_relid(Oid relid, DropBehavior behavior, int32 log_level)
{
ObjectAddress objaddr = {
.classId = RelationRelationId,
.objectId = relid,
};

const char *schema_name = get_namespace_name(get_rel_namespace(relid));
const char *table_name = get_rel_name(relid);

if (log_level >= 0)
{
elog(log_level, "dropping chunk %s.%s", schema_name, table_name);
}

/* Remove the chunk from the chunk table */
ts_chunk_delete_by_relid_and_relname(relid, schema_name, table_name, behavior);

/* Drop the table */
performDeletion(&objaddr, behavior, 0);

/* Evict the chunk stats from the shared memory */
ts_stats_chunk_evict(relid);
}

static void
lock_referenced_tables(Oid table_relid)
{
Expand Down
1 change: 1 addition & 0 deletions src/chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ extern TSDLLEXPORT bool ts_chunk_is_frozen(const Chunk *chunk);
extern TSDLLEXPORT bool ts_chunk_set_compressed_chunk(Chunk *chunk, int32 compressed_chunk_id);
extern TSDLLEXPORT bool ts_chunk_clear_compressed_chunk(Chunk *chunk);
extern TSDLLEXPORT void ts_chunk_drop(const Chunk *chunk, DropBehavior behavior, int32 log_level);
extern TSDLLEXPORT void ts_chunk_drop_by_relid(Oid relid, DropBehavior behavior, int32 log_level);
extern TSDLLEXPORT List *ts_chunk_do_drop_chunks(Hypertable *ht, int64 older_than, int64 newer_than,
int32 log_level, Oid time_type, Oid arg_type,
bool older_newer);
Expand Down
4 changes: 1 addition & 3 deletions src/chunk_tuple_routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ ts_chunk_tuple_routing_find_chunk(ChunkTupleRouting *ctr, Point *point)
{
Hypertable *compressed_ht =
ts_hypertable_get_by_id(ctr->hypertable->fd.compressed_hypertable_id);
Chunk *compressed_chunk =
ts_cm_functions->compression_chunk_create(compressed_ht, chunk);
ts_chunk_set_compressed_chunk(chunk, compressed_chunk->fd.id);
ts_cm_functions->compression_chunk_create(compressed_ht, chunk);
created_compressed_chunk = true;

/* mark chunk as partial unless completely new chunk */
Expand Down
2 changes: 1 addition & 1 deletion src/cross_module_fn.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ typedef struct CrossModuleFunctions
TupleTableSlot *slot);
void (*compressor_flush)(RowCompressor *compressor, BulkWriter *bulk_writer);
void (*compressor_close)(RowCompressor *compressor, BulkWriter *bulk_writer);
Chunk *(*compression_chunk_create)(Hypertable *ht, Chunk *src_chunk);
void (*compression_chunk_create)(Hypertable *ht, Chunk *src_chunk);

/* The compression functions below are not installed in SQL as part of create extension;
* They are installed and tested during testing scripts. They are exposed in cross-module
Expand Down
9 changes: 3 additions & 6 deletions src/process_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -1728,14 +1728,11 @@ process_drop_chunk(ProcessUtilityArgs *args, DropStmt *stmt)
if (stmt->behavior == DROP_CASCADE && ts_chunk_is_compressed(chunk))
{
Oid compressed_relid = ts_relation_get_compressed_relid(chunk->table_id);
Chunk *compressed_chunk = ts_chunk_get_by_relid_locked(compressed_relid,
AccessExclusiveLock,
&slice_lock,
false);
/* The chunk may have been delete by a CASCADE */
if (compressed_chunk != NULL)
if (OidIsValid(compressed_relid))
{
ts_chunk_drop(compressed_chunk, stmt->behavior, DEBUG1);
LockRelationOid(compressed_relid, AccessExclusiveLock);
ts_chunk_drop_by_relid(compressed_relid, stmt->behavior, DEBUG1);
}
}

Expand Down
34 changes: 17 additions & 17 deletions test/expected/alter.out
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,24 @@ SELECT relname, reloptions FROM pg_class WHERE relname IN ('_hyper_2_3_chunk','_

-- Need superuser to ALTER chunks in _timescaledb_internal schema
\c :TEST_DBNAME :ROLE_SUPERUSER
SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk WHERE id = 2;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+------------------+---------------------+--------+-----------
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk WHERE id = 2;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+------------------+--------+-----------
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | 0 | f

-- Rename chunk
ALTER TABLE _timescaledb_internal._hyper_2_2_chunk RENAME TO new_chunk_name;
SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk WHERE id = 2;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+----------------+---------------------+--------+-----------
2 | 2 | _timescaledb_internal | new_chunk_name | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk WHERE id = 2;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+----------------+--------+-----------
2 | 2 | _timescaledb_internal | new_chunk_name | 0 | f

-- Set schema
ALTER TABLE _timescaledb_internal.new_chunk_name SET SCHEMA public;
SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk WHERE id = 2;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-------------+----------------+---------------------+--------+-----------
2 | 2 | public | new_chunk_name | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk WHERE id = 2;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-------------+----------------+--------+-----------
2 | 2 | public | new_chunk_name | 0 | f

-- Test that we cannot rename chunk columns
\set ON_ERROR_STOP 0
Expand Down Expand Up @@ -634,11 +634,11 @@ SELECT * from _timescaledb_catalog.hypertable;
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
12 | public | my_table | new_associated_schema | _hyper_12 | 1 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk from _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+--------------------+---------------------+--------+-----------
24 | 12 | new_associated_schema | _hyper_12_24_chunk | | 0 | f
25 | 12 | new_associated_schema | _hyper_12_25_chunk | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk from _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+--------------------+--------+-----------
24 | 12 | new_associated_schema | _hyper_12_24_chunk | 0 | f
25 | 12 | new_associated_schema | _hyper_12_25_chunk | 0 | f

DROP TABLE my_table;
-- test renaming unique constraints/indexes
Expand Down
14 changes: 7 additions & 7 deletions test/expected/create_hypertable.out
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,13 @@ select * from _timescaledb_catalog.hypertable where table_name = 'test_migrate';
----+-------------+--------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
10 | test_schema | test_migrate | _timescaledb_internal | _hyper_10 | 1 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0

select id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk from _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+--------------------+---------------------+--------+-----------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | | 0 | f
8 | 7 | _timescaledb_internal | _hyper_7_8_chunk | | 0 | f
9 | 10 | _timescaledb_internal | _hyper_10_9_chunk | | 0 | f
10 | 10 | _timescaledb_internal | _hyper_10_10_chunk | | 0 | f
select id, hypertable_id, schema_name, table_name, status, osm_chunk from _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+--------------------+--------+-----------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 0 | f
8 | 7 | _timescaledb_internal | _hyper_7_8_chunk | 0 | f
9 | 10 | _timescaledb_internal | _hyper_10_9_chunk | 0 | f
10 | 10 | _timescaledb_internal | _hyper_10_10_chunk | 0 | f

select * from test_schema.test_migrate;
time | temp
Expand Down
24 changes: 12 additions & 12 deletions test/expected/drop_owned-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ SELECT * FROM _timescaledb_catalog.hypertable ORDER BY id;
1 | hypertable_schema | default_perm_user | _timescaledb_internal | _hyper_1 | 2 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0
2 | hypertable_schema | superuser | _timescaledb_internal | _hyper_2 | 2 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+------------------+---------------------+--------+-----------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | | 0 | f
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+------------------+--------+-----------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 0 | f
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | 0 | f

DROP OWNED BY :ROLE_DEFAULT_PERM_USER;
SELECT * FROM _timescaledb_catalog.hypertable ORDER BY id;
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compression_state | compressed_hypertable_id | status
----+-------------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
2 | hypertable_schema | superuser | _timescaledb_internal | _hyper_2 | 2 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+------------------+---------------------+--------+-----------
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+------------------+--------+-----------
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | 0 | f

DROP TABLE hypertable_schema.superuser;
--everything should be cleaned up
SELECT * FROM _timescaledb_catalog.hypertable GROUP BY id;
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compression_state | compressed_hypertable_id | status
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+------------------------+-------------------+-------------------+--------------------------+--------

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-------------+------------+---------------------+--------+-----------
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-------------+------------+--------+-----------

SELECT * FROM _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
Expand Down
24 changes: 12 additions & 12 deletions test/expected/drop_owned-16.out
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ SELECT * FROM _timescaledb_catalog.hypertable ORDER BY id;
1 | hypertable_schema | default_perm_user | _timescaledb_internal | _hyper_1 | 2 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0
2 | hypertable_schema | superuser | _timescaledb_internal | _hyper_2 | 2 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+------------------+---------------------+--------+-----------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | | 0 | f
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+------------------+--------+-----------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 0 | f
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | 0 | f

DROP OWNED BY :ROLE_DEFAULT_PERM_USER;
SELECT * FROM _timescaledb_catalog.hypertable ORDER BY id;
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compression_state | compressed_hypertable_id | status
----+-------------------+------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+-------------------+--------------------------+--------
2 | hypertable_schema | superuser | _timescaledb_internal | _hyper_2 | 2 | _timescaledb_functions | calculate_chunk_interval | 0 | 0 | | 0

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-----------------------+------------------+---------------------+--------+-----------
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | | 0 | f
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-----------------------+------------------+--------+-----------
2 | 2 | _timescaledb_internal | _hyper_2_2_chunk | 0 | f

DROP TABLE hypertable_schema.superuser;
--everything should be cleaned up
SELECT * FROM _timescaledb_catalog.hypertable GROUP BY id;
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compression_state | compressed_hypertable_id | status
----+-------------+------------+------------------------+-------------------------+----------------+--------------------------+------------------------+-------------------+-------------------+--------------------------+--------

SELECT id, hypertable_id, schema_name, table_name, compressed_chunk_id, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | status | osm_chunk
----+---------------+-------------+------------+---------------------+--------+-----------
SELECT id, hypertable_id, schema_name, table_name, status, osm_chunk FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | status | osm_chunk
----+---------------+-------------+------------+--------+-----------

SELECT * FROM _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func
Expand Down
Loading
Loading