| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* verify_common.c |
| 4 |
* Utility functions common to all access methods. |
| 5 |
* |
| 6 |
* Copyright (c) 2016-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* IDENTIFICATION |
| 9 |
* contrib/amcheck/verify_common.c |
| 10 |
* |
| 11 |
*------------------------------------------------------------------------- |
| 12 |
*/ |
| 13 |
#include "postgres.h" |
| 14 |
|
| 15 |
#include "access/genam.h" |
| 16 |
#include "access/table.h" |
| 17 |
#include "access/tableam.h" |
| 18 |
#include "verify_common.h" |
| 19 |
#include "catalog/index.h" |
| 20 |
#include "catalog/pg_am.h" |
| 21 |
#include "commands/tablecmds.h" |
| 22 |
#include "utils/guc.h" |
| 23 |
#include "utils/syscache.h" |
| 24 |
|
| 25 |
static bool amcheck_index_mainfork_expected(Relation rel); |
| 26 |
|
| 27 |
|
| 28 |
/* |
| 29 |
* Check if index relation should have a file for its main relation fork. |
| 30 |
* Verification uses this to skip unlogged indexes when in hot standby mode, |
| 31 |
* where there is simply nothing to verify. |
| 32 |
* |
| 33 |
* NB: Caller should call index_checkable() before calling here. |
| 34 |
*/ |
| 35 |
static bool |
| 36 |
amcheck_index_mainfork_expected(Relation rel) |
| 37 |
{ |
| 38 |
if (rel->rd_rel->relpersistence != RELPERSISTENCE_UNLOGGED || |
| 39 |
!RecoveryInProgress()) |
| 40 |
return true; |
| 41 |
|
| 42 |
ereport(NOTICE, |
| 43 |
(errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION), |
| 44 |
errmsg("cannot verify unlogged index \"%s\" during recovery, skipping", |
| 45 |
RelationGetRelationName(rel)))); |
| 46 |
|
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* Amcheck main workhorse. |
| 52 |
* Given index relation OID, lock relation. |
| 53 |
* Next, take a number of standard actions: |
| 54 |
* 1) Make sure the index can be checked |
| 55 |
* 2) change the context of the user, |
| 56 |
* 3) keep track of GUCs modified via index functions |
| 57 |
* 4) execute callback function to verify integrity. |
| 58 |
*/ |
| 59 |
void |
| 60 |
amcheck_lock_relation_and_check(Oid indrelid, |
| 61 |
Oid am_id, |
| 62 |
IndexDoCheckCallback check, |
| 63 |
LOCKMODE lockmode, |
| 64 |
void *state) |
| 65 |
{ |
| 66 |
Oid heapid; |
| 67 |
Relation indrel; |
| 68 |
Relation heaprel; |
| 69 |
Oid save_userid; |
| 70 |
int save_sec_context; |
| 71 |
int save_nestlevel; |
| 72 |
|
| 73 |
/* |
| 74 |
* We must lock table before index to avoid deadlocks. However, if the |
| 75 |
* passed indrelid isn't an index then IndexGetRelation() will fail. |
| 76 |
* Rather than emitting a not-very-helpful error message, postpone |
| 77 |
* complaining, expecting that the is-it-an-index test below will fail. |
| 78 |
* |
| 79 |
* In hot standby mode this will raise an error when parentcheck is true. |
| 80 |
*/ |
| 81 |
heapid = IndexGetRelation(indrelid, true); |
| 82 |
if (OidIsValid(heapid)) |
| 83 |
{ |
| 84 |
heaprel = table_open(heapid, lockmode); |
| 85 |
|
| 86 |
/* |
| 87 |
* Switch to the table owner's userid, so that any index functions are |
| 88 |
* run as that user. Also lock down security-restricted operations |
| 89 |
* and arrange to make GUC variable changes local to this command. |
| 90 |
*/ |
| 91 |
GetUserIdAndSecContext(&save_userid, &save_sec_context); |
| 92 |
SetUserIdAndSecContext(heaprel->rd_rel->relowner, |
| 93 |
save_sec_context | SECURITY_RESTRICTED_OPERATION); |
| 94 |
save_nestlevel = NewGUCNestLevel(); |
| 95 |
RestrictSearchPath(); |
| 96 |
} |
| 97 |
else |
| 98 |
{ |
| 99 |
heaprel = NULL; |
| 100 |
/* Set these just to suppress "uninitialized variable" warnings */ |
| 101 |
save_userid = InvalidOid; |
| 102 |
save_sec_context = -1; |
| 103 |
save_nestlevel = -1; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* Open the target index relations separately (like relation_openrv(), but |
| 108 |
* with heap relation locked first to prevent deadlocking). In hot |
| 109 |
* standby mode this will raise an error when parentcheck is true. |
| 110 |
* |
| 111 |
* There is no need for the usual indcheckxmin usability horizon test |
| 112 |
* here, even in the heapallindexed case, because index undergoing |
| 113 |
* verification only needs to have entries for a new transaction snapshot. |
| 114 |
* (If this is a parentcheck verification, there is no question about |
| 115 |
* committed or recently dead heap tuples lacking index entries due to |
| 116 |
* concurrent activity.) |
| 117 |
*/ |
| 118 |
indrel = index_open(indrelid, lockmode); |
| 119 |
|
| 120 |
/* |
| 121 |
* Since we did the IndexGetRelation call above without any lock, it's |
| 122 |
* barely possible that a race against an index drop/recreation could have |
| 123 |
* netted us the wrong table. |
| 124 |
*/ |
| 125 |
if (heaprel == NULL || heapid != IndexGetRelation(indrelid, false)) |
| 126 |
ereport(ERROR, |
| 127 |
(errcode(ERRCODE_UNDEFINED_TABLE), |
| 128 |
errmsg("could not open parent table of index \"%s\"", |
| 129 |
RelationGetRelationName(indrel)))); |
| 130 |
|
| 131 |
/* Check that relation suitable for checking */ |
| 132 |
if (index_checkable(indrel, am_id)) |
| 133 |
check(indrel, heaprel, state, lockmode == ShareLock); |
| 134 |
|
| 135 |
/* Roll back any GUC changes executed by index functions */ |
| 136 |
AtEOXact_GUC(false, save_nestlevel); |
| 137 |
|
| 138 |
/* Restore userid and security context */ |
| 139 |
SetUserIdAndSecContext(save_userid, save_sec_context); |
| 140 |
|
| 141 |
/* |
| 142 |
* Release locks early. That's ok here because nothing in the called |
| 143 |
* routines will trigger shared cache invalidations to be sent, so we can |
| 144 |
* relax the usual pattern of only releasing locks after commit. |
| 145 |
*/ |
| 146 |
index_close(indrel, lockmode); |
| 147 |
if (heaprel) |
| 148 |
table_close(heaprel, lockmode); |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* Basic checks about the suitability of a relation for checking as an index. |
| 153 |
* |
| 154 |
* |
| 155 |
* NB: Intentionally not checking permissions, the function is normally not |
| 156 |
* callable by non-superusers. If granted, it's useful to be able to check a |
| 157 |
* whole cluster. |
| 158 |
*/ |
| 159 |
bool |
| 160 |
index_checkable(Relation rel, Oid am_id) |
| 161 |
{ |
| 162 |
if (rel->rd_rel->relkind != RELKIND_INDEX || |
| 163 |
rel->rd_rel->relam != am_id) |
| 164 |
{ |
| 165 |
HeapTuple amtup; |
| 166 |
HeapTuple amtuprel; |
| 167 |
|
| 168 |
amtup = SearchSysCache1(AMOID, ObjectIdGetDatum(am_id)); |
| 169 |
amtuprel = SearchSysCache1(AMOID, ObjectIdGetDatum(rel->rd_rel->relam)); |
| 170 |
ereport(ERROR, |
| 171 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 172 |
errmsg("expected \"%s\" index as targets for verification", NameStr(((Form_pg_am) GETSTRUCT(amtup))->amname)), |
| 173 |
errdetail("Relation \"%s\" is a %s index.", |
| 174 |
RelationGetRelationName(rel), NameStr(((Form_pg_am) GETSTRUCT(amtuprel))->amname)))); |
| 175 |
} |
| 176 |
|
| 177 |
if (RELATION_IS_OTHER_TEMP(rel)) |
| 178 |
ereport(ERROR, |
| 179 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 180 |
errmsg("cannot access temporary tables of other sessions"), |
| 181 |
errdetail("Index \"%s\" is associated with temporary relation.", |
| 182 |
RelationGetRelationName(rel)))); |
| 183 |
|
| 184 |
if (!rel->rd_index->indisvalid) |
| 185 |
ereport(ERROR, |
| 186 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 187 |
errmsg("cannot check index \"%s\"", |
| 188 |
RelationGetRelationName(rel)), |
| 189 |
errdetail("Index is not valid."))); |
| 190 |
|
| 191 |
return amcheck_index_mainfork_expected(rel); |
| 192 |
} |