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
+27-21Lines changed: 27 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ The user must have the `CREATE` [privilege]({% link {{ page.version.version }}/s
78
78
`create_as_col_qual_list` | An optional column definition, which may include [primary key constraints]({% link {{ page.version.version }}/primary-key.md %}) and [column family assignments]({% link {{ page.version.version }}/column-families.md %}).
79
79
`family_def` | An optional [column family definition]({% link {{ page.version.version }}/column-families.md %}). Column family names must be unique within the table but can have the same name as columns, constraints, or indexes.
80
80
`create_as_constraint_def` | An optional [primary key constraint]({% link {{ page.version.version }}/primary-key.md %}).
81
-
`select_stmt` | A [selection query]({% link {{ page.version.version }}/selection-queries.md %}) to provide the data.
81
+
`select_stmt` | A [selection query]({% link {{ page.version.version }}/select-clause.md %}) to provide the data.
82
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)**.
83
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).
84
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.
@@ -302,40 +302,35 @@ You can define the [column families]({% link {{ page.version.version }}/column-f
302
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
303
{{site.data.alerts.end}}
304
304
305
-
The syntax for creating a new table from a historical table at a given timestamp is as follows; this example creates a new table at the most recent timestamp that can perform a [follower read]({% link {{ page.version.version }}/follower-reads.md %}).
305
+
The following example creates a new table from the [`movr`]({% link {{ page.version.version }}/movr.md %}) dataset at the most recent timestamp that can perform a [follower read]({% link {{ page.version.version }}/follower-reads.md %}).
306
306
307
307
{% include_cached copy-clipboard.html %}
308
308
~~~sql
309
309
CREATETABLEanalysis_vehicle_location_histories
310
-
ASSELECT*FROM vehicle_location_histories
310
+
ASSELECT*FROMmovr.vehicle_location_histories
311
311
AS OF SYSTEM TIME follower_read_timestamp();
312
312
~~~
313
313
314
+
~~~
315
+
CREATE TABLE AS
316
+
~~~
317
+
314
318
#### Undo an accidental table deletion
315
319
316
-
The following steps show how to undo an accidental table deletion using `CREATE TABLE AS ... AS OF SYSTEM TIME`.
320
+
The following steps use a table from the [`movr`]({% link {{ page.version.version }}/movr.md %}) dataset to show how to undo an accidental table deletion using `CREATE TABLE AS ... AS OF SYSTEM TIME`.
317
321
318
-
1. Create and populate a table:
319
-
320
-
{% include_cached copy-clipboard.html %}
321
-
~~~sql
322
-
CREATETABLEcustomers (
323
-
id INTPRIMARY KEY,
324
-
name STRING
325
-
);
326
-
327
-
INSERT INTO customers (id, name) VALUES (1, 'Alice'), (2, 'Bob');
328
-
~~~
329
-
330
-
1. Get a timestampfrom before the table is deleted:
322
+
1. Get a timestamp before the table is deleted in an upcoming step:
331
323
332
324
{% include_cached copy-clipboard.html %}
333
325
~~~sql
334
326
SELECT now();
335
327
~~~
336
328
337
329
~~~
338
-
2025-06-0919:55:40.286833+00
330
+
now
331
+
--------------------------------
332
+
2025-06-1715:04:15.82632+00
333
+
(1 row)
339
334
~~~
340
335
341
336
1. Wait a few seconds to simulate time passing (adjust as needed):
@@ -345,18 +340,29 @@ The following steps show how to undo an accidental table deletion using `CREATE
345
340
SELECT pg_sleep(5);
346
341
~~~
347
342
343
+
~~~
344
+
pg_sleep
345
+
------------
346
+
t
347
+
(1 row)
348
+
~~~
349
+
348
350
1. Drop the original table:
349
351
350
352
{% include_cached copy-clipboard.html %}
351
353
~~~ sql
352
-
DROPTABLE customers;
354
+
DROPTABLEmovr.vehicle_location_histories;
355
+
~~~
356
+
357
+
~~~
358
+
DROPTABLE
353
359
~~~
354
360
355
-
1. Restore the table using the [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}) clause:
361
+
1. Restore the table using the [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}) clauseand the timestamp you obtained before dropping the table:
356
362
357
363
{% include_cached copy-clipboard.html %}
358
364
~~~ sql
359
-
CREATETABLEcustomers_restoredASSELECT*FROMcustomersAS OF SYSTEM TIME'2025-06-09 19:55:40.286833+00';
365
+
CREATETABLEmovr.vehicle_location_historiesASSELECT*FROMmovr.vehicle_location_historiesAS OF SYSTEM TIME'2025-06-17 15:04:15.82632+00';
0 commit comments