|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pagefuncs.c |
| 4 | + * Functions for features related to relation pages. |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * |
| 10 | + * IDENTIFICATION |
| 11 | + * src/backend/utils/adt/pagefuncs.c |
| 12 | + * |
| 13 | + *------------------------------------------------------------------------- |
| 14 | + */ |
| 15 | + |
| 16 | +#include "postgres.h" |
| 17 | + |
| 18 | +#include "access/relation.h" |
| 19 | +#include "funcapi.h" |
| 20 | +#include "miscadmin.h" |
| 21 | +#include "storage/bufmgr.h" |
| 22 | +#include "storage/lmgr.h" |
| 23 | +#include "storage/smgr.h" |
| 24 | +#include "utils/builtins.h" |
| 25 | +#include "utils/syscache.h" |
| 26 | + |
| 27 | +static void check_one_relation(TupleDesc tupdesc, Tuplestorestate *tupstore, |
| 28 | + Oid relid, ForkNumber single_forknum); |
| 29 | +static void check_relation_fork(TupleDesc tupdesc, Tuplestorestate *tupstore, |
| 30 | + Relation relation, ForkNumber forknum); |
| 31 | + |
| 32 | +/* |
| 33 | + * callback arguments for check_pages_error_callback() |
| 34 | + */ |
| 35 | +typedef struct CheckPagesErrorInfo |
| 36 | +{ |
| 37 | + char *path; |
| 38 | + BlockNumber blkno; |
| 39 | +} CheckPagesErrorInfo; |
| 40 | + |
| 41 | +/* |
| 42 | + * Error callback specific to check_relation_fork(). |
| 43 | + */ |
| 44 | +static void |
| 45 | +check_pages_error_callback(void *arg) |
| 46 | +{ |
| 47 | + CheckPagesErrorInfo *errinfo = (CheckPagesErrorInfo *) arg; |
| 48 | + |
| 49 | + errcontext("while checking page %u of path %s", |
| 50 | + errinfo->blkno, errinfo->path); |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | + * pg_relation_check_pages |
| 55 | + * |
| 56 | + * Check the state of all the pages for one or more fork types in the given |
| 57 | + * relation. |
| 58 | + */ |
| 59 | +Datum |
| 60 | +pg_relation_check_pages(PG_FUNCTION_ARGS) |
| 61 | +{ |
| 62 | + Oid relid; |
| 63 | + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 64 | + TupleDesc tupdesc; |
| 65 | + Tuplestorestate *tupstore; |
| 66 | + MemoryContext per_query_ctx; |
| 67 | + MemoryContext oldcontext; |
| 68 | + ForkNumber forknum; |
| 69 | + |
| 70 | + /* Switch into long-lived context to construct returned data structures */ |
| 71 | + per_query_ctx = rsinfo->econtext->ecxt_per_query_memory; |
| 72 | + oldcontext = MemoryContextSwitchTo(per_query_ctx); |
| 73 | + |
| 74 | + /* Build a tuple descriptor for our result type */ |
| 75 | + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) |
| 76 | + elog(ERROR, "return type must be a row type"); |
| 77 | + |
| 78 | + tupstore = tuplestore_begin_heap(true, false, work_mem); |
| 79 | + rsinfo->returnMode = SFRM_Materialize; |
| 80 | + rsinfo->setResult = tupstore; |
| 81 | + rsinfo->setDesc = tupdesc; |
| 82 | + |
| 83 | + MemoryContextSwitchTo(oldcontext); |
| 84 | + |
| 85 | + /* handle arguments */ |
| 86 | + if (PG_ARGISNULL(0)) |
| 87 | + { |
| 88 | + /* Just leave if nothing is defined */ |
| 89 | + PG_RETURN_VOID(); |
| 90 | + } |
| 91 | + |
| 92 | + /* By default all the forks of a relation are checked */ |
| 93 | + if (PG_ARGISNULL(1)) |
| 94 | + forknum = InvalidForkNumber; |
| 95 | + else |
| 96 | + { |
| 97 | + const char *forkname = TextDatumGetCString(PG_GETARG_TEXT_PP(1)); |
| 98 | + |
| 99 | + forknum = forkname_to_number(forkname); |
| 100 | + } |
| 101 | + |
| 102 | + relid = PG_GETARG_OID(0); |
| 103 | + |
| 104 | + check_one_relation(tupdesc, tupstore, relid, forknum); |
| 105 | + tuplestore_donestoring(tupstore); |
| 106 | + |
| 107 | + return (Datum) 0; |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | + * Perform the check on a single relation, possibly filtered with a single |
| 112 | + * fork. This function will check if the given relation exists or not, as |
| 113 | + * a relation could be dropped after checking for the list of relations and |
| 114 | + * before getting here, and we don't want to error out in this case. |
| 115 | + */ |
| 116 | +static void |
| 117 | +check_one_relation(TupleDesc tupdesc, Tuplestorestate *tupstore, |
| 118 | + Oid relid, ForkNumber single_forknum) |
| 119 | +{ |
| 120 | + Relation relation; |
| 121 | + ForkNumber forknum; |
| 122 | + |
| 123 | + /* Check if relation exists. leaving if there is no such relation */ |
| 124 | + if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relid))) |
| 125 | + return; |
| 126 | + |
| 127 | + relation = relation_open(relid, AccessShareLock); |
| 128 | + |
| 129 | + /* |
| 130 | + * Sanity checks, returning no results if not supported. Temporary |
| 131 | + * relations and relations without storage are out of scope. |
| 132 | + */ |
| 133 | + if (!RELKIND_HAS_STORAGE(relation->rd_rel->relkind) || |
| 134 | + relation->rd_rel->relpersistence == RELPERSISTENCE_TEMP) |
| 135 | + { |
| 136 | + relation_close(relation, AccessShareLock); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + RelationOpenSmgr(relation); |
| 141 | + |
| 142 | + for (forknum = 0; forknum <= MAX_FORKNUM; forknum++) |
| 143 | + { |
| 144 | + if (single_forknum != InvalidForkNumber && single_forknum != forknum) |
| 145 | + continue; |
| 146 | + |
| 147 | + if (smgrexists(relation->rd_smgr, forknum)) |
| 148 | + check_relation_fork(tupdesc, tupstore, relation, forknum); |
| 149 | + } |
| 150 | + |
| 151 | + relation_close(relation, AccessShareLock); |
| 152 | +} |
| 153 | + |
| 154 | +/* |
| 155 | + * For a given relation and fork, do the real work of iterating over all pages |
| 156 | + * and doing the check. Caller must hold an AccessShareLock lock on the given |
| 157 | + * relation. |
| 158 | + */ |
| 159 | +static void |
| 160 | +check_relation_fork(TupleDesc tupdesc, Tuplestorestate *tupstore, |
| 161 | + Relation relation, ForkNumber forknum) |
| 162 | +{ |
| 163 | + BlockNumber blkno, |
| 164 | + nblocks; |
| 165 | + SMgrRelation smgr = relation->rd_smgr; |
| 166 | + char *path; |
| 167 | + CheckPagesErrorInfo errinfo; |
| 168 | + ErrorContextCallback errcallback; |
| 169 | + |
| 170 | + /* Number of output arguments in the SRF */ |
| 171 | +#define PG_CHECK_RELATION_COLS 2 |
| 172 | + |
| 173 | + Assert(CheckRelationLockedByMe(relation, AccessShareLock, true)); |
| 174 | + |
| 175 | + /* |
| 176 | + * We remember the number of blocks here. Since caller must hold a lock |
| 177 | + * on the relation, we know that it won't be truncated while we are |
| 178 | + * iterating over the blocks. Any block added after this function started |
| 179 | + * will not be checked. |
| 180 | + */ |
| 181 | + nblocks = RelationGetNumberOfBlocksInFork(relation, forknum); |
| 182 | + |
| 183 | + path = relpathbackend(smgr->smgr_rnode.node, |
| 184 | + smgr->smgr_rnode.backend, |
| 185 | + forknum); |
| 186 | + |
| 187 | + /* |
| 188 | + * Error context to print some information about blocks and relations |
| 189 | + * impacted by corruptions. |
| 190 | + */ |
| 191 | + errinfo.path = pstrdup(path); |
| 192 | + errinfo.blkno = 0; |
| 193 | + errcallback.callback = check_pages_error_callback; |
| 194 | + errcallback.arg = (void *) &errinfo; |
| 195 | + errcallback.previous = error_context_stack; |
| 196 | + error_context_stack = &errcallback; |
| 197 | + |
| 198 | + for (blkno = 0; blkno < nblocks; blkno++) |
| 199 | + { |
| 200 | + Datum values[PG_CHECK_RELATION_COLS]; |
| 201 | + bool nulls[PG_CHECK_RELATION_COLS]; |
| 202 | + int i = 0; |
| 203 | + |
| 204 | + /* Update block number for the error context */ |
| 205 | + errinfo.blkno = blkno; |
| 206 | + |
| 207 | + CHECK_FOR_INTERRUPTS(); |
| 208 | + |
| 209 | + /* Check the given buffer */ |
| 210 | + if (CheckBuffer(smgr, forknum, blkno)) |
| 211 | + continue; |
| 212 | + |
| 213 | + memset(values, 0, sizeof(values)); |
| 214 | + memset(nulls, 0, sizeof(nulls)); |
| 215 | + |
| 216 | + values[i++] = CStringGetTextDatum(path); |
| 217 | + values[i++] = UInt32GetDatum(blkno); |
| 218 | + |
| 219 | + Assert(i == PG_CHECK_RELATION_COLS); |
| 220 | + |
| 221 | + /* Save the corrupted blocks in the tuplestore. */ |
| 222 | + tuplestore_putvalues(tupstore, tupdesc, values, nulls); |
| 223 | + |
| 224 | + pfree(path); |
| 225 | + } |
| 226 | + |
| 227 | + /* Pop the error context stack */ |
| 228 | + error_context_stack = errcallback.previous; |
| 229 | +} |
0 commit comments