Virtual Tables
Virtual Table Overview
Virtual tables are logical tables that do not store data directly. When you query a virtual table, TDengine reads column data from one or more physical tables or existing virtual tables according to the virtual-table definition, aligns the data by timestamp, and generates the result set on demand.
Virtual tables include the following categories:
- Virtual basic tables: standalone virtual tables whose columns are defined directly with their source mappings.
- Virtual supertables: templates that define the shared columns and tags of virtual subtables and do not store data themselves.
- Virtual subtables: tables created from virtual supertables; their column data comes from other tables, and their tags can be either literals or references to tags in other tables. In addition, a virtual subtable can have owned columns and owned tags that are independent of the virtual supertable — see Create Virtual Subtable and Modify Virtual Subtables.
Virtual Supertable Inheritance
A virtual supertable (VST) can inherit columns and tags from one or more parent VSTs using the BASE ON clause. This enables building hierarchical virtual table topologies — for example, a device-type VST that inherits common fields from a base device VST, then adds its own specialized columns.
Create an Inherited Virtual Supertable
CREATE STABLE [IF NOT EXISTS] [db_name.]stb_name
(col_name col_type [, ...])
[TAGS (tag_name tag_type [, ...])]
BASE ON [db_name.]parent_stb_name [, [db_name.]parent_stb_name] ...
VIRTUAL 1
The BASE ON clause specifies one or more parent VSTs to inherit from. The child VST inherits all tags and all non-primary-timestamp columns from each parent. The child's own columns and tags are appended after the inherited ones.
Example:
-- Parent VST with common device fields
CREATE STABLE p_device (ts timestamp, status int) TAGS (region int) VIRTUAL 1;
-- Child VST inherits status + region, adds temperature
CREATE STABLE p_temp (ts timestamp, temp float) TAGS (sensor_id int)
BASE ON test_db.p_device VIRTUAL 1;
-- Create a VCT under the child
CREATE VTABLE vct_t1 (status FROM src.c1, temp FROM src.c2)
USING test_db.p_temp TAGS (100, 1);
-- Query the child VST (leaf)
SELECT * FROM test_db.p_temp;
-- Query the parent VST (non-leaf) — returns UNION ALL of all descendant VCTs
SELECT count(*) FROM test_db.p_device;
Alter Inheritance
-- Add a parent
ALTER STABLE [db_name.]stb_name ADD BASE ON [db_name.]parent_stb_name;
-- Drop a parent (columns/tags contributed by that parent are removed)
ALTER STABLE [db_name.]stb_name DROP BASE ON [db_name.]parent_stb_name;
Non-leaf VST Queries
When you query a non-leaf VST (one that has child VSTs inheriting from it), the engine automatically rewrites the query into a UNION ALL of all leaf-descendant VCTs. This means:
SELECT * FROM parent_vstreturns data from all descendant VCTs.SELECT count(*) FROM parent_vstaggregates across all descendants.- Multi-level inheritance (grandparent → parent → leaf) is supported — querying the grandparent traverses the full descendant tree.
View Inheritance Relationships
SHOW VTABLE INHERITS;
This displays the parent-child relationships between VSTs, including database name, child stable name, and parent stable names.
Constraints
- Virtual only:
BASE ONrequires both parent and child to be virtual supertables (VIRTUAL 1). - Same database: Parent and child VSTs must reside in the same database.
- Column/tag conflict: The child's own column/tag names must not conflict with inherited names from any parent.
- Circular inheritance: Circular dependency chains are detected and rejected.
- Max parents: A VST can inherit from at most 10 parent VSTs.
- Non-leaf restrictions: A non-leaf VST (one with children) cannot have VCTs created directly under it — VCTs must be created under leaf VSTs. A parent VST that already has VCTs cannot be used as a
BASE ONtarget. - Schema changes:
ADD COLUMN,DROP COLUMN,ADD TAG,DROP TAG,RENAME TAG, and tag-width changes on a parent VST that has children are rejected. The parent's schema is frozen once it becomes a non-leaf.
Create Virtual Table
The CREATE VTABLE statement is used to create virtual basic tables and virtual subtables using virtual supertables as templates.
Create Virtual Supertables
Refer to the VIRTUAL parameter in Create Supertable.
Create Virtual Basic Table
CREATE VTABLE [IF NOT EXISTS] [db_name].vtb_name
(
ts_col_name timestamp,
create_definition[ ,create_definition] ...
)
[reference_definition | series_definition] ...
[TAGS (vtag_def [, vtag_def] ...)]
create_definition:
vtb_col_name column_definition
column_definition:
type_name [FROM column_reference]
column_reference:
[db_name.]table_name.col_name
| ext_source_name.db_name.table_name.col_name [WHERE filter_condition]
| ext_source_name.database_name.schema_name.table_name.col_name [WHERE filter_condition]
| reference_alias.col_name [WHERE filter_condition]
| series_alias.col_name
reference_definition:
REFERENCE reference_alias AS external_table_path TIMESTAMP time_column
series_definition:
SERIES series_alias AS ext_source_name.db_name.measurement_name
(tag_name = 'tag_value' [, tag_name = 'tag_value'] ...)
vtag_def:
tag_name type_name = const_value
| tag_name type_name FROM [db_name.]table_name.tag_name
For a TDengine table or an existing virtual table, column_reference is [db_name.]table_name.col_name, so the column definition remains type_name [FROM [db_name.]table_name.col_name]. ext_source_name names an existing external data source. series_alias names an InfluxDB SERIES declaration, and reference_alias names a REFERENCE declaration that selects an external time column. For the optional WHERE filter_condition on external references, see External Column Reference Filters. SQL Server external column references support three forms: the three-part ext_source_name.table_name.col_name (requires default DATABASE and SCHEMA on the source), the four-part ext_source_name.schema.table_name.col_name (requires a default DATABASE), and the five-part ext_source_name.database.schema.table_name.col_name; relative forms are expanded to the full five-part namespace when stored.
InfluxDB 1.8 and 3.x column references use ext_source_name.table_name.col_name (requires a default DATABASE on the source) or ext_source_name.db_name.table_name.col_name. For 1.8, retention_policy on the source selects the RP; it is not part of the column path, and the SERIES target remains three-part. See InfluxDB SERIES Column References for configuration and examples.
The same column_reference definition applies to virtual-subtable column reference lists and to ALTER VTABLE with ADD COLUMN ... FROM or ALTER COLUMN ... SET.
When creating a virtual basic table, you can declare tags with the TAGS clause; see the "Virtual Basic Table Tags" section below.
Create Virtual Subtable
CREATE VTABLE [IF NOT EXISTS] [db_name].vtb_name
(create_definition[ ,create_definition] ...)
USING [db_name.]stb_name
[(tag_definition [, tag_definition] ...)]
TAGS (tag_value [, tag_value] ...)
[reference_definition | series_definition] ...
create_definition:
[stb_col_name FROM] column_reference
| owned_col_name type_name [FROM column_reference]
tag_definition:
tag_name
| owned_tag_name type_name
tag_value:
const_value
| [db_name.]table_name.tag_name
| FROM [db_name.]table_name.tag_name
| tag_name FROM [db_name.]table_name.tag_name
Batch Creation of Virtual Subtables
A single CREATE VTABLE statement can contain multiple virtual-subtable clauses. Do not separate clauses with commas. Each clause can independently specify IF NOT EXISTS, a virtual supertable, column references, tag values, tag references, REFERENCE, and SERIES declarations.
CREATE VTABLE
IF NOT EXISTS meter_v1 (
voltage FROM source_meter_1.voltage,
current FROM source_meter_1.current
) USING meters_vst TAGS ('beijing', 1)
IF NOT EXISTS meter_v2 (
voltage FROM s2.voltage,
current FROM s2.current
) USING meters_vst TAGS ('shanghai', 2)
SERIES s2 AS influx_src.metrics.meters (site='shanghai');
The following rules apply to batch creation:
IF NOT EXISTSapplies only to the virtual subtable immediately following it.- Each target virtual subtable must be in the same database as the virtual supertable named by its
USINGclause. Different clauses can use different databases, virtual supertables, or vgroups. - TDengine validates every clause before sending create-table requests. If any clause has a syntax, metadata, permission, or reference error, no virtual subtable is created.
- After requests reach the server, the batch is not guaranteed to be atomic. If an error occurs after some virtual subtables have been created, those tables are not rolled back. Specify
IF NOT EXISTSfor every clause to make retries safe. - A clause cannot reference another virtual subtable created by the same batch statement. Every reference source must exist before the statement is executed.
- Virtual basic tables and virtual supertables cannot be created in batches. A statement also cannot mix regular subtables, virtual basic tables, and virtual subtables.
tag_value Syntax Notes
const_value: Use a literal constant as the tag value, matching the behavior of a regular subtable.FROM [db_name.]table_name.tag_name: A tag reference (tag-ref) to the specified source tag. Whentag_namebeforeFROMis omitted, the target tag name is the same as the source tag name. Whentag_name FROM ...is used, the source tag can be mapped to a different target tag name.[db_name.]table_name.tag_name: A shorthand tag-ref form withoutFROM, commonly used withALTER VTABLE ... SET TAG.
This is consistent with the column-reference form [stb_col_name FROM] table_name.col_name: the name before FROM is the virtual-table-side name, and the name after FROM is the source-side name.
Owned columns and owned tags
A virtual subtable can have members that are independent of the virtual supertable. Syntactically, the distinction is whether an explicit type is specified:
- In
create_definition, an entry without a type ([stb_col_name FROM] table_name.col_name) is an aligned column (existing semantics, aligned by name or position with the virtual supertable's columns); an entry with an explicit type (owned_col_name type_name [FROM table_name.col_name]) declares an owned column — the column name is not in the virtual supertable's schema. WithFROM, the column is bound to a data source; withoutFROM, it is declared unbound (supported fromv3.4.3.10), a state identical to runningALTER COLUMN ... SET NULLon a bound column, and it can be bound later withALTER COLUMN ... SET. An unbound column returnsNULLon the available timeline. Specifying a type for a column that already exists in the virtual supertable is rejected. - In
tag_definition, an entry without a type (tag_name) binds a virtual-supertable tag by name (existing semantics); an entry with an explicit type (owned_tag_name type_name) declares an owned tag, whose value is given positionally inTAGS(...)as a literal,NULL, or a tag-ref. Positional tag binding (omitting the tag list) does not support declaring owned tags.
Example
CREATE TABLE ev_log (ts timestamp, event bigint);
CREATE VTABLE v_d1 (
switch FROM p10.val, -- aligned column: no type
ext_ev BIGINT FROM ev_log.event -- owned column: explicit type + reference
) USING v_devices (
device, -- aligned tag: bound by name
owner VARCHAR(16) -- owned tag: explicit type
) TAGS ('d1', 'alice');
Owned columns and owned tags exist only on that virtual subtable: they are visible when you query the subtable directly (in SELECT *, owned columns appear after the virtual supertable's columns), but they are not visible when you query the virtual supertable — the virtual supertable's schema is unaffected.
An unbound owned column has no data source and provides no data; it does not contribute to the timestamp union, and when queried alongside other columns its value is NULL. A bare declaration cannot carry a WHERE filter condition (WHERE can only follow a FROM reference), and an owned column cannot be declared PRIMARY KEY.
Usage Notes:
- Naming rules for virtual tables/columns follow Name Rules.
- The maximum number of columns in a virtual table is 32767.
- Maximum table name length: 192 characters.
- The first column must be TIMESTAMP and is automatically set as primary key.
- Row length cannot exceed 512KB (Note: VARCHAR/NCHAR/GEOMETRY columns consume 2 extra bytes each).
- Specify maximum length for VARCHAR/NCHAR/GEOMETRY types (e.g., VARCHAR(20)).
- Virtual tables do not support the
BLOBorMEDIUMBLOBdata types. - Use
FROMto specify column data sources. Cross-database sources are supported viadb_name. Whendb_nameis omitted, TDengine uses the current database; if no current database is selected anddb_nameis not specified, the statement fails. - You cannot explicitly specify a source for the
tscolumn. During queries, the virtual table'stsvalues are the union of the timelines of the selected columns.REFERENCEbindings use their declared time columns, and other bindings use their existing timelines. - Virtual supertables only support creating virtual subtables, and virtual subtables can only use virtual supertables as templates.
- Ensure virtual tables' column/tag data types match their source columns/tags.
- Virtual table names must be unique within a database and cannot conflict with table names, and it is recommended that view names do not duplicate virtual table names (not enforced). When a view and a virtual table have the same name, operations such as writing, querying, granting, and revoking permissions prioritize the virtual table with the same name.
- When creating virtual subtables or virtual basic tables,
FROMcolumns can come from regular tables, subtables, or existing virtual tables. TDengine supertables, views, and TDengine tables with composite primary keys are not supported as direct sources. For MySQL, PostgreSQL, and SQL Server external tables, see External Column Reference Filters. - In
TAGS (...)for a virtual subtable, each tag can be a literal value or a tag-ref. Supported tag-ref forms aretable.tag,FROM table.tag, andtag_name FROM table.tag. Usedb_name.table.tagfor cross-database references. - Reference-related limits and behaviors are described in the "Virtual-Table Reference Capabilities" section below.
- Rules for virtual basic table tags (the
TAGSclause) are described in the "Virtual Basic Table Tags" section below. - An owned column can omit
FROMand stay unbound, but it still cannot store local data. When bound, the reference target must be a data column (not a tag column), its type must match the declared type, and reference cycles are not allowed. - Owned tags do not support the
JSONorDECIMALtypes. If the virtual supertable has a singleJSONtag, no owned tag can be declared on its virtual subtables. - Column names and tag names share a single namespace: owned column/tag names must not duplicate the virtual supertable's columns, tags, or existing owned members. In addition, if a new column/tag name introduced by
ALTER STABLEon a virtual supertable is already used by an owned member of any virtual subtable, thatALTERfails. - Columns of a virtual basic table must carry a type (
name TYPE [FROM ...]); the untypedname FROM ...form is reserved for aligned columns of virtual subtables.
Select an External Time Column
Virtual basic tables and virtual subtables can use REFERENCE to select a time column from a MySQL, PostgreSQL, SQL Server, or Oracle external table or view. The source object can have no primary key, and the selected time column can be an ordinary data column. Its connector-mapped type must be TIMESTAMP, and the virtual table's database PRECISION must satisfy the selected column's precision requirements. Time conversion, timezone interpretation, and precision validation follow the federated query rules.
A REFERENCE declaration stores the source object and time column. Each data column binds its value through reference_alias.column_name and stores its own optional WHERE condition. Filters follow the expression and type rules in the next section, can select any business columns, and can cover only part of a primary key. Queries construct timelines from the column bindings they read. An unused declaration contributes no timestamps.
CREATE VTABLE readings (
ts TIMESTAMP,
value DOUBLE FROM r1.value WHERE device = 'd1'
)
REFERENCE r1 AS sqlsrc.telemetry.dbo.readings_view TIMESTAMP EventTime;
CREATE STABLE readings_vst (ts TIMESTAMP, value DOUBLE) TAGS (device VARCHAR(16)) VIRTUAL 1;
CREATE VTABLE readings_d1 (
value FROM r1.value WHERE device = 'd1',
extra_value DOUBLE
) USING readings_vst TAGS ('d1')
REFERENCE r1 AS sqlsrc.telemetry.dbo.readings_view TIMESTAMP EventTime;
The example assumes that the SQL Server view readings_view exposes an EventTime column mapped to TIMESTAMP and a value column mapped to DOUBLE. Multiple aliases can select the same or different time columns from one object. Declarations follow the column list and precede TAGS for virtual basic tables. They follow TAGS for virtual subtables. Multiple REFERENCE and SERIES declarations can be interleaved and separated by whitespace.
Unquoted aliases are folded to lowercase, and backtick-quoted aliases preserve case. REFERENCE and SERIES share an alias namespace and use the existing duplicate-name checks. For a two-part alias.column input, resolution tries an exact REFERENCE alias, a local table with that name, and then a case-insensitive REFERENCE alias in declaration order, before continuing with existing source resolution. Different REFERENCE and SERIES candidates from mixed declarations produce an ambiguous-name error. Use db_name.table_name.column_name to select a local table that shares an alias name. Source object and column case matching follows the external source's rules.
Each virtual basic table or virtual subtable can store up to 128 REFERENCE declarations, including declarations with no dependent columns. Each declaration saves the full object identity resolved when it is created or modified. Later changes to USE or the source's default namespace preserve that saved binding.
Existing virtual basic tables and virtual subtables also support the following operations.
ALTER VTABLE readings ADD REFERENCE r2 AS sqlsrc.telemetry.dbo.readings_view
TIMESTAMP CollectedAt;
ALTER VTABLE readings ALTER COLUMN value SET r2.value WHERE device = 'd1';
ALTER VTABLE readings MODIFY REFERENCE r2 AS sqlsrc.telemetry.dbo.readings_view
TIMESTAMP EventTime;
ALTER VTABLE readings ALTER COLUMN value SET NULL;
ALTER VTABLE readings REMOVE REFERENCE r2;
ADD REFERENCE saves a new declaration. Existing columns use it after an explicit binding change. MODIFY REFERENCE requires the complete source object and time column. It validates every dependent value column, type, and saved filter before applying the change atomically. MODIFY and REMOVE find the saved alias by exact name after identifier processing. All dependent columns must be unbound or rebound before removing a declaration. SET NULL clears a column's binding and filter. REMOVE REFERENCE separately deletes the declaration. Dropping the virtual table also deletes its declarations.
Queries read by the selected time column and validate the raw records actually consumed after column-level filtering, before timestamp alignment. A NULL, duplicate, or decreasing timestamp returns an error. Validation continues across fetch batches and covers the input consumed by this query. LIMIT can be pushed down under existing optimization rules. OFFSET is applied locally after time validation, alignment, and query filtering. A successful query means its consumed input passed these checks.
External Column Reference Filters
Virtual basic tables and virtual subtables can reference data columns in MySQL, PostgreSQL, or SQL Server external tables and append WHERE to each column reference. The condition is saved in the virtual-table definition. Each query filters the external rows before aligning the referenced columns by timestamp.
filter_condition:
column_name comparison_operator literal
| (filter_condition)
| filter_condition AND filter_condition
comparison_operator: = | != | <> | < | <= | > | >=
Filter constraints
WHEREis supported for direct data-column references to MySQL, PostgreSQL, or SQL Server external tables and forREFERENCEdata-column bindings to MySQL, PostgreSQL, SQL Server, or Oracle. It cannot be appended to references to TDengine tables, other virtual tables, tag columns, or InfluxDBSERIES. Indirect references through other virtual tables retain filters already saved on the underlying references.- With a direct external column path, the external table must have a primary key containing exactly one column that maps to TDengine
TIMESTAMP. Every other primary-key column must be covered bycolumn = literalin each reference'sWHERE, ensuring at most one row per timestamp after filtering.WHEREis optional when the timestamp column is the entire primary key. - Each comparison must have an unqualified column name from the current external table on the left and a numeric, string, or boolean literal on the right. The filter may use external columns that are not referenced by the virtual table. Literals must convert to the external column's data type; timestamp values must be string literals.
- Each external column may appear only once in a filter. MySQL and SQL Server column matching is case insensitive (for SQL Server, the database collation applies). PostgreSQL column matching is case sensitive: identifiers without backticks are converted to lowercase, so use backticks to preserve column names containing uppercase letters. For example, PostgreSQL columns named
deviceandDeviceare distinct and may both appear in the same filter. For direct external column paths, non-timestamp primary-key columns require=; other columns support the comparison operators listed above. Multiple comparisons must be joined byAND; parentheses are allowed. OR,NOT,IN,LIKE,IS NULL,NULLvalues, functions, arithmetic expressions, subqueries, and parameter placeholders are not supported.BETWEENexpands into two comparisons of the same column and is therefore also unsupported.- A query's
WHEREcannot replace the primary-key filter required in a column definition. Different columns can reference different filtered results from the same external table. Filters apply independently; timestamps are the union of the filtered results for references involved in the query, with missing values filled withNULL.
Assume an existing MySQL external data source named mysql_src contains metrics.readings with columns ts, device, tenant, v, and w. Its primary key is (ts, device, tenant); ts maps to TIMESTAMP, and v and w map to INT:
CREATE VTABLE filtered_readings (
ts TIMESTAMP,
a INT FROM mysql_src.metrics.readings.v WHERE device = 'd1' AND tenant = 1,
b INT FROM mysql_src.metrics.readings.w WHERE device = 'd2' AND tenant = 1
);
CREATE STABLE filtered_stable (ts TIMESTAMP, a INT) TAGS (site INT) VIRTUAL 1;
CREATE VTABLE filtered_child (
a FROM mysql_src.metrics.readings.v WHERE device = 'd1' AND tenant = 1
) USING filtered_stable TAGS (1);
ALTER VTABLE filtered_readings ADD COLUMN c INT
FROM mysql_src.metrics.readings.v WHERE device = 'd1' AND tenant = 1 AND v >= 10;
ALTER VTABLE filtered_readings ALTER COLUMN a SET
mysql_src.metrics.readings.v WHERE device = 'd2' AND tenant = 1;
ALTER VTABLE filtered_readings ALTER COLUMN b SET NULL;
SHOW CREATE VTABLE filtered_readings;
ALTER COLUMN ... SET replaces the entire reference and its condition; there is no separate operation to edit only WHERE. Omitting WHERE when rebinding clears the old condition, but the new reference must still satisfy the primary-key requirements. In this example, the composite key requires equality conditions for both device and tenant. SET NULL clears both the reference and its filter. SHOW CREATE VTABLE includes the saved WHERE so that the definition can be recreated.
Queries revalidate saved conditions against the current external metadata. Removing a filter column, changing a type so that a literal is incompatible, or changing the primary key so that the filter no longer covers it causes affected queries to fail. Failure to execute the external filter also fails the query instead of returning unfiltered data.
Virtual Basic Table Tags
Like its columns, a virtual basic table's tags come in two forms, which can be mixed in the same table:
- Owned tag: A tag owned by the table itself. Its value is specified inline at creation (
tag_name type_name = const_value, where= NULLmeans a NULL value) or written later withSET TAG, and is stored in the table. An owned tag is a table-level constant: projecting it returns that constant for every row, and using it in aWHEREfilter is evaluated with constant semantics. - Tag reference (tag-ref):
tag_name type_name FROM [db_name.]table_name.tag_namereferences a tag of an underlying physical table and is resolved to the source tag's current value at query time. When used in aWHEREfilter, the predicate is pushed down to the source table's tag index so that only matching subtables are scanned.
Example
In a smart-meter scenario, the subtables of the supertable meters serve as tag-ref sources:
CREATE STABLE meters (ts TIMESTAMP, v INT) TAGS (region VARCHAR(16), gid INT);
CREATE TABLE d0 USING meters TAGS ('us-east', 1);
-- Create a virtual basic table with both an owned tag and a tag-ref
CREATE VTABLE vntb (
ts TIMESTAMP,
v INT FROM db.d0.v -- column reference
) TAGS (
owner VARCHAR(16) = 'alice', -- owned tag
level INT = 0, -- owned tag
region VARCHAR(16) FROM db.d0.region -- tag-ref, value follows d0.region
);
SELECT owner, level FROM vntb; -- owned tags, projected from the table itself
SELECT region FROM vntb; -- tag-ref, resolved from the source table
SELECT * FROM vntb WHERE region = 'us-east'; -- filter pushed down to the source tag index
Usage Notes:
- Every tag in the
TAGSclause must carry an explicit value: use= const_valuefor an owned tag (= NULLis a valid explicit value) orFROM [db_name.]table_name.tag_namefor a tag-ref. A baretag_name type_name(neither=norFROM) is rejected. - A tag-ref must point to a tag column of a TDengine table (a subtable or virtual subtable), not a data column; the declared type must match the source tag type; external data sources are not supported.
- Tag-refs follow the same permission rules as column references: creating or modifying a tag-ref requires
READpermission on the source table. - The
DECIMALtype is not supported for tags; tag count and total length limits are the same as for other tables — see General Restrictions. - A
JSONtag can only be declared at table creation and must be the only tag of the table;JSONtags cannot be added later withALTER. - Tag definitions do not support column options such as
PRIMARY KEY,ENCODE,COMPRESS, orCOMMENT.
Virtual-Table Reference Capabilities
What references mean
In a virtual table, a referenced column or tag does not store copied data. Instead, TDengine resolves it dynamically from the source table at query time:
- Tag reference (tag-ref): A virtual subtable's tag value references a tag column of another table and is resolved to the source tag's current value at query time. If the source tag is updated with
ALTER TABLE ... SET TAG, query results on the virtual table reflect the new value immediately. - Column reference (col-ref): A virtual-table data column references a data column of another table, including another virtual table. New data written to the source becomes visible through the virtual table on subsequent queries.
Reference chains
- Virtual-table columns can reference columns from existing virtual tables, so multi-hop chains such as virtual table -> virtual table -> physical table are supported.
- Tag-ref and col-ref can be mixed across multiple hops.
- Same-database and cross-database reference chains are supported.
Behavior when referenced objects change
| Change operation | Impact on virtual tables |
|---|---|
Source table tag updated with ALTER TABLE ... SET TAG | Queries on tag-ref virtual tables immediately reflect the new value |
| New data written to the source table | Queries on col-ref virtual tables can see the new data |
Source table dropped with DROP TABLE | Virtual-table queries fail because the source no longer exists |
Referenced source column dropped with ALTER TABLE ... DROP COLUMN | Virtual-table queries fail because the referenced column no longer exists |
| A referenced virtual table is dropped | Other virtual tables that reference it fail during query |
| Source tag-column type change | Rejected while dependent tag-refs exist |
Constraints
- A tag-ref must point to a tag column, not a data column, and the source tag type must match the target virtual tag type.
- A col-ref must point to a data column, not a tag column, and the source column type must match the target virtual column type.
- TDengine supertables, views, and TDengine tables with composite primary keys are not supported as reference sources. MySQL, PostgreSQL, and SQL Server external tables can use composite primary keys that meet the filter requirements.
- Reference cycles are not allowed and are rejected during validation.
- The total reference-chain depth cannot exceed 32 hops; validation or query execution returns
0x8000620Cwhen the chain exceeds that limit.
Query Virtual Tables
Virtual tables use the same query syntax as regular tables, but their dataset may vary between queries based on data alignment rules.
Data Alignment Rules
- Align data from multiple source tables by timestamp.
- Combine columns with same timestamp into one row; missing values fill with NULL.
- Virtual table timestamps are the union of all involved columns' origin tables' timestamps. Therefore, the number of rows in the result set may vary when different queries select different columns.
- Users can combine any columns from multiple tables; unselected columns are excluded.
Example:
Given tables t1, t2, t3 with data:
| t1 | t2 | t3 | ||||||
|---|---|---|---|---|---|---|---|---|
| ts | value | ts | value | ts | value1 | value2 | ||
| 0:00:01 | 1 | |||||||
| 0:00:02 | 20 | |||||||
| 0:00:03 | 300 | 3000 | ||||||
| 0:00:04 | 4 | 0:00:04 | 40 | |||||
| 0:00:05 | 50 | 0:00:05 | 500 | 5000 | ||||
Create a virtual table v1:
CREATE VTABLE v1 (
ts timestamp,
c1 int FROM t1.value,
c2 int FROM t2.value,
c3 int FROM t3.value1,
c4 int FROM t3.value2);
Querying all columns:
SELECT * FROM v1;
Result:
| v1 | ||||
|---|---|---|---|---|
| ts | c1 | c2 | c3 | c4 |
| 0:00:01 | 1 | |||
| 0:00:02 | 20 | |||
| 0:00:03 | 300 | 3000 | ||
| 0:00:04 | 4 | 40 | ||
| 0:00:05 | 50 | 500 | 5000 | |
Partial column query:
SELECT c1, c2 FROM v1;
Result:
| v1 | ||||
|---|---|---|---|---|
| ts | c1 | c2 | ||
| 0:00:01 | 1 | |||
| 0:00:02 | 20 | |||
| 0:00:04 | 4 | 40 | ||
| 0:00:05 | 50 | |||
Since the original tables t1 and t2 (corresponding to columns c1 and c2) lack the timestamp 0:00:03, this timestamp will not appear in the final result.
Modify Virtual Basic Tables
ALTER VTABLE [db_name.]vtb_name alter_table_clause
alter_table_clause: {
ADD COLUMN vtb_col_name vtb_column_type [FROM column_reference]
| DROP COLUMN vtb_col_name
| ALTER COLUMN vtb_col_name SET {column_reference | NULL}
| MODIFY COLUMN col_name column_type
| RENAME COLUMN old_col_name new_col_name
| ADD TAG tag_name tag_type [FROM [db_name.]table_name.tag_name]
| SET TAG tag_name = {new_tag_value | [db_name.]table_name.tag_name}
| DROP TAG tag_name
| RENAME TAG old_tag_name new_tag_name
| ADD REFERENCE reference_alias AS external_table_path TIMESTAMP time_column
| MODIFY REFERENCE reference_alias AS external_table_path TIMESTAMP time_column
| REMOVE REFERENCE reference_alias
}
Usage Notes
For virtual basic tables, the following modifications are supported:
ADD COLUMN: Add a column.DROP COLUMN: Drop a column.MODIFY COLUMN: Modify the column definition. For variable-length data types, this can be used only to increase the width, not decrease it. If the virtual-table column already has a source column, widening the column fails because the new width no longer matches the source-column width. Clear the source first withALTER COLUMN ... SET NULL, then modify the width.RENAME COLUMN: Rename a column.ALTER COLUMN ... SET: Change the source of a column.SET NULLclears the source of the virtual-table column.ADD TAG: Add a tag. WithoutFROMit adds an owned tag (initial valueNULL); withFROMit adds a tag-ref.SET TAG: Modify a tag. Setting it to a literal (includingNULL) clears any existing tag-ref and converts the tag to an owned tag; setting it to[db_name.]table_name.tag_nameconverts an owned tag to a tag-ref or repoints an existing tag-ref to another source tag. An error is returned if the source tag does not exist, the types do not match, or the target is a data column.DROP TAG: Drop a tag; both owned tags and tag-refs can be dropped.RENAME TAG: Change a tag's name; both owned tags and tag-refs can be renamed. Only the display name changes: an owned tag keeps its value and a tag-ref keeps pointing at its source tag. The new name must not clash with an existing column or tag of the table. Supported since v3.4.3.0.
Add Column
ALTER VTABLE vtb_name ADD COLUMN vtb_col_name vtb_col_type [FROM column_reference]
Drop Column
ALTER VTABLE vtb_name DROP COLUMN vtb_col_name
Modify Column Width
ALTER VTABLE vtb_name MODIFY COLUMN vtb_col_name data_type(length);
Rename Column
ALTER VTABLE vtb_name RENAME COLUMN old_col_name new_col_name
Change Column Source
ALTER VTABLE vtb_name ALTER COLUMN vtb_col_name SET {column_reference | NULL}
Add Tag
-- Add an owned tag (initial value NULL)
ALTER VTABLE vtb_name ADD TAG tag_name tag_type;
-- Add a tag-ref
ALTER VTABLE vtb_name ADD TAG tag_name tag_type FROM [db_name.]table_name.tag_name;
Modify Tag
-- Set an owned tag value; on a tag-ref this clears the reference and converts it to an owned tag
ALTER VTABLE vtb_name SET TAG tag_name = new_tag_value;
-- Convert an owned tag to a tag-ref, or repoint a tag-ref to another source tag
ALTER VTABLE vtb_name SET TAG tag_name = [db_name.]table_name.tag_name;
The conversion semantics of SET TAG are the same as for virtual subtables; see the "Modify Virtual Subtables" section below.
Drop Tag
ALTER VTABLE vtb_name DROP TAG tag_name;
Rename Tag
ALTER VTABLE vtb_name RENAME TAG old_tag_name new_tag_name;
Modify Virtual Subtables
ALTER VTABLE [db_name.]vtb_name alter_table_clause
alter_table_clause: {
ALTER COLUMN vtb_col_name SET {column_reference | NULL}
| ADD COLUMN owned_col_name type_name FROM column_reference
| ADD TAG owned_tag_name type_name
| SET TAG tag_name = {new_tag_value | [db_name.]table_name.tag_name}
| DROP COLUMN owned_col_name
| DROP TAG owned_tag_name
| ADD REFERENCE reference_alias AS external_table_path TIMESTAMP time_column
| MODIFY REFERENCE reference_alias AS external_table_path TIMESTAMP time_column
| REMOVE REFERENCE reference_alias
}
Usage Notes
- Changes to columns and tags that a virtual subtable inherits from its virtual supertable (cascaded members) must be performed through the virtual supertable.
- For a virtual subtable's owned columns and owned tags,
ADD COLUMN,ALTER COLUMN ... SET,ADD TAG,SET TAG,DROP COLUMN, andDROP TAGcan be executed directly on the subtable. DROP COLUMN/DROP TAGapply only to owned members; applying them to a cascaded member fails — useALTER STABLEinstead (which cascades to all virtual subtables under the virtual supertable).- After an owned member is dropped, its name is released immediately and can be reused by either a column or a tag. Dropping an owned tag that carries a tag-ref also clears that reference.
MODIFY COLUMN,RENAME COLUMN, andRENAME TAGare not supported for virtual subtables; the width and name of an owned member cannot be changed — drop and re-create it instead.
Modify Subtable Tag Value
ALTER VTABLE tb_name SET TAG tag_name1=new_tag_value1, tag_name2=new_tag_value2 ...;
SET TAG can assign either a literal value or a tag-ref to a tag. This also applies to owned tags: assigning a literal clears any existing reference; assigning a tag-ref makes the value track the source tag dynamically.
Set a Tag to a Literal
ALTER VTABLE v0 SET TAG local_tag='local0_updated';
When a tag is set to a literal value, any existing tag-ref on that tag is cleared. The tag then becomes a static value that no longer tracks the source.
Set a Tag to a tag-ref (create or repoint a reference)
-- Same database: reference the city tag of table src0
ALTER VTABLE v0 SET TAG ref_city=src0.city;
-- Cross database: use the db_name.table.tag three-part form
ALTER VTABLE v0 SET TAG ref_city=db1.src1.city;
After a tag is set to a tag-ref, queries resolve it to the referenced tag's current value at query time. This operation can both add a reference to a tag that was previously a literal and repoint an existing tag-ref to a different source tag. The constraints match those for tag-refs at CREATE VTABLE time:
- The referenced object must be a tag column (of a child table or virtual child table), not a data column.
- The source tag and target tag must have the same data type.
- Reference cycles are not allowed (for example, pointing a tag of
v_aat a virtual table that ultimately referencesv_aagain); this is validated and rejected. - The total reference chain depth must not exceed 32 hops; exceeding it returns error code
0x8000620C.
Repointing within a multi-hop reference chain
When a virtual subtable's tag is one link of a multi-hop chain (for example, v2_0.l2_ref_city -> v0.ref_city -> src0.city), you can adjust the reference at any level with SET TAG, and the change propagates at query time following dynamic-binding rules:
- Repointing an intermediate link (e.g.
ALTER VTABLE v0 SET TAG ref_city=src1.city) changes the result of upper-layer virtual tables that reference it. - Repointing the top link (e.g.
ALTER VTABLE v2_0 SET TAG l2_ref_city=db.v1.ref_city) can redirect it to a different chain, or even point it directly at a physical tag to "flatten" the chain. - Setting an intermediate tag to a literal clears that link's reference, severing the upper layer's propagation from the original physical source; the upper layer then resolves to that literal value.
Note: The batch form
ALTER VTABLE USING stb_name SET TAG ... WHERE ...(modifying tags through the virtual super table) accepts literal values only and does not support setting a tag to a tag-ref. To set or repoint a tag-ref, use the single-subtableALTER VTABLE vtb_name SET TAG ...syntax shown above.
Change Column Source
ALTER VTABLE vtb_name ALTER COLUMN vtb_col_name SET {column_reference | NULL}
ALTER COLUMN ... SET replaces the column's data-source binding and applies to both cascaded and owned columns. For an owned column, SET column_reference (re)binds it, while SET NULL clears the binding and keeps the column and its type; the column can be bound again later, and the cleared state is identical to a bare declaration at creation time. For a cascaded column, SET NULL clears only this table's data source; the virtual supertable's schema is unchanged.
-- Rebind the owned column ext_ev to another event table
ALTER VTABLE v_d1 ALTER COLUMN ext_ev SET db.ev_log_2.event;
-- Clear the binding: the column remains but provides no data
ALTER VTABLE v_d1 ALTER COLUMN ext_ev SET NULL;
-- Bind it again
ALTER VTABLE v_d1 ALTER COLUMN ext_ev SET db.ev_log.event;
Add an Owned Column
ALTER VTABLE vtb_name ADD COLUMN owned_col_name type_name FROM column_reference
Appends an owned column to a virtual subtable. When used with ADD COLUMN, the FROM data-source reference is mandatory — omitting it fails. To get an unbound owned column, use a bare declaration when creating the virtual subtable, or run SET NULL after adding it. The reference target must be a data column, its type must match the declared type, and validation follows the same rules as at creation time.
ALTER VTABLE v_d1 ADD COLUMN ext_v2 INT FROM p20.val;
Add an Owned Tag
ALTER VTABLE vtb_name ADD TAG owned_tag_name type_name
Appends an owned tag to a virtual subtable with an initial value of NULL; literal values or tag-refs are attached with SET TAG (see "Modify Subtable Tag Value" above).
ALTER VTABLE v_d1 ADD TAG dept VARCHAR(16);
ALTER VTABLE v_d1 SET TAG dept = 'rd';
Drop an Owned Column or Owned Tag
ALTER VTABLE vtb_name DROP COLUMN owned_col_name
ALTER VTABLE vtb_name DROP TAG owned_tag_name
Only owned members can be dropped. To remove a cascaded column or tag inherited from the virtual supertable, use ALTER STABLE stb_name DROP COLUMN/DROP TAG (this cascades to all virtual subtables and does not affect other owned members).
Drop Virtual Tables
You can drop one or more virtual tables (virtual regular tables or virtual subtables) in a single SQL statement. The tables can belong to different databases.
DROP VTABLE [IF EXISTS] [dbname].vtb_name [, [IF EXISTS] [dbname].vtb_name] ...;
Notes
- When dropping multiple tables, all tables in the list are validated first (whether each table exists, is a virtual table, and is not a virtual supertable). If any table fails validation, the entire statement fails and no tables are dropped.
IF EXISTSonly applies to the single table it precedes. For example, inDROP VTABLE IF EXISTS vtb1, vtb2, a missingvtb1is skipped, but a missingvtb2still returns an error.- If the same table name appears multiple times in the list, the first occurrence is dropped and subsequent occurrences return an error because the table no longer exists (consistent with
DROP TABLE). - A virtual supertable cannot be dropped with
DROP VTABLE; useDROP STABLEinstead.
View Virtual Table Information
List Virtual Tables
SHOW [NORMAL | CHILD] [db_name.]VTABLES [LIKE 'pattern'];
Usage Notes:
- If
db_nameis omitted,SHOW VTABLESlists virtual basic tables and virtual subtables in the current database. If no current database is selected anddb_nameis not specified, the statement fails withdatabase not specified.LIKEcan be used for fuzzy matching.NORMALlists only virtual basic tables, andCHILDlists only virtual subtables. SHOW TABLESdoes not return the virtual basic tables and virtual subtables described here; useSHOW VTABLESinstead.
Show Creation Statement
SHOW CREATE VTABLE [db_name.]vtable_name;
Displays the creation statement for the specified virtual table. For virtual subtables created with tag-ref, the returned statement preserves the tag-ref definition.
For a virtual subtable with owned members, the returned statement also includes the full definition of owned columns (with their source references) and owned tags (with their values or references); DECIMAL columns are rendered with precision and scale as DECIMAL(p,s). An owned column whose data source has been cleared is emitted as name TYPE without FROM, and replaying the statement keeps it unbound; a cascaded column with no data source is inherited from the virtual supertable and is not emitted. The output is a single, fully replayable DDL.
For a virtual basic table with tags, the returned statement includes the TAGS(...) clause: owned tags are inlined as = value (a NULL value is emitted as = NULL) and tag-refs as FROM db_name.table_name.tag_name, so a single statement fully recreates the virtual table.
SHOW CREATE VTABLE preserves REFERENCE alias bindings, column filters, full source object paths, and selected time columns. It emits mixed REFERENCE and SERIES declarations in their saved order, including declarations with no dependent columns.
Describe Structure
DESCRIBE [db_name.]vtb_name;
DESCRIBE shows the virtual table's columns and tags. For tag-ref or col-ref entries, the result also shows the reference source. Tag rows are marked with TAG (both owned tags of virtual basic tables and owned tags of virtual subtables). For a virtual subtable with owned members, owned columns appear as regular data columns, after the virtual supertable's columns.
For REFERENCE bindings, DESCRIBE and information_schema.ins_columns.col_source show the full value-column path, (TIMESTAMP time_column), and the saved WHERE condition. Length-limited output preserves character boundaries and ends with ... [truncated]. Source fields in information_schema.ins_virtual_child_columns retain their plain column paths.
Show Current Tag Values of a Virtual Child Table
SHOW TAGS FROM child_table_name [FROM db_name];
SHOW TAGS FROM [db_name.]child_table_name;
For tag-ref virtual subtables, SHOW TAGS returns the currently resolved tag values. SHOW TAGS also supports virtual basic tables: owned tags return the table's own value, and tag-refs return the resolved source value.
Validate Virtual-Table References
SHOW VTABLE VALIDATE FOR [db_name.]vtb_name;
SHOW VTABLE VALIDATE checks column/tag references for a virtual basic table or virtual child table and returns the same validation metadata as information_schema.ins_virtual_tables_referencing, including err_code and err_msg.
REFERENCE validation also returns reference_alias, reference_object, and reference_time_column. Declarations with no dependent columns return type = 2, with virtual_col_name and src_column_name set to NULL. Validation checks the current external object, time column, types, precision, and access. Timestamp values are validated when queries read data.
Query Virtual Basic Tables and Virtual Subtables
SELECT ... FROM information_schema.ins_tables WHERE type = 'VIRTUAL_NORMAL_TABLE' OR type = 'VIRTUAL_CHILD_TABLE';
SELECT ... FROM information_schema.ins_virtual_tables_referencing;
Use ins_virtual_tables_referencing to inspect source database, source table, source column, and validation status for virtual-table columns and tags.
Write to Virtual Tables
Writing or deleting data in virtual tables is not supported. Virtual tables are logical views computed from source tables.
Virtual Tables vs. Views
| Property | Virtual Table | View |
|---|---|---|
| Definition | Dynamic structure combining multiple tables by timestamp. | Saved SQL query definition. |
| Data Source | Multiple tables with timestamp alignment. | Single/multiple table query results. |
| Storage | No physical storage; dynamic generation. | No storage; query logic only. |
| Timestamp Handling | Aligns timestamps across tables. | Follows query logic. |
| Update Mechanism | Real-time reflection of source changes. | Depends on query execution. |
| Special Features | Supports NULL filling and interpolation (prev/next/linear). | No built-in interpolation. |
| Use Case | Time series alignment, cross-table analysis. | Simplify complex queries, access control. |
| Performance | Potentially higher complexity. | Similar to underlying queries. |
Mutual conversion between virtual tables and views is not supported. For example, you cannot create a view based on a virtual table or create a virtual table from a view.
Permissions
Virtual table permissions are categorized into READ and WRITE. Query operations require READ permission, while operations to delete or modify the virtual table itself require WRITE permission.
Syntax
Grant
GRANT privileges ON [db_name.]vtable_name TO user_name
privileges: {
ALL,
| priv_type [, priv_type] ...
}
priv_type: {
READ
| WRITE
}
Revoke
REVOKE privileges ON [db_name.]vtable_name FROM user_name
privileges: {
ALL,
| priv_type [, priv_type] ...
}
priv_type: {
READ
| WRITE
}
Permission Rules
- The creator of a virtual table and the root user have all permissions by default.
- Users can grant or revoke read/write permissions for specific virtual tables (including virtual supertables and virtual regular tables) via
dbname.vtbname. Direct permission operations on virtual subtables are not supported. - Virtual subtables and virtual supertables do not support tag-based authorization (table-level authorization). Virtual subtables inherit permissions from their virtual supertables.
- Granting and revoking permissions for other users must be performed through
GRANTandREVOKEstatements, and only the root user can execute these operations. - The detailed permission control rules are summarized below:
| No. | Operation | Permission Requirements |
|---|---|---|
| 1 | CREATE VTABLE | The user has WRITE permission on the database to which the virtual table belongs, and the user has READ permission on the source tables or source virtual tables referenced by the virtual table. |
| 2 | DROP/ALTER VTABLE | The user has WRITE permission on the virtual table. If specifying a source for either a column reference or a tag reference, the user must also have READ permission on the referenced source table or source virtual table. |
| 3 | SHOW VTABLES | None |
| 4 | SHOW CREATE VTABLE | None |
| 5 | DESCRIBE VTABLE | None |
| 6 | Query System Tables | None |
| 7 | SELECT FROM VTABLE | The user has READ permission on the virtual table. |
| 8 | GRANT/REVOKE | Only the root user has permission. |
Use Cases
| SQL Query | SQL Write | STMT Query | STMT Write |
|---|---|---|---|
| Supported | Not Supported | Not Supported | Not Supported |