|
| 1 | +--- |
| 2 | +api_name: create_chunk() |
| 3 | +excerpt: Create a chunk with specified dimensional constraints |
| 4 | +topics: [hypertables] |
| 5 | +keywords: [chunks, hypertables, create] |
| 6 | +api: |
| 7 | + license: community |
| 8 | + type: function |
| 9 | +products: [cloud, mst, self_hosted] |
| 10 | +--- |
| 11 | + |
| 12 | +# create_chunk() |
| 13 | + |
| 14 | +Manually create a $CHUNK with specific time ranges and space partition boundaries in a [$HYPERTABLE][hypertable-docs]. |
| 15 | + |
| 16 | +You can either create a new $CHUNK, or attach an existing table as a $CHUNK. When you add an existing table, $TIMESCALE_DB attaches it to the $HYPERTABLE and uses it as the data table for |
| 17 | +the new $CHUNK. If necessary, $TIMESCALE_DB renames the table and/or moves the table to the specified schema. |
| 18 | + |
| 19 | +Creating a $CHUNK requires `INSERT` privileges on the $HYPERTABLE. If `chunk_table` is provided, the table must |
| 20 | +have the same columns and compatible constraints as the $HYPERTABLE. CHECK constraints must have the same names |
| 21 | +as the parent table. |
| 22 | + |
| 23 | +## Samples |
| 24 | + |
| 25 | +- **Create a new $CHUNK for a $HYPERTABLE with a time range**: |
| 26 | + |
| 27 | + ```sql |
| 28 | + SELECT * FROM _timescaledb_functions.create_chunk( |
| 29 | + 'conditions', |
| 30 | + '{"time": ["2018-01-01 00:00:00", "2018-01-08 00:00:00"]}' |
| 31 | + ); |
| 32 | + ``` |
| 33 | + |
| 34 | +- **Create a $CHUNK with a custom schema and table name**: |
| 35 | + |
| 36 | + ```sql |
| 37 | + SELECT * FROM _timescaledb_functions.create_chunk( |
| 38 | + 'conditions', |
| 39 | + '{"time": ["2018-01-08 00:00:00", "2018-01-15 00:00:00"]}', |
| 40 | + 'custom_schema', |
| 41 | + 'custom_chunk_name' |
| 42 | + ); |
| 43 | + ``` |
| 44 | + |
| 45 | +- **Create a $CHUNK from an existing table**: |
| 46 | + |
| 47 | + ```sql |
| 48 | + -- Create a table with the same structure as your hypertable |
| 49 | + CREATE TABLE my_chunk_table (time timestamptz NOT NULL, device int, temp float); |
| 50 | + |
| 51 | + -- Attach it as a chunk |
| 52 | + SELECT * FROM _timescaledb_functions.create_chunk( |
| 53 | + 'conditions', |
| 54 | + '{"time": ["2018-01-15 00:00:00", "2018-01-22 00:00:00"]}', |
| 55 | + schema_name => 'public', |
| 56 | + table_name => 'my_chunk', |
| 57 | + chunk_table => 'my_chunk_table' |
| 58 | + ); |
| 59 | + ``` |
| 60 | + |
| 61 | +- **Create a $CHUNK with space partitioning (advanced)**: |
| 62 | + |
| 63 | + For $HYPERTABLEs with additional space dimensions, specify all dimension constraints: |
| 64 | + |
| 65 | + ```sql |
| 66 | + SELECT * FROM _timescaledb_functions.create_chunk( |
| 67 | + 'conditions', |
| 68 | + '{"time": ["2018-01-22 00:00:00", "2018-01-29 00:00:00"], "device": [-9223372036854775808, 1073741823]}' |
| 69 | + ); |
| 70 | + ``` |
| 71 | + |
| 72 | +## Arguments |
| 73 | + |
| 74 | +|Name|Type|Default|Required| Description | |
| 75 | +|-|-|-|-|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 76 | +|`hypertable`|REGCLASS||✔| The $HYPERTABLE to create the $CHUNK for | |
| 77 | +|`slices`|JSONB||✔| A JSONB object specifying the dimensional constraints for the $CHUNK. Specify each dimension with a two-element array `[range_start, range_end]`. <br/><br/>Each key is a dimension column name as defined in `hypertable`, and each value is a two-element array `[range_start, range_end]`. <br/><br/>For timestamp dimensions, use numeric values representing microseconds from Unix epoch or ISO 8601 timestamp strings. For example, `"2018-01-01 00:00:00"`. For integer or space dimensions, use numeric values matching the dimension's data type. <br/><br/>Specify all dimensions defined in the $HYPERTABLE. For example, `{"time": [1514419200000000, 1515024000000000], "device": [-9223372036854775808, 1073741823]}` | |
| 78 | +|`schema_name`|NAME|`NULL`|✖| Schema name for the $CHUNK. If not specified, $TIMESCALE_DB uses the default $CHUNK schema | |
| 79 | +|`table_name`|NAME|`NULL`|✖| Table name for the $CHUNK. If not specified, $TIMESCALE_DB generates a default $CHUNK name | |
| 80 | +|`chunk_table`|REGCLASS|`NULL`|✖| Attach an existing table as the $CHUNK. $TIMESCALE_DB renames and/or moves the table as necessary to match `schema_name` and `table_name` | |
| 81 | + |
| 82 | +## Returns |
| 83 | + |
| 84 | +|Column|Type| Description | |
| 85 | +|-|-|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 86 | +|`chunk_id`|INTEGER| The internal ID of the $CHUNK | |
| 87 | +|`hypertable_id`|INTEGER| The internal ID of the $HYPERTABLE | |
| 88 | +|`schema_name`|NAME| The schema name of the new $CHUNK | |
| 89 | +|`table_name`|NAME| The table name of the new $CHUNK | |
| 90 | +|`relkind`|CHAR| The relation kind, usually `r` for a regular table | |
| 91 | +|`slices`|JSONB| The dimensional constraints that define the $CHUNK | |
| 92 | +|`created`|BOOLEAN| `true` if a new $CHUNK was created. If a $CHUNK with the same dimensional constraints already exists, the function returns information about the existing $CHUNK with `created` set to `false` | |
| 93 | + |
| 94 | +[hypertable-docs]: /use-timescale/:currentVersion:/hypertables/ |
0 commit comments