|
| 1 | +--- |
| 2 | +title: INSPECT |
| 3 | +summary: Use the INSPECT statement to run data consistency validation checks against tables or databases. |
| 4 | +toc: true |
| 5 | +docs_area: reference.sql |
| 6 | +--- |
| 7 | + |
| 8 | +The `INSPECT` [statement]({% link {{ page.version.version }}/sql-statements.md %}) runs a data consistency validation job against a table or database. The initial validation checks primary-to-secondary index consistency and records any errors. To display any errors recorded by the validation job, use [`SHOW INSPECT ERRORS`]({% link {{ page.version.version }}/show-inspect-errors.md %}). |
| 9 | + |
| 10 | +{{site.data.alerts.callout_info}} |
| 11 | +`INSPECT` is used to verify data integrity. It does not automatically repair errors. |
| 12 | +{{site.data.alerts.end}} |
| 13 | + |
| 14 | +## Required privileges |
| 15 | + |
| 16 | +To run `INSPECT` and view its results, the user must have: |
| 17 | + |
| 18 | +- The `INSPECT` system-level [privilege]({% link {{ page.version.version }}/security-reference/authorization.md %}#supported-privileges) is required to run the `INSPECT` statement. |
| 19 | +- The `VIEWSYSTEMTABLE` system-level [privilege]({% link {{ page.version.version }}/security-reference/authorization.md %}#supported-privileges), which is required to view the results of [`SHOW INSPECT ERRORS`]({% link {{ page.version.version }}/show-inspect-errors.md %})). |
| 20 | + |
| 21 | +## Synopsis |
| 22 | + |
| 23 | +<div> |
| 24 | + {% remote_include https://raw.githubusercontent.com/cockroachdb/generated-diagrams/{{ page.release_info.crdb_branch_name }}/grammar_svg/inspect_table.html %} |
| 25 | +</div> |
| 26 | + |
| 27 | +<div> |
| 28 | + {% remote_include https://raw.githubusercontent.com/cockroachdb/generated-diagrams/{{ page.release_info.crdb_branch_name }}/grammar_svg/inspect_database.html %} |
| 29 | +</div> |
| 30 | + |
| 31 | +## Parameters |
| 32 | + |
| 33 | +Parameter | Description |
| 34 | +----------|------------ |
| 35 | +`table_name` | The [table]({% link {{ page.version.version }}/create-table.md %}) to inspect. |
| 36 | +`db_name` | The [database]({% link {{ page.version.version }}/create-database.md %}) to inspect. |
| 37 | +`opt_as_of_clause` | Optional. Run the inspection against a historical read timestamp using `INSPECT ... AS OF SYSTEM TIME {expr}`. For an example, see [Inspect at a specific timestamp](#inspect-at-a-specific-timestamp). For more information about historical reads, see [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}). |
| 38 | +`opt_inspect_options_clause` | Optional. Control which [indexes]({% link {{ page.version.version }}/indexes.md %}) are inspected using `INSPECT ... WITH OPTIONS (...)`. For an example, see [Inspect a table for specific indexes](#inspect-a-table-for-specific-indexes). See [Options](#options). |
| 39 | + |
| 40 | +### Options |
| 41 | + |
| 42 | +Option | Description |
| 43 | +-------|------------ |
| 44 | +`INDEX ALL` | Inspect all supported index types in the target table or database. This is the default. |
| 45 | +`INDEX ({index_name} [, ...])` | Inspect only the specified indexes. Note that `INDEX ALL` and this option are mutually exclusive. |
| 46 | + |
| 47 | +## Considerations |
| 48 | + |
| 49 | +- `INSPECT` must be run in an [implicit transaction]({% link {{ page.version.version }}/transactions.md %}#individual-statements). It cannot be run inside a multi-statement transaction. For more information, see [Run Multi-Statement Transactions]({% link {{ page.version.version }}/run-multi-statement-transactions.md %}). |
| 50 | +- `INSPECT` always runs as a [background job]({% link {{ page.version.version }}/show-jobs.md %}). |
| 51 | +- `INSPECT` runs with low priority under the [admission control]({% link {{ page.version.version }}/admission-control.md %}) subsystem and may take time on large datasets. Plan to run it during periods of lower system load. |
| 52 | +- The following index types are unsupported: |
| 53 | + - [Vector indexes]({% link {{ page.version.version }}/vector-indexes.md %}) |
| 54 | + - [Partial indexes]({% link {{ page.version.version }}/partial-indexes.md %}) |
| 55 | + - [Expression indexes]({% link {{ page.version.version }}/expression-indexes.md %}) |
| 56 | +- Unsupported index types are automatically skipped when using the default `INDEX ALL` behavior. If an unsupported index type is directly requested using `INDEX {index_name}`, the statement will fail before starting. |
| 57 | + |
| 58 | +## Examples |
| 59 | + |
| 60 | +### `INSPECT` a table (all supported indexes) |
| 61 | + |
| 62 | +{% include_cached copy-clipboard.html %} |
| 63 | +~~~ sql |
| 64 | +INSPECT TABLE movr.public.users; |
| 65 | +~~~ |
| 66 | + |
| 67 | +~~~ |
| 68 | +NOTICE: waiting for INSPECT job to complete: 1141477630617223169 |
| 69 | +If the statement is canceled, the job will continue in the background. |
| 70 | +~~~ |
| 71 | + |
| 72 | +### `INSPECT` a table for specific indexes |
| 73 | + |
| 74 | +{% include_cached copy-clipboard.html %} |
| 75 | +~~~ sql |
| 76 | +INSPECT TABLE movr.public.vehicles WITH OPTIONS INDEX (vehicles_auto_index_fk_city_ref_users); |
| 77 | +~~~ |
| 78 | + |
| 79 | +~~~ |
| 80 | +NOTICE: waiting for INSPECT job to complete: 1141477560713150465 |
| 81 | +If the statement is canceled, the job will continue in the background. |
| 82 | +~~~ |
| 83 | + |
| 84 | +### `INSPECT` at a specific timestamp |
| 85 | + |
| 86 | +{% include_cached copy-clipboard.html %} |
| 87 | +~~~ sql |
| 88 | +INSPECT TABLE movr.public.users AS OF SYSTEM TIME '-10s'; |
| 89 | +~~~ |
| 90 | + |
| 91 | +~~~ |
| 92 | +NOTICE: waiting for INSPECT job to complete: 1141477013029322753 |
| 93 | +If the statement is canceled, the job will continue in the background. |
| 94 | +~~~ |
| 95 | + |
| 96 | +### Checking `INSPECT` job status |
| 97 | + |
| 98 | +When you issue the `INSPECT` statement, a `NOTICE` message is returned to the client showing the job ID: |
| 99 | + |
| 100 | +~~~ |
| 101 | +NOTICE: waiting for INSPECT job to complete: 1141477013029322753 |
| 102 | +If the statement is canceled, the job will continue in the background. |
| 103 | +~~~ |
| 104 | + |
| 105 | +You can check the status of the `INSPECT` job using a statement like the following: |
| 106 | + |
| 107 | +{% include_cached copy-clipboard.html %} |
| 108 | +~~~ sql |
| 109 | +SELECT * FROM [SHOW JOBS] WHERE job_id = 1141477013029322753; |
| 110 | +~~~ |
| 111 | +~~~ |
| 112 | + job_id | job_type | description | user_name | status | running_status | created | started | finished | modified | fraction_completed | error | coordinator_id |
| 113 | +----------------------+----------+---------------------------------+-----------+-----------+----------------+------------------------+------------------------+------------------------+------------------------+--------------------+-------+----------------- |
| 114 | + 1141477013029322753 | INSPECT | INSPECT TABLE movr.public.users | node | succeeded | NULL | 2026-01-14 20:12:19+00 | 2026-01-14 20:12:19+00 | 2026-01-14 20:12:19+00 | 2026-01-14 20:12:19+00 | 1 | | 1 |
| 115 | +~~~ |
| 116 | + |
| 117 | +### Viewing `INSPECT` results |
| 118 | + |
| 119 | +To view errors found by an inspection job, use [`SHOW INSPECT ERRORS`]({% link {{ page.version.version }}/show-inspect-errors.md %}). Errors are stored in an internal system table and are subject to a 90 day retention policy. |
| 120 | + |
| 121 | +## See also |
| 122 | + |
| 123 | +- [`SHOW INSPECT ERRORS`]({% link {{ page.version.version }}/show-inspect-errors.md %}) |
| 124 | +- [`SHOW JOBS`]({% link {{ page.version.version }}/show-jobs.md %}) |
| 125 | +- [Jobs page in DB Console]({% link {{ page.version.version }}/ui-jobs-page.md %}) |
| 126 | +- [`AS OF SYSTEM TIME`]({% link {{ page.version.version }}/as-of-system-time.md %}) |
| 127 | +- [Supported privileges]({% link {{ page.version.version }}/security-reference/authorization.md %}#supported-privileges) |
0 commit comments