Some minor code cleanup

This commit is contained in:
Matvey Arye 2017-06-21 12:06:39 -04:00 committed by Erik Nordström
parent 71c5e7801f
commit 9489d06595
9 changed files with 71 additions and 46 deletions

View File

@ -203,6 +203,7 @@ CREATE OR REPLACE FUNCTION _timescaledb_internal.chunk_create_after_lock(
$BODY$
DECLARE
dimension_row _timescaledb_catalog.dimension;
hypertable_id INTEGER;
free_index INTEGER;
fixed_dimension_ids INTEGER[];
fixed_values BIGINT[];
@ -211,10 +212,10 @@ DECLARE
slice_ids INTEGER[];
slice_id INTEGER;
BEGIN
SELECT *
INTO STRICT dimension_row
FROM _timescaledb_catalog.dimension
WHERE id = dimension_ids[1] ;
SELECT d.hypertable_id
INTO STRICT hypertable_id
FROM _timescaledb_catalog.dimension d
WHERE d.id = dimension_ids[1];
slice_ids = NULL;
FOR free_index IN 1 .. array_upper(dimension_ids, 1)
@ -225,12 +226,16 @@ BEGIN
fixed_values = dimension_values[:free_index-1]
|| dimension_values[free_index+1:];
--TODO currently assumes only one dimension is aligned.
SELECT *
INTO STRICT dimension_row
FROM _timescaledb_catalog.dimension
WHERE id = dimension_ids[free_index];
SELECT *
INTO free_range_start, free_range_end
FROM _timescaledb_internal.chunk_calculate_new_ranges(
dimension_ids[free_index], dimension_values[free_index],
fixed_dimension_ids, fixed_values, free_index = 1);
fixed_dimension_ids, fixed_values, dimension_row.aligned);
--do not use RETURNING here (ON CONFLICT DO NOTHING)
INSERT INTO _timescaledb_catalog.dimension_slice
@ -253,7 +258,7 @@ BEGIN
FROM
nextval(pg_get_serial_sequence('_timescaledb_catalog.chunk','id')) seq_id,
_timescaledb_catalog.hypertable h
WHERE h.id = dimension_row.hypertable_id
WHERE h.id = hypertable_id
RETURNING *
)
INSERT INTO _timescaledb_catalog.chunk_constraint (dimension_slice_id, chunk_id)

View File

@ -69,22 +69,22 @@ BEGIN
END IF;
--create time dimension
INSERT INTO _timescaledb_catalog.dimension(hypertable_id, column_name, column_type,
INSERT INTO _timescaledb_catalog.dimension(hypertable_id, column_name, column_type, aligned,
num_slices, partitioning_func_schema, partitioning_func,
interval_length
) VALUES (
hypertable_row.id, time_column_name, time_column_type,
hypertable_row.id, time_column_name, time_column_type, TRUE,
NULL, NULL, NULL,
chunk_time_interval
);
IF partitioning_column IS NOT NULL THEN
--create space dimension
INSERT INTO _timescaledb_catalog.dimension(hypertable_id, column_name, column_type,
INSERT INTO _timescaledb_catalog.dimension(hypertable_id, column_name, column_type, aligned,
num_slices, partitioning_func_schema, partitioning_func,
interval_length
) VALUES (
hypertable_row.id, partitioning_column, partitioning_column_type,
hypertable_row.id, partitioning_column, partitioning_column_type, FALSE,
number_partitions::smallint, partitioning_func_schema, partitioning_func,
NULL
);

View File

@ -68,6 +68,7 @@ CREATE TABLE _timescaledb_catalog.dimension (
hypertable_id INTEGER NOT NULL REFERENCES _timescaledb_catalog.hypertable(id) ON DELETE CASCADE,
column_name NAME NOT NULL,
column_type REGTYPE NOT NULL,
aligned BOOLEAN NOT NULL,
-- closed dimensions
num_slices SMALLINT NULL,
partitioning_func_schema NAME NULL,

View File

@ -109,6 +109,7 @@ enum Anum_dimension
Anum_dimension_hypertable_id,
Anum_dimension_column_name,
Anum_dimension_column_type,
Anum_dimension_aligned,
Anum_dimension_num_slices,
Anum_dimension_partitioning_func_schema,
Anum_dimension_partitioning_func,
@ -125,6 +126,7 @@ typedef struct FormData_dimension
int32 hypertable_id;
NameData column_name;
Oid column_type;
bool aligned;
/* closed (space) columns */
int16 num_slices;
NameData partitioning_func_schema;

View File

@ -15,17 +15,28 @@
#include "metadata_queries.h"
#include "scanner.h"
static void
chunk_fill(Chunk *chunk, HeapTuple tuple)
{
memcpy(&chunk->fd, GETSTRUCT(tuple), sizeof(FormData_chunk));
chunk->table_id = get_relname_relid(chunk->fd.table_name.data,
get_namespace_oid(chunk->fd.schema_name.data, false));
}
Chunk *
chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints)
{
Chunk *chunk;
chunk = palloc0(CHUNK_SIZE(num_constraints));
memcpy(&chunk->fd, GETSTRUCT(tuple), sizeof(FormData_chunk));
chunk->capacity = num_constraints;
chunk->num_constraints = 0;
chunk->table_id = get_relname_relid(chunk->fd.table_name.data,
get_namespace_oid(chunk->fd.schema_name.data, false));
chunk_fill(chunk, tuple);
chunk_constraint_scan_by_chunk_id(chunk);
chunk->cube = hypercube_from_constraints(chunk->constraints, chunk->num_constraints);
return chunk;
}
@ -38,9 +49,18 @@ chunk_create_new(Hyperspace *hs, Point *p)
chunk = spi_chunk_create(hs, p);
Assert(chunk != NULL);
chunk_constraint_scan_by_chunk_id(chunk);
chunk->cube = hypercube_from_constraints(chunk->constraints, chunk->num_constraints);
return chunk;
}
Chunk *
chunk_create_stub(int32 id, int16 num_constraints) {
Chunk *chunk;
chunk = palloc0(CHUNK_SIZE(num_constraints));
chunk->capacity = num_constraints;
chunk->num_constraints = 0;
chunk->fd.id = id;
return chunk;
}
@ -48,16 +68,14 @@ static bool
chunk_tuple_found(TupleInfo *ti, void *arg)
{
Chunk *chunk = arg;
memcpy(&chunk->fd, GETSTRUCT(ti->tuple), sizeof(FormData_chunk));
chunk->table_id = get_relname_relid(chunk->fd.table_name.data,
get_namespace_oid(chunk->fd.schema_name.data, false));
chunk_fill(chunk, ti->tuple);
return false;
}
/* Fill in a chunk stub. The stub data structure needs the chunk ID set. The
* rest of the fields will be filled in from the table data. */
/* Fill in a chunk stub. The stub data structure needs the chunk ID and constraints set.
* The rest of the fields will be filled in from the table data. */
static Chunk *
chunk_scan(Chunk *chunk_stub, bool tuplock)
chunk_fill_stub(Chunk *chunk_stub, bool tuplock)
{
ScanKeyData scankey[1];
Catalog *catalog = catalog_get();
@ -89,6 +107,8 @@ chunk_scan(Chunk *chunk_stub, bool tuplock)
if (num_found != 1)
elog(ERROR, "No chunk found with ID %d", chunk_stub->fd.id);
chunk_stub->cube = hypercube_from_constraints(chunk_stub->constraints, chunk_stub->num_constraints);
return chunk_stub;
}
@ -207,10 +227,8 @@ chunk_find(Hyperspace *hs, Point *p)
if (NULL != chunk)
{
chunk->cube = hypercube_from_constraints(chunk->constraints, chunk->num_constraints);
/* Fill in the rest of the chunk's data from the chunk table */
chunk_scan(chunk, false);
chunk_fill_stub(chunk, false);
}
return chunk;

View File

@ -63,6 +63,7 @@ typedef struct ChunkScanEntry
extern Chunk *chunk_create_from_tuple(HeapTuple tuple, int16 num_constraints);
extern Chunk *chunk_create_new(Hyperspace *hs, Point *p);
extern Chunk *chunk_create_stub(int32 id, int16 num_constraints);
extern bool chunk_add_constraint(Chunk *chunk, ChunkConstraint *constraint);
extern bool chunk_add_constraint_from_tuple(Chunk *chunk, HeapTuple constraint_tuple);
extern Chunk *chunk_find(Hyperspace *hs, Point *p);

View File

@ -101,9 +101,7 @@ chunk_constraint_dimension_id_tuple_found(TupleInfo *ti, void *data)
if (!found)
{
chunk = palloc0(CHUNK_SIZE(ctx->num_dimensions));
chunk->fd.id = constraint.fd.chunk_id;
chunk->capacity = ctx->num_dimensions;
chunk = chunk_create_stub(constraint.fd.chunk_id, ctx->num_dimensions);
entry->chunk = chunk;
} else {
chunk = entry->chunk;

View File

@ -71,16 +71,16 @@ SELECT * FROM _timescaledb_catalog.chunk c
LEFT JOIN _timescaledb_catalog.hypertable h ON (d.hypertable_id = h.id)
WHERE h.schema_name = 'public' AND h.table_name = 'chunk_test'
ORDER BY c.id, d.id;
id | hypertable_id | schema_name | table_name | chunk_id | dimension_slice_id | id | dimension_id | range_start | range_end | id | hypertable_id | column_name | column_type | num_slices | partitioning_func_schema | partitioning_func | interval_length | id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions
----+---------------+-----------------------+------------------+----------+--------------------+----+--------------+-------------+------------+----+---------------+-------------+-------------+------------+--------------------------+-----------------------+-----------------+----+-------------+------------+------------------------+-------------------------+----------------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 1 | 1 | 1 | 1 | 0 | 10 | 1 | 1 | time | bigint | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 1 | 2 | 2 | 2 | 1073741823 | 2147483647 | 2 | 1 | device_id | text | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
2 | 1 | _timescaledb_internal | _hyper_1_2_chunk | 2 | 1 | 1 | 1 | 0 | 10 | 1 | 1 | time | bigint | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
2 | 1 | _timescaledb_internal | _hyper_1_2_chunk | 2 | 4 | 4 | 2 | 0 | 1073741823 | 2 | 1 | device_id | text | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
3 | 1 | _timescaledb_internal | _hyper_1_3_chunk | 3 | 5 | 5 | 1 | 40 | 50 | 1 | 1 | time | bigint | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
3 | 1 | _timescaledb_internal | _hyper_1_3_chunk | 3 | 4 | 4 | 2 | 0 | 1073741823 | 2 | 1 | device_id | text | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
4 | 1 | _timescaledb_internal | _hyper_1_4_chunk | 4 | 7 | 7 | 1 | 10 | 40 | 1 | 1 | time | bigint | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
4 | 1 | _timescaledb_internal | _hyper_1_4_chunk | 4 | 4 | 4 | 2 | 0 | 1073741823 | 2 | 1 | device_id | text | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
id | hypertable_id | schema_name | table_name | chunk_id | dimension_slice_id | id | dimension_id | range_start | range_end | id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions
----+---------------+-----------------------+------------------+----------+--------------------+----+--------------+-------------+------------+----+---------------+-------------+-------------+---------+------------+--------------------------+-----------------------+-----------------+----+-------------+------------+------------------------+-------------------------+----------------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 1 | 1 | 1 | 1 | 0 | 10 | 1 | 1 | time | bigint | t | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | 1 | 2 | 2 | 2 | 1073741823 | 2147483647 | 2 | 1 | device_id | text | f | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
2 | 1 | _timescaledb_internal | _hyper_1_2_chunk | 2 | 1 | 1 | 1 | 0 | 10 | 1 | 1 | time | bigint | t | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
2 | 1 | _timescaledb_internal | _hyper_1_2_chunk | 2 | 4 | 4 | 2 | 0 | 1073741823 | 2 | 1 | device_id | text | f | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
3 | 1 | _timescaledb_internal | _hyper_1_3_chunk | 3 | 5 | 5 | 1 | 40 | 50 | 1 | 1 | time | bigint | t | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
3 | 1 | _timescaledb_internal | _hyper_1_3_chunk | 3 | 4 | 4 | 2 | 0 | 1073741823 | 2 | 1 | device_id | text | f | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
4 | 1 | _timescaledb_internal | _hyper_1_4_chunk | 4 | 7 | 7 | 1 | 10 | 40 | 1 | 1 | time | bigint | t | | | | 40 | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
4 | 1 | _timescaledb_internal | _hyper_1_4_chunk | 4 | 4 | 4 | 2 | 0 | 1073741823 | 2 | 1 | device_id | text | f | 2 | _timescaledb_internal | get_partition_for_key | | 1 | public | chunk_test | _timescaledb_internal | _hyper_1 | 2
(8 rows)
-- Test chunk aligning between partitions

View File

@ -11,8 +11,8 @@ SELECT * from _timescaledb_catalog.hypertable;
(0 rows)
SELECT * from _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | num_slices | partitioning_func_schema | partitioning_func | interval_length
----+---------------+-------------+-------------+------------+--------------------------+-------------------+-----------------
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length
----+---------------+-------------+-------------+---------+------------+--------------------------+-------------------+-----------------
(0 rows)
CREATE TABLE should_drop (time timestamp, temp float8);
@ -38,9 +38,9 @@ SELECT * from _timescaledb_catalog.hypertable;
(1 row)
SELECT * from _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | num_slices | partitioning_func_schema | partitioning_func | interval_length
----+---------------+-------------+-----------------------------+------------+--------------------------+-------------------+-----------------
1 | 1 | time | timestamp without time zone | | | | 2592000000000
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length
----+---------------+-------------+-----------------------------+---------+------------+--------------------------+-------------------+-----------------
1 | 1 | time | timestamp without time zone | t | | | | 2592000000000
(1 row)
DROP TABLE should_drop;
@ -59,8 +59,8 @@ SELECT * from _timescaledb_catalog.hypertable;
(1 row)
SELECT * from _timescaledb_catalog.dimension;
id | hypertable_id | column_name | column_type | num_slices | partitioning_func_schema | partitioning_func | interval_length
----+---------------+-------------+-----------------------------+------------+--------------------------+-------------------+-----------------
2 | 2 | time | timestamp without time zone | | | | 2592000000000
id | hypertable_id | column_name | column_type | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length
----+---------------+-------------+-----------------------------+---------+------------+--------------------------+-------------------+-----------------
2 | 2 | time | timestamp without time zone | t | | | | 2592000000000
(1 row)