Skip to content

Commit

Permalink
Add bool created to create_hypertable and add_dimension return value
Browse files Browse the repository at this point in the history
Add bool created to return value of create_hypertable and add_dimension.
When if_not_exists is true and creation is skipped because the object
already exists created will be false, otherwise it will be true. This
modifies the functions to return meta data even when no object was
created.
  • Loading branch information
svenklemm committed Oct 17, 2018
1 parent 45278ff commit 3e3bb0c
Show file tree
Hide file tree
Showing 57 changed files with 832 additions and 820 deletions.
4 changes: 2 additions & 2 deletions sql/ddl_api.sql
Expand Up @@ -29,7 +29,7 @@ CREATE OR REPLACE FUNCTION create_hypertable(
migrate_data BOOLEAN = FALSE,
chunk_target_size TEXT = NULL,
chunk_sizing_func REGPROC = '_timescaledb_internal.calculate_chunk_interval'::regproc
) RETURNS TABLE(hypertable_id INT, schema_name NAME, table_name NAME) AS '@MODULE_PATHNAME@', 'ts_hypertable_create' LANGUAGE C VOLATILE;
) RETURNS TABLE(hypertable_id INT, schema_name NAME, table_name NAME, created BOOL) AS '@MODULE_PATHNAME@', 'ts_hypertable_create' LANGUAGE C VOLATILE;

-- Set adaptive chunking. To disable, set chunk_target_size => 'off'.
CREATE OR REPLACE FUNCTION set_adaptive_chunking(
Expand Down Expand Up @@ -138,7 +138,7 @@ CREATE OR REPLACE FUNCTION add_dimension(
chunk_time_interval ANYELEMENT = NULL::BIGINT,
partitioning_func REGPROC = NULL,
if_not_exists BOOLEAN = FALSE
) RETURNS TABLE(dimension_id INT, schema_name NAME, table_name NAME, column_name NAME)
) RETURNS TABLE(dimension_id INT, schema_name NAME, table_name NAME, column_name NAME, created BOOL)
AS '@MODULE_PATHNAME@', 'ts_dimension_add' LANGUAGE C VOLATILE;

CREATE OR REPLACE FUNCTION attach_tablespace(
Expand Down
9 changes: 4 additions & 5 deletions src/dimension.c
Expand Up @@ -952,6 +952,7 @@ dimension_validate_info(DimensionInfo *info)
errmsg("column \"%s\" is already a dimension",
NameStr(*info->colname))));

info->dimension_id = dim->fd.id;
info->skip = true;

ereport(NOTICE,
Expand Down Expand Up @@ -1027,6 +1028,7 @@ dimension_create_datum(FunctionCallInfo fcinfo, DimensionInfo *info)
values[AttrNumberGetAttrOffset(Anum_add_dimension_schema_name)] = NameGetDatum(&info->ht->fd.schema_name);
values[AttrNumberGetAttrOffset(Anum_add_dimension_table_name)] = NameGetDatum(&info->ht->fd.table_name);
values[AttrNumberGetAttrOffset(Anum_add_dimension_column_name)] = NameGetDatum(info->colname);
values[AttrNumberGetAttrOffset(Anum_add_dimension_created)] = BoolGetDatum(!info->skip);
tuple = heap_form_tuple(tupdesc, values, nulls);

return HeapTupleGetDatum(tuple);
Expand Down Expand Up @@ -1117,15 +1119,12 @@ ts_dimension_add(PG_FUNCTION_ARGS)
*/
info.ht = hypertable_get_by_id(info.ht->fd.id);
indexing_verify_indexes(info.ht);
retval = dimension_create_datum(fcinfo, &info);
}

retval = dimension_create_datum(fcinfo, &info);
cache_release(hcache);

if (retval)
PG_RETURN_DATUM(retval);
else
PG_RETURN_NULL();
PG_RETURN_DATUM(retval);
}

/* Used as a tuple found function */
Expand Down
1 change: 1 addition & 0 deletions src/dimension.h
Expand Up @@ -115,6 +115,7 @@ enum Anum_add_dimension
Anum_add_dimension_schema_name,
Anum_add_dimension_table_name,
Anum_add_dimension_column_name,
Anum_add_dimension_created,
_Anum_add_dimension_max,
};

Expand Down
19 changes: 15 additions & 4 deletions src/hypertable.c
Expand Up @@ -1189,7 +1189,7 @@ ts_hypertable_insert_blocker_trigger_add(PG_FUNCTION_ARGS)
}

static Datum
create_hypertable_datum(FunctionCallInfo fcinfo, Hypertable *ht)
create_hypertable_datum(FunctionCallInfo fcinfo, Hypertable *ht, bool created)
{
TupleDesc tupdesc;
Datum values[Natts_create_hypertable];
Expand All @@ -1206,6 +1206,7 @@ create_hypertable_datum(FunctionCallInfo fcinfo, Hypertable *ht)
values[AttrNumberGetAttrOffset(Anum_create_hypertable_id)] = Int32GetDatum(ht->fd.id);
values[AttrNumberGetAttrOffset(Anum_create_hypertable_schema_name)] = NameGetDatum(&ht->fd.schema_name);
values[AttrNumberGetAttrOffset(Anum_create_hypertable_table_name)] = NameGetDatum(&ht->fd.table_name);
values[AttrNumberGetAttrOffset(Anum_create_hypertable_created)] = BoolGetDatum(created);
tuple = heap_form_tuple(tupdesc, values, nulls);

return HeapTupleGetDatum(tuple);
Expand Down Expand Up @@ -1282,7 +1283,12 @@ ts_hypertable_create(PG_FUNCTION_ARGS)
errmsg("table \"%s\" is already a hypertable, skipping",
get_rel_name(table_relid))));

PG_RETURN_NULL();
hcache = hypertable_cache_pin();
ht = hypertable_cache_get_entry(hcache, table_relid);
retval = create_hypertable_datum(fcinfo, ht, false);
cache_release(hcache);

PG_RETURN_DATUM(retval);
}

/*
Expand Down Expand Up @@ -1313,7 +1319,12 @@ ts_hypertable_create(PG_FUNCTION_ARGS)
errmsg("table \"%s\" is already a hypertable, skipping",
get_rel_name(table_relid))));

PG_RETURN_NULL();
hcache = hypertable_cache_pin();
ht = hypertable_cache_get_entry(hcache, table_relid);
retval = create_hypertable_datum(fcinfo, ht, false);
cache_release(hcache);

PG_RETURN_DATUM(retval);
}

ereport(ERROR,
Expand Down Expand Up @@ -1489,7 +1500,7 @@ ts_hypertable_create(PG_FUNCTION_ARGS)
if (create_default_indexes)
indexing_create_default_indexes(ht);

retval = create_hypertable_datum(fcinfo, ht);
retval = create_hypertable_datum(fcinfo, ht, true);
cache_release(hcache);

PG_RETURN_DATUM(retval);
Expand Down
1 change: 1 addition & 0 deletions src/hypertable.h
Expand Up @@ -31,6 +31,7 @@ enum Anum_create_hypertable
Anum_create_hypertable_id = 1,
Anum_create_hypertable_schema_name,
Anum_create_hypertable_table_name,
Anum_create_hypertable_created,
_Anum_create_hypertable_max,
};

Expand Down
12 changes: 6 additions & 6 deletions test/expected/agg_bookends.out
@@ -1,9 +1,9 @@
CREATE TABLE "btest"(time timestamp, time_alt timestamp, gp INTEGER, temp float, strid TEXT DEFAULT 'testing');
SELECT create_hypertable('"btest"', 'time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
-------------------
(1,public,btest)
create_hypertable
--------------------
(1,public,btest,t)
(1 row)

INSERT INTO "btest" VALUES('2017-01-20T09:00:01', '2017-01-20T10:00:00', 1, 22.5);
Expand Down Expand Up @@ -133,9 +133,9 @@ CREATE TABLE btest_numeric
);
SELECT create_hypertable('btest_numeric', 'time');
NOTICE: adding not-null constraint to column "time"
create_hypertable
--------------------------
(2,public,btest_numeric)
create_hypertable
----------------------------
(2,public,btest_numeric,t)
(1 row)

-- Insert rows, with rows that contain NULL values
Expand Down
66 changes: 33 additions & 33 deletions test/expected/alter.out
Expand Up @@ -8,9 +8,9 @@ ALTER TABLE alter_before ALTER COLUMN temp SET STATISTICS 100;
ALTER TABLE alter_before ALTER COLUMN notes SET STORAGE EXTERNAL;
SELECT create_hypertable('alter_before', 'time', chunk_time_interval => 2628000000000);
NOTICE: adding not-null constraint to column "time"
create_hypertable
-------------------------
(1,public,alter_before)
create_hypertable
---------------------------
(1,public,alter_before,t)
(1 row)

INSERT INTO alter_before VALUES ('2017-03-22T09:18:22', 23.5, 1);
Expand Down Expand Up @@ -46,9 +46,9 @@ ORDER BY c.relname, a.attnum;
CREATE TABLE alter_after(id serial, time timestamp, temp float, colorid integer, notes text, notes_2 text);
SELECT create_hypertable('alter_after', 'time', chunk_time_interval => 2628000000000);
NOTICE: adding not-null constraint to column "time"
create_hypertable
------------------------
(2,public,alter_after)
create_hypertable
--------------------------
(2,public,alter_after,t)
(1 row)

-- Create first chunk
Expand Down Expand Up @@ -222,9 +222,9 @@ WHERE tablename = 'hyper_in_space' OR tablename LIKE '\_hyper\__\__\_chunk' ORDE
CREATE TABLE hyper_in_space(time bigint, temp float, device int);
SELECT create_hypertable('hyper_in_space', 'time', 'device', 4, chunk_time_interval=>1);
NOTICE: adding not-null constraint to column "time"
create_hypertable
---------------------------
(3,public,hyper_in_space)
create_hypertable
-----------------------------
(3,public,hyper_in_space,t)
(1 row)

INSERT INTO hyper_in_space(time, temp, device) VALUES (1, 20, 1);
Expand Down Expand Up @@ -307,9 +307,9 @@ DROP TABLE hyper_in_space;
CREATE TABLE hyper_in_space(time bigint, temp float, device int) TABLESPACE tablespace1;
SELECT create_hypertable('hyper_in_space', 'time', 'device', 4, chunk_time_interval=>1);
NOTICE: adding not-null constraint to column "time"
create_hypertable
---------------------------
(4,public,hyper_in_space)
create_hypertable
-----------------------------
(4,public,hyper_in_space,t)
(1 row)

INSERT INTO hyper_in_space(time, temp, device) VALUES (1, 20, 1);
Expand Down Expand Up @@ -419,9 +419,9 @@ CREATE TABLE original_name.my_table (
quantity double precision
);
SELECT create_hypertable('original_name.my_table','date');
create_hypertable
----------------------------
(5,original_name,my_table)
create_hypertable
------------------------------
(5,original_name,my_table,t)
(1 row)

INSERT INTO original_name.my_table (date, quantity) VALUES ('2018-07-04T21:00:00+00:00', 8);
Expand All @@ -443,21 +443,21 @@ CREATE TABLE regular_table (
quantity double precision
);
SELECT create_hypertable('original_name.my_table','date');
create_hypertable
----------------------------
(6,original_name,my_table)
create_hypertable
------------------------------
(6,original_name,my_table,t)
(1 row)

SELECT create_hypertable('original_name.my_table2','date');
create_hypertable
-----------------------------
(7,original_name,my_table2)
create_hypertable
-------------------------------
(7,original_name,my_table2,t)
(1 row)

SELECT create_hypertable('regular_table','date');
create_hypertable
--------------------------
(8,public,regular_table)
create_hypertable
----------------------------
(8,public,regular_table,t)
(1 row)

INSERT INTO original_name.my_table (date, quantity) VALUES ('2018-07-04T21:00:00+00:00', 8);
Expand All @@ -479,15 +479,15 @@ CREATE TABLE original_name.my_table2 (
quantity double precision
);
SELECT create_hypertable('original_name.my_table','date');
create_hypertable
----------------------------
(9,original_name,my_table)
create_hypertable
------------------------------
(9,original_name,my_table,t)
(1 row)

SELECT create_hypertable('original_name.my_table2','date');
create_hypertable
------------------------------
(10,original_name,my_table2)
create_hypertable
--------------------------------
(10,original_name,my_table2,t)
(1 row)

INSERT INTO original_name.my_table (date, quantity) VALUES ('2018-07-04T21:00:00+00:00', 8);
Expand Down Expand Up @@ -518,9 +518,9 @@ CREATE TABLE my_table (
quantity double precision
);
SELECT create_hypertable('my_table','date', associated_schema_name => 'my_associated_schema');
create_hypertable
----------------------
(11,public,my_table)
create_hypertable
------------------------
(11,public,my_table,t)
(1 row)

INSERT INTO my_table (date, quantity) VALUES ('2018-07-04T21:00:00+00:00', 8);
Expand Down

0 comments on commit 3e3bb0c

Please sign in to comment.