# F.2.安切克

F.2.1.功能F.2.2.可选择的*希帕尔指数*验证F.2.3.使用安切克有效地F.2.4.修复腐败

这个安切克模块提供的函数允许您验证关系结构的逻辑一致性。

B-树检查功能验证各种不变性在特定关系的表示结构中。索引扫描和其他重要操作背后的访问方法函数的正确性取决于这些不变量始终保持不变。例如,除其他事项外,某些函数验证所有B-树页面是否具有“逻辑”顺序的项(例如,对于页面上的B-树索引)文本,索引元组应按词汇顺序排列)。如果某个特定的不变量以某种方式无法保持不变,我们可以预期受影响页面上的二进制搜索会错误地引导索引扫描,从而导致对SQL查询的错误回答。如果结构似乎有效,则不会引发错误。

验证过程与索引扫描本身使用的过程相同,可能是用户定义的操作员类别代码。例如,B树索引验证依赖于与一个或多个B树支持函数1例程进行的比较。看见第38.16.3节有关操作员级支持功能的详细信息。

与通过引发错误报告损坏的B树检查函数不同,堆检查函数验证\u heapam检查一个表并尝试返回一组行,每个检测到的损坏一行。尽管如此,如果验证\u heapam如果函数本身已损坏,则该函数可能无法继续,反而可能会引发错误。

执行许可安切克功能可以授予非超级用户,但在授予此类权限之前,应仔细考虑数据安全和隐私问题。虽然这些函数生成的损坏报告不太关注损坏数据的内容,而更关注该数据的结构和发现的损坏的性质,但获得执行这些函数的权限的攻击者(尤其是如果攻击者也可能导致损坏的话)可能能够从这些消息中推断出数据本身的某些内容。

# F.2.1.功能

bt_index_check(index regclass,heapallindexed boolean)返回void

bt_索引_检查测试其目标B树索引是否尊重各种不变量。用法示例:

test=# SELECT bt_index_check(index => c.oid, heapallindexed => i.indisunique),
               c.relname,
               c.relpages
FROM pg_index i
JOIN pg_opclass op ON i.indclass[0] = op.oid
JOIN pg_am am ON op.opcmethod = am.oid
JOIN pg_class c ON i.indexrelid = c.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE am.amname = 'btree' AND n.nspname = 'pg_catalog'
-- Don't check temp tables, which may be from another session:
AND c.relpersistence != 't'
-- Function may throw an error when this is omitted:
AND c.relkind = 'i' AND i.indisready AND i.indisvalid
ORDER BY c.relpages DESC LIMIT 10;
 bt_index_check |             relname             | relpages
### Tip

`bt_index_check` and `bt_index_parent_check` both output log messages about the verification process at `DEBUG1` and `DEBUG2` severity levels. These messages provide detailed information about the verification process that may be of interest to PostgreSQL developers. Advanced users may also find this information helpful, since it provides additional context should verification actually detect an inconsistency. Running:

设置client_min_messages=DEBUG1;

 in an interactive psql session before running a verification query will display messages about the progress of verification with a manageable level of detail.

` verify_heapam(relation regclass, on_error_stop boolean, check_toast boolean, skip text, startblock bigint, endblock bigint, blkno OUT bigint, offnum OUT integer, attnum OUT integer, msg OUT text) returns setof record `

 Checks a table for structural corruption, where pages in the relation contain data that is invalidly formatted, and for logical corruption, where pages are structurally valid but inconsistent with the rest of the database cluster.

 The following optional arguments are recognized:

`on_error_stop`

 If true, corruption checking stops at the end of the first block in which any corruptions are found.

 Defaults to false.

`check_toast`

 If true, toasted values are checked against the target relation's TOAST table.

 This option is known to be slow. Also, if the toast table or its index is corrupt, checking it against toast values could conceivably crash the server, although in many cases this would just produce an error.

 Defaults to false.

`skip`

 If not `none`, corruption checking skips blocks that are marked as all-visible or all-frozen, as specified. Valid options are `all-visible`, `all-frozen` and `none`.

 Defaults to `none`.

`startblock`

 If specified, corruption checking begins at the specified block, skipping all previous blocks. It is an error to specify a *`startblock`* outside the range of blocks in the target table.

 By default, checking begins at the first block.

`endblock`

 If specified, corruption checking ends at the specified block, skipping all remaining blocks. It is an error to specify an *`endblock`* outside the range of blocks in the target table.

 By default, all blocks are checked.

 For each corruption detected, `verify_heapam` returns a row with the following columns:

`blkno`

 The number of the block containing the corrupt page.

`offnum`

 The OffsetNumber of the corrupt tuple.

`attnum`

 The attribute number of the corrupt column in the tuple, if the corruption is specific to a column and not the tuple as a whole.

`msg`

 A message describing the problem detected.

