CREATE
CREATE is used to create new databases or tables.
CREATE DATABASE
Syntax
Creates a new database:
CREATE DATABASE [IF NOT EXISTS] db_name [WITH <options>]
If the db_name database already exists, then GreptimeDB has the following behaviors:
- Doesn't create a new database.
- Doesn't return an error when the clause
IF NOT EXISTSis presented. - Otherwise, returns an error.
The database can also carry options similar to the CREATE TABLE statement by using the WITH keyword. The following options are available for databases:
ttl- Time-To-Live for data in all tables within the database (cannot be set toinstant)memtable.type- Type of memtable (bulk,time_series)append_mode- Whether tables in the database should be append-only (true/false)merge_mode- Strategy for merging duplicate rows (last_row,last_non_null)skip_wal- Whether to disable Write-Ahead-Log for tables in the database ('true'/'false')sst_format- SST (Sorted String Table) file format for tables in the database (flat,primary_key)compaction.*- Compaction-related settings (e.g.,compaction.type,compaction.twcs.time_window)
Read more about table options.
Database options behave differently:
-
TTL and Compaction options (
ttl,compaction.*): These options have ongoing effect. Tables without specified values will continuously inherit database-level values. Changing the database TTL or compaction options will immediately impact all tables that don't have their own settings. -
Other options (
memtable.type,append_mode,merge_mode,skip_wal,sst_format): These act as template variables that are only applied when creating new tables. Changing these database-level options will NOT affect existing tables - they only serve as defaults for newly created tables.
When creating a table, if the corresponding table options are not provided, the options configured at the database level will be applied.
Examples
Creates a test database:
CREATE DATABASE test;
Query OK, 1 row affected (0.05 sec)
Creates it again with IF NOT EXISTS:
CREATE DATABASE IF NOT EXISTS test;
Create a database with a TTL (Time-To-Live) of seven days, which means all the tables in this database will inherit this option if they don't have their own TTL setting:
CREATE DATABASE test WITH (ttl='7d');
Create a database with multiple options, including append mode and custom memtable type:
CREATE DATABASE test WITH (
ttl='30d',
'memtable.type'='bulk',
'append_mode'='true'
);
Create a database with Write-Ahead-Log disabled and custom merge mode:
CREATE DATABASE test WITH (
'skip_wal'='true',
'merge_mode'='last_non_null'
);
Create a database with a specific SST file format:
CREATE DATABASE test WITH ('sst_format'='flat');
CREATE TABLE
Syntax
Creates a new table in the db database or the current database in use:
CREATE TABLE [IF NOT EXISTS] [db.]table_name
(
column1 type1 [NULL | NOT NULL] [DEFAULT expr1] [TIME INDEX] [PRIMARY KEY] [indexes] [COMMENT comment1],
column2 type2 [NULL | NOT NULL] [DEFAULT expr2] [TIME INDEX] [PRIMARY KEY] [indexes] [COMMENT comment2],
...
[TIME INDEX (column)],
[PRIMARY KEY(column1, column2, ...)],
)
[
PARTITION ON COLUMNS(column1, column2, ...) (
<PARTITION EXPR>,
...
)
]
ENGINE = engine WITH([TTL | storage | ...] = expr, ...)
The table schema is specified by the brackets before the ENGINE. The table schema is a list of column definitions and table constraints.
For information on the engine option and table engine selection, please refer to the Table Engines guide.
A column definition includes the column column_name, type, and options such as nullable or default values, etc. Please see below.
Table constraints
The table constraints contain the following:
TIME INDEXspecifies the time index column, which always has one and only one column. It indicates theTimestamptype in the data model of GreptimeDB.PRIMARY KEYspecifies the table's primary key column, which indicates theTagtype in the data model of GreptimeDB. It cannot include the time index column, but it always implicitly adds the time index column to the end of keys.- The Other columns are
Fieldcolumns in the data model of GreptimeDB.
The PRIMARY KEY columns and TIME INDEX together form the storage key used to order and merge rows. Unlike a primary key in a relational database, this storage key does not enforce uniqueness. The merge_mode and append_mode options determine how GreptimeDB handles rows with the same storage key.
The statement won't do anything if the table already exists and IF NOT EXISTS is presented; otherwise returns an error.
Indexes
GreptimeDB provides various type of indexes to accelerate query. Please refer to Data Index for more details.
Table options
Users can add table options by using WITH. The valid options contain the following:
| Option | Description | Value |
|---|---|---|
ttl | The storage time of the table data | A time duration string such as '60m', '1h' for one hour, '14d' for 14 days etc. Supported time units are: s / m / h / d. |
storage | The name of the table storage engine provider | String value, such as S3, Gcs, etc. It must be configured in [[storage.providers]], see configuration. |
compaction.type | Compaction strategy of the table | String value. Only twcs is allowed. |
compaction.twcs.trigger_file_num | Number of files in a specific time window to trigger a compaction | String value, such as '8'. Only available when compaction.type is twcs. You can refer to this document to learn more about the twcs compaction strategy. |
compaction.twcs.time_window | Compaction time window | String value, such as '1d' for 1 day. The table usually partitions rows into different time windows by their timestamps. Only available when compaction.type is twcs. |
compaction.twcs.max_output_file_size | Maximum allowed output file size for TWCS compaction | String value, such as '1GB', '512MB'. Sets the maximum size for files produced by TWCS compaction. Only available when compaction.type is twcs. |
memtable.type | Type of the memtable | String value: bulk or time_series. If omitted, Mito selects the implementation from the SST format; the default flat format uses bulk. Setting bulk forces sst_format=flat, and flat SSTs use the bulk implementation even if time_series is specified. The legacy value partition_tree is accepted for compatibility and maps to the bulk and flat path. |
append_mode | Whether the table is append-only | String value. Default is 'false', which removes duplicate rows by primary keys and timestamps according to the merge_mode. Setting it to 'true' to enable append mode and create an append-only table which keeps duplicate rows. |
merge_mode | The strategy to merge duplicate rows | String value. Only available when append_mode is 'false'. Default is last_row, which keeps the last row for the same primary key and timestamp. Setting it to last_non_null to keep the last non-null field for the same primary key and timestamp. |
sst_format | The format of SST files | String value, supports primary_key, flat. Default is flat. flat is recommended for tables which have a large number of unique primary keys. |
comment | Table level comment | String value. |
skip_wal | Whether to disable Write-Ahead-Log for this table | String type. When set to 'true', the data written to the table will not be persisted to the write-ahead log, which can avoid storage wear and improve write throughput. However, when the process restarts, any unflushed data will be lost. Please use this feature only when the data source itself can ensure reliability. |
write_buffer_size | Per-region write buffer stall threshold for this table | String type, such as '512MB' or '1GB'. For a positive value, GreptimeDB schedules a flush when mutable memtable usage reaches half the value, stalls writes at the value, and rejects writes at twice the value. The table option overrides region_engine.mito.default_region_write_buffer_size. An explicit '0' disables the per-region limit even when the engine default is nonzero. Unset the option to remove the table override and fall back to the engine default. |
auto_flush_interval | How long a region of this table may go without a flush before one is triggered | String type, a time duration such as '5m' or '1h'. Must be greater than zero. The table option overrides the engine-wide region_engine.mito.auto_flush_interval. Set it to NULL with ALTER TABLE to drop the override and fall back to the engine setting. |
max_row_group_row_count | Maximum number of rows in a Parquet row group | String type representing an integer from 1 through 10485760 (10 * 1024 * 1024). The default is 102400 (100 * 1024) when this option is not set. |
index.type | Index type | Only for metric engine String value, supports none, skipping. |
Create a table with a custom row group size
CREATE TABLE IF NOT EXISTS temperatures(
ts TIMESTAMP TIME INDEX,
temperature DOUBLE DEFAULT 10
) WITH ('max_row_group_row_count' = '1024');
The default row group size is recommended for most users. For advanced usage, benchmark different values against your workload to improve performance, or use a smaller value when a row group consumes too much memory. Smaller row groups may reduce memory usage and enable finer-grained pruning, but create more row groups and increase metadata overhead. Larger row groups make the opposite tradeoff.
Create a table with TTL
For example, to create a table with the storage data TTL(Time-To-Live) is seven days:
CREATE TABLE IF NOT EXISTS temperatures(
ts TIMESTAMP TIME INDEX,
temperature DOUBLE DEFAULT 10,
) with(ttl='7d');
The ttl value can be one of the following:
- A duration like
1hour 12min 5s. forever,NULL, an empty string''and0s(or any zero length duration, like0d), means the data will never be deleted.instant, note that database's TTL can't be set toinstant.instantmeans the data will be deleted instantly when inserted. Avoid usinginstantTTL source tables for new Flow workloads because they fall back to the deprecated streaming mode; see the flow management documents.- Unset,
ttlcan be unset by usingALTER TABLE <table-name> UNSET 'ttl', which means the table will inherit the database's ttl policy (if any).
If a table has its own TTL policy, it will take precedence over the database TTL policy. Otherwise, the database TTL policy will be applied to the table.
So if table's TTL is set to forever, no matter what the database's TTL is, the data will never be deleted. But if you unset table TTL using:
ALTER TABLE <table-name> UNSET 'ttl';
Then the database's TTL will be applied to the table.
Note that the default TTL setting for table and database is unset, which also means the data will never be deleted.
Create a table with custom storage
Create a table that stores the data in Google Cloud Storage:
CREATE TABLE IF NOT EXISTS temperatures(
ts TIMESTAMP TIME INDEX,
temperature DOUBLE DEFAULT 10,
) with(ttl='7d', storage="Gcs");
Create a table with custom compaction options
Create a table with custom compaction options. The table will attempt to partition data into 1-day time window based on the timestamps of the data and merges files within each time window if they exceed 8 files.
CREATE TABLE IF NOT EXISTS temperatures(
ts TIMESTAMP TIME INDEX,
temperature DOUBLE DEFAULT 10,
)
with(
'compaction.type'='twcs',
'compaction.twcs.time_window'='1d',
'compaction.twcs.trigger_file_num'='8',
'compaction.twcs.max_output_file_size'='1GB'
);
Create an append-only table
Create an append-only table which disables deduplication.
CREATE TABLE IF NOT EXISTS temperatures(
ts TIMESTAMP TIME INDEX,
temperature DOUBLE DEFAULT 10,
) with('append_mode'='true');
Create a table with merge mode
Create a table with last_row merge mode, which is the default merge mode.
create table if not exists metrics(
host string,
ts timestamp,
cpu double,
memory double,
TIME INDEX (ts),
PRIMARY KEY(host)
)
with('merge_mode'='last_row');
Under last_row mode, the table merges rows with the same primary key and timestamp by only keeping the latest row.
INSERT INTO metrics VALUES ('host1', 0, 0, NULL), ('host2', 1, NULL, 1);
INSERT INTO metrics VALUES ('host1', 0, NULL, 10), ('host2', 1, 11, NULL);
SELECT * from metrics ORDER BY host, ts;
+-------+-------------------------+------+--------+
| host | ts | cpu | memory |
+-------+-------------------------+------+--------+
| host1 | 1970-01-01T00:00:00 | | 10.0 |
| host2 | 1970-01-01T00:00:00.001 | 11.0 | |
+-------+-------------------------+------+--------+
Create a table with last_non_null merge mode.
create table if not exists metrics(
host string,
ts timestamp,
cpu double,
memory double,
TIME INDEX (ts),
PRIMARY KEY(host)
)
with('merge_mode'='last_non_null');
Under last_non_null mode, the table merges rows with the same primary key and timestamp by keeping the latest non-null value of each field.
INSERT INTO metrics VALUES ('host1', 0, 0, NULL), ('host2', 1, NULL, 1);
INSERT INTO metrics VALUES ('host1', 0, NULL, 10), ('host2', 1, 11, NULL);
SELECT * from metrics ORDER BY host, ts;
+-------+-------------------------+------+--------+
| host | ts | cpu | memory |
+-------+-------------------------+------+--------+
| host1 | 1970-01-01T00:00:00 | 0.0 | 10.0 |
| host2 | 1970-01-01T00:00:00.001 | 11.0 | 1.0 |
+-------+-------------------------+------+--------+
Create a table with WAL disabled
Create a table with WAL disabled. Please note that when WAL is disabled, unflushed data will be lost on process restart.
CREATE TABLE IF NOT EXISTS temperatures(
ts TIMESTAMP TIME INDEX,
temperature DOUBLE DEFAULT 10
) with('skip_wal'='true');