You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/current/v25.2/create-table-as.md
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,8 @@ When the results of a query are reused multiple times within a larger query, a v
22
22
23
23
A view is also advisable when the results must be up-to-date; a view always retrieves the current data from the tables that the view query mentions.
24
24
25
+
{% include_cached new-in.html version="v25.2" %} Use `CREATE TABLE t AS ...` with the [`AS OF SYSTEM TIME` clause](#as-of-system-time-clause) to leverage [historical reads]({% link {{ page.version.version }}/as-of-system-time.md %}) to reduce contention and improve performance.
26
+
25
27
{{site.data.alerts.callout_info}}
26
28
The default rules for [column families]({% link {{ page.version.version }}/column-families.md %}) apply.
27
29
{{site.data.alerts.end}}
@@ -80,6 +82,7 @@ The user must have the `CREATE` [privilege]({% link {{ page.version.version }}/s
80
82
`opt_persistence_temp_table` | Defines the table as a session-scoped temporary table. For more information, see [Temporary Tables]({% link {{ page.version.version }}/temporary-tables.md %}).<br><br>Note that the `LOCAL`, `GLOBAL`, and `UNLOGGED` options are no-ops, allowed by the parser for PostgreSQL compatibility.<br><br>**Support for temporary tables is [in preview]({% link {{ page.version.version }}/cockroachdb-feature-availability.md %}#temporary-objects)**.
81
83
`opt_with_storage_parameter_list` | A comma-separated list of [spatial index tuning parameters]({% link {{ page.version.version }}/spatial-indexes.md %}#index-tuning-parameters). Supported parameters include `fillfactor`, `s2_max_level`, `s2_level_mod`, `s2_max_cells`, `geometry_min_x`, `geometry_max_x`, `geometry_min_y`, and `geometry_max_y`. The `fillfactor` parameter is a no-op, allowed for PostgreSQL-compatibility.<br><br>For details, see [Spatial index tuning parameters]({% link {{ page.version.version }}/spatial-indexes.md %}#index-tuning-parameters). For an example, see [Create a spatial index that uses all of the tuning parameters]({% link {{ page.version.version }}/spatial-indexes.md %}#create-a-spatial-index-that-uses-all-of-the-tuning-parameters).
82
84
`ON COMMIT PRESERVE ROWS` | This clause is a no-op, allowed by the parser for PostgreSQL compatibility. CockroachDB only supports session-scoped [temporary tables]({% link {{ page.version.version }}/temporary-tables.md %}), and does not support the clauses `ON COMMIT DELETE ROWS` and `ON COMMIT DROP`, which are used to define transaction-scoped temporary tables in PostgreSQL.
85
+
`AS OF SYSTEM TIME {timestamp}` <aname="as-of-system-time-clause"></a> | **New in v25.2** Used to [create a new table from an existing table as of a historical timestamp](#create-a-new-table-from-an-existing-table-as-of-a-historical-timestamp).
83
86
84
87
## Known limitations
85
88
@@ -287,6 +290,34 @@ You can define the [column families]({% link {{ page.version.version }}/column-f
287
290
(1 row)
288
291
~~~
289
292
293
+
### Populate `CREATE TABLE AS` with historical data
294
+
295
+
{% include_cached new-in.html version="v25.2" %} CockroachDB supports creating a table using historical data using the [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}) clause. You can use this to create a new table based on the state of an existing table as of a specific [timestamp]({% link {{ page.version.version }}/timestamp.md %}) in the past. This is useful for:
296
+
297
+
- Generating static datasets for reporting or analytical workloads without increasing contention on production tables or otherwise impacting performance.
298
+
- Analyzing data changes over time.
299
+
- Preserving historical data for regulatory or investigative purposes.
300
+
301
+
{{site.data.alerts.callout_info}}
302
+
The timestamp must be within the [garbage collection (GC) window]({% link {{ page.version.version }}/architecture/storage-layer.md %}#garbage-collection) of the source table for the data to be available.
303
+
{{site.data.alerts.end}}
304
+
305
+
The syntax for creating a new table from a historical table at a given timestamp is as follows; it creates a new table at the most recent timestamp that can perform a [follower read]({% link {{ page.version.version }}/follower-reads.md %}).
306
+
307
+
{% include_cached copy-clipboard.html %}
308
+
~~~sql
309
+
CREATETABLEanalysis_vehicle_location_histories
310
+
ASSELECT*FROM vehicle_location_histories
311
+
AS OF SYSTEM TIME follower_read_timestamp();
312
+
~~~
313
+
314
+
~~~
315
+
NOTICE: CREATE TABLE ... AS does not copy over indexes, default expressions, or constraints; the new table has a hidden rowid primary key column
316
+
CREATE TABLE AS
317
+
~~~
318
+
319
+
For more information about historical reads using the `AS OF SYSTEM TIME` clause, refer to [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}).
320
+
290
321
## See also
291
322
292
323
-[Selection Queries]({% link {{ page.version.version }}/selection-queries.md %})
@@ -299,3 +330,5 @@ You can define the [column families]({% link {{ page.version.version }}/column-f
299
330
-[`ALTER PRIMARY KEY`]({% link {{ page.version.version }}/alter-table.md %}#alter-primary-key)
300
331
-[`ALTER TABLE`]({% link {{ page.version.version }}/alter-table.md %})
301
332
-[Online Schema Changes]({% link {{ page.version.version }}/online-schema-changes.md %})
333
+
-[`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %})
334
+
-[Follower Reads]({% link {{ page.version.version }}/follower-reads.md %})
0 commit comments