### F.2.2. Optional *`heapallindexed`* Verification

 When the *`heapallindexed`* argument to B-Tree verification functions is `true`, an additional phase of verification is performed against the table associated with the target index relation. This consists of a “dummy” `CREATE INDEX` operation, which checks for the presence of all hypothetical new index tuples against a temporary, in-memory summarizing structure (this is built when needed during the basic first phase of verification). The summarizing structure “fingerprints” every tuple found within the target index. The high level principle behind *`heapallindexed`* verification is that a new index that is equivalent to the existing, target index must only have entries that can be found in the existing structure.

 The additional *`heapallindexed`* phase adds significant overhead: verification will typically take several times longer. However, there is no change to the relation-level locks acquired when *`heapallindexed`* verification is performed.

 The summarizing structure is bound in size by `maintenance_work_mem`. In order to ensure that there is no more than a 2% probability of failure to detect an inconsistency for each heap tuple that should be represented in the index, approximately 2 bytes of memory are needed per tuple. As less memory is made available per tuple, the probability of missing an inconsistency slowly increases. This approach limits the overhead of verification significantly, while only slightly reducing the probability of detecting a problem, especially for installations where verification is treated as a routine maintenance task. Any single absent or malformed tuple has a new opportunity to be detected with each new verification attempt.

### F.2.3. Using `amcheck` Effectively

`amcheck` can be effective at detecting various types of failure modes that [data checksums](app-initdb.html#APP-INITDB-DATA-CHECKSUMS) will fail to catch. These include:

* Structural inconsistencies caused by incorrect operator class implementations.

   This includes issues caused by the comparison rules of operating system collations changing. Comparisons of datums of a collatable type like `text` must be immutable (just as all comparisons used for B-Tree index scans must be immutable), which implies that operating system collation rules must never change. Though rare, updates to operating system collation rules can cause these issues. More commonly, an inconsistency in the collation order between a primary server and a standby server is implicated, possibly because the *major* operating system version in use is inconsistent. Such inconsistencies will generally only arise on standby servers, and so can generally only be detected on standby servers.

   If a problem like this arises, it may not affect each individual index that is ordered using an affected collation, simply because *indexed* values might happen to have the same absolute ordering regardless of the behavioral inconsistency. See [Section 24.1](locale.html) and [Section 24.2](collation.html) for further details about how PostgreSQL uses operating system locales and collations.

* Structural inconsistencies between indexes and the heap relations that are indexed (when *`heapallindexed`* verification is performed).

   There is no cross-checking of indexes against their heap relation during normal operation. Symptoms of heap corruption can be subtle.

* Corruption caused by hypothetical undiscovered bugs in the underlying PostgreSQL access method code, sort code, or transaction management code.

   Automatic verification of the structural integrity of indexes plays a role in the general testing of new or proposed PostgreSQL features that could plausibly allow a logical inconsistency to be introduced. Verification of table structure and associated visibility and transaction status information plays a similar role. One obvious testing strategy is to call `amcheck` functions continuously when running the standard regression tests. See [Section 33.1](regress-run.html) for details on running the tests.

* File system or storage subsystem faults where checksums happen to simply not be enabled.

   Note that `amcheck` examines a page as represented in some shared memory buffer at the time of verification if there is only a shared buffer hit when accessing the block. Consequently, `amcheck` does not necessarily examine data read from the file system at the time of verification. Note that when checksums are enabled, `amcheck` may raise an error due to a checksum failure when a corrupt block is read into a buffer.

* Corruption caused by faulty RAM, or the broader memory subsystem.

  PostgreSQL does not protect against correctable memory errors and it is assumed you will operate using RAM that uses industry standard Error Correcting Codes (ECC) or better protection. However, ECC memory is typically only immune to single-bit errors, and should not be assumed to provide *absolute* protection against failures that result in memory corruption.

   When *`heapallindexed`* verification is performed, there is generally a greatly increased chance of detecting single-bit errors, since strict binary equality is tested, and the indexed attributes within the heap are tested.

 Structural corruption can happen due to faulty storage hardware, or relation files being overwritten or modified by unrelated software. This kind of corruption can also be detected with [data page checksums](checksums.html).

 Relation pages which are correctly formatted, internally consistent, and correct relative to their own internal checksums may still contain logical corruption. As such, this kind of corruption cannot be detected with checksums. Examples include toasted values in the main table which lack a corresponding entry in the toast table, and tuples in the main table with a Transaction ID that is older than the oldest valid Transaction ID in the database or cluster.

 Multiple causes of logical corruption have been observed in production systems, including bugs in the PostgreSQL server software, faulty and ill-conceived backup and restore tools, and user error.

 Corrupt relations are most concerning in live production environments, precisely the same environments where high risk activities are least welcome. For this reason, `verify_heapam` has been designed to diagnose corruption without undue risk. It cannot guard against all causes of backend crashes, as even executing the calling query could be unsafe on a badly corrupted system. Access to [catalog tables](catalogs-overview.html) is performed and could be problematic if the catalogs themselves are corrupted.

 In general, `amcheck` can only prove the presence of corruption; it cannot prove its absence.

### F.2.4. Repairing Corruption

 No error concerning corruption raised by `amcheck` should ever be a false positive. `amcheck` raises errors in the event of conditions that, by definition, should never happen, and so careful analysis of `amcheck` errors is often required.

 There is no general method of repairing problems that `amcheck` detects. An explanation for the root cause of an invariant violation should be sought. [pageinspect](pageinspect.html) may play a useful role in diagnosing corruption that `amcheck` detects. A `REINDEX` may not be effective in repairing corruption.