|
| 1 | +/*---------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * tableamapi.c |
| 4 | + * Support routines for API for Postgres table access methods |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * src/backend/access/table/tableamapi.c |
| 10 | + *---------------------------------------------------------------------- |
| 11 | + */ |
| 12 | +#include "postgres.h" |
| 13 | + |
| 14 | +#include "access/heapam.h" |
| 15 | +#include "access/htup_details.h" |
| 16 | +#include "access/tableam.h" |
| 17 | +#include "access/xact.h" |
| 18 | +#include "catalog/pg_am.h" |
| 19 | +#include "catalog/pg_proc.h" |
| 20 | +#include "utils/fmgroids.h" |
| 21 | +#include "utils/memutils.h" |
| 22 | +#include "utils/syscache.h" |
| 23 | + |
| 24 | + |
| 25 | +static Oid get_table_am_oid(const char *tableamname, bool missing_ok); |
| 26 | + |
| 27 | + |
| 28 | +/* |
| 29 | + * GetTableAmRoutine |
| 30 | + * Call the specified access method handler routine to get its |
| 31 | + * TableAmRoutine struct, which will be palloc'd in the caller's |
| 32 | + * memory context. |
| 33 | + */ |
| 34 | +const TableAmRoutine * |
| 35 | +GetTableAmRoutine(Oid amhandler) |
| 36 | +{ |
| 37 | + Datum datum; |
| 38 | + const TableAmRoutine *routine; |
| 39 | + |
| 40 | + datum = OidFunctionCall0(amhandler); |
| 41 | + routine = (TableAmRoutine *) DatumGetPointer(datum); |
| 42 | + |
| 43 | + if (routine == NULL || !IsA(routine, TableAmRoutine)) |
| 44 | + elog(ERROR, "Table access method handler %u did not return a TableAmRoutine struct", |
| 45 | + amhandler); |
| 46 | + |
| 47 | + return routine; |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * GetTableAmRoutineByAmId - look up the handler of the table access |
| 52 | + * method with the given OID, and get its TableAmRoutine struct. |
| 53 | + */ |
| 54 | +const TableAmRoutine * |
| 55 | +GetTableAmRoutineByAmId(Oid amoid) |
| 56 | +{ |
| 57 | + regproc amhandler; |
| 58 | + HeapTuple tuple; |
| 59 | + Form_pg_am amform; |
| 60 | + |
| 61 | + /* Get handler function OID for the access method */ |
| 62 | + tuple = SearchSysCache1(AMOID, ObjectIdGetDatum(amoid)); |
| 63 | + if (!HeapTupleIsValid(tuple)) |
| 64 | + elog(ERROR, "cache lookup failed for access method %u", |
| 65 | + amoid); |
| 66 | + amform = (Form_pg_am) GETSTRUCT(tuple); |
| 67 | + |
| 68 | + /* Check that it is a table access method */ |
| 69 | + if (amform->amtype != AMTYPE_TABLE) |
| 70 | + ereport(ERROR, |
| 71 | + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 72 | + errmsg("access method \"%s\" is not of type %s", |
| 73 | + NameStr(amform->amname), "TABLE"))); |
| 74 | + |
| 75 | + amhandler = amform->amhandler; |
| 76 | + |
| 77 | + /* Complain if handler OID is invalid */ |
| 78 | + if (!RegProcedureIsValid(amhandler)) |
| 79 | + ereport(ERROR, |
| 80 | + (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
| 81 | + errmsg("table access method \"%s\" does not have a handler", |
| 82 | + NameStr(amform->amname)))); |
| 83 | + |
| 84 | + ReleaseSysCache(tuple); |
| 85 | + |
| 86 | + /* And finally, call the handler function to get the API struct. */ |
| 87 | + return GetTableAmRoutine(amhandler); |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * get_table_am_oid - given a table access method name, look up the OID |
| 92 | + * |
| 93 | + * If missing_ok is false, throw an error if table access method name not |
| 94 | + * found. If true, just return InvalidOid. |
| 95 | + */ |
| 96 | +static Oid |
| 97 | +get_table_am_oid(const char *tableamname, bool missing_ok) |
| 98 | +{ |
| 99 | + Oid result; |
| 100 | + Relation rel; |
| 101 | + HeapScanDesc scandesc; |
| 102 | + HeapTuple tuple; |
| 103 | + ScanKeyData entry[1]; |
| 104 | + |
| 105 | + /* |
| 106 | + * Search pg_tablespace. We use a heapscan here even though there is an |
| 107 | + * index on name, on the theory that pg_tablespace will usually have just |
| 108 | + * a few entries and so an indexed lookup is a waste of effort. |
| 109 | + */ |
| 110 | + rel = heap_open(AccessMethodRelationId, AccessShareLock); |
| 111 | + |
| 112 | + ScanKeyInit(&entry[0], |
| 113 | + Anum_pg_am_amname, |
| 114 | + BTEqualStrategyNumber, F_NAMEEQ, |
| 115 | + CStringGetDatum(tableamname)); |
| 116 | + scandesc = heap_beginscan_catalog(rel, 1, entry); |
| 117 | + tuple = heap_getnext(scandesc, ForwardScanDirection); |
| 118 | + |
| 119 | + /* We assume that there can be at most one matching tuple */ |
| 120 | + if (HeapTupleIsValid(tuple) && |
| 121 | + ((Form_pg_am) GETSTRUCT(tuple))->amtype == AMTYPE_TABLE) |
| 122 | + result = ((Form_pg_am) GETSTRUCT(tuple))->oid; |
| 123 | + else |
| 124 | + result = InvalidOid; |
| 125 | + |
| 126 | + heap_endscan(scandesc); |
| 127 | + heap_close(rel, AccessShareLock); |
| 128 | + |
| 129 | + if (!OidIsValid(result) && !missing_ok) |
| 130 | + ereport(ERROR, |
| 131 | + (errcode(ERRCODE_UNDEFINED_OBJECT), |
| 132 | + errmsg("table access method \"%s\" does not exist", |
| 133 | + tableamname))); |
| 134 | + |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +/* check_hook: validate new default_table_access_method */ |
| 139 | +bool |
| 140 | +check_default_table_access_method(char **newval, void **extra, GucSource source) |
| 141 | +{ |
| 142 | + /* |
| 143 | + * If we aren't inside a transaction, we cannot do database access so |
| 144 | + * cannot verify the name. Must accept the value on faith. |
| 145 | + */ |
| 146 | + if (IsTransactionState()) |
| 147 | + { |
| 148 | + if (**newval != '\0' && |
| 149 | + !OidIsValid(get_table_am_oid(*newval, true))) |
| 150 | + { |
| 151 | + /* |
| 152 | + * When source == PGC_S_TEST, don't throw a hard error for a |
| 153 | + * nonexistent table access method, only a NOTICE. See comments in |
| 154 | + * guc.h. |
| 155 | + */ |
| 156 | + if (source == PGC_S_TEST) |
| 157 | + { |
| 158 | + ereport(NOTICE, |
| 159 | + (errcode(ERRCODE_UNDEFINED_OBJECT), |
| 160 | + errmsg("Table access method \"%s\" does not exist", |
| 161 | + *newval))); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + GUC_check_errdetail("Table access method \"%s\" does not exist.", |
| 166 | + *newval); |
| 167 | + return false; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return true; |
| 173 | +} |
0 commit comments