|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * sequenceamapi.c |
| 4 | + * general sequence access method routines |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * |
| 10 | + * IDENTIFICATION |
| 11 | + * src/backend/access/sequence/sequenceamapi.c |
| 12 | + * |
| 13 | + * |
| 14 | + * Sequence access method allows the SQL Standard Sequence objects to be |
| 15 | + * managed according to either the default access method or a pluggable |
| 16 | + * replacement. Each sequence can only use one access method at a time, |
| 17 | + * though different sequence access methods can be in use by different |
| 18 | + * sequences at the same time. |
| 19 | + * |
| 20 | + * ------------------------------------------------------------------------- |
| 21 | + */ |
| 22 | + |
| 23 | +#include "postgres.h" |
| 24 | + |
| 25 | +#include "access/htup_details.h" |
| 26 | +#include "access/sequenceam.h" |
| 27 | +#include "access/xact.h" |
| 28 | +#include "catalog/pg_am.h" |
| 29 | +#include "commands/defrem.h" |
| 30 | +#include "miscadmin.h" |
| 31 | +#include "utils/guc_hooks.h" |
| 32 | +#include "utils/syscache.h" |
| 33 | + |
| 34 | + |
| 35 | +/* GUC */ |
| 36 | +char *default_sequence_access_method = DEFAULT_SEQUENCE_ACCESS_METHOD; |
| 37 | + |
| 38 | +/* |
| 39 | + * GetSequenceAmRoutine |
| 40 | + * Call the specified access method handler routine to get its |
| 41 | + * SequenceAmRoutine struct, which will be palloc'd in the caller's |
| 42 | + * memory context. |
| 43 | + */ |
| 44 | +const SequenceAmRoutine * |
| 45 | +GetSequenceAmRoutine(Oid amhandler) |
| 46 | +{ |
| 47 | + Datum datum; |
| 48 | + SequenceAmRoutine *routine; |
| 49 | + |
| 50 | + datum = OidFunctionCall0(amhandler); |
| 51 | + routine = (SequenceAmRoutine *) DatumGetPointer(datum); |
| 52 | + |
| 53 | + if (routine == NULL || !IsA(routine, SequenceAmRoutine)) |
| 54 | + elog(ERROR, "sequence access method handler %u did not return a SequenceAmRoutine struct", |
| 55 | + amhandler); |
| 56 | + |
| 57 | + /* |
| 58 | + * Assert that all required callbacks are present. That makes it a bit |
| 59 | + * easier to keep AMs up to date, e.g. when forward porting them to a new |
| 60 | + * major version. |
| 61 | + */ |
| 62 | + Assert(routine->get_table_am != NULL); |
| 63 | + Assert(routine->init != NULL); |
| 64 | + Assert(routine->nextval != NULL); |
| 65 | + Assert(routine->setval != NULL); |
| 66 | + Assert(routine->reset != NULL); |
| 67 | + Assert(routine->get_state != NULL); |
| 68 | + Assert(routine->change_persistence != NULL); |
| 69 | + |
| 70 | + return routine; |
| 71 | +} |
| 72 | + |
| 73 | +/* |
| 74 | + * GetSequenceAmRoutineId |
| 75 | + * Call pg_am and retrieve the OID of the access method handler. |
| 76 | + */ |
| 77 | +Oid |
| 78 | +GetSequenceAmRoutineId(Oid amoid) |
| 79 | +{ |
| 80 | + Oid amhandleroid; |
| 81 | + HeapTuple tuple; |
| 82 | + Form_pg_am aform; |
| 83 | + |
| 84 | + tuple = SearchSysCache1(AMOID, |
| 85 | + ObjectIdGetDatum(amoid)); |
| 86 | + if (!HeapTupleIsValid(tuple)) |
| 87 | + elog(ERROR, "cache lookup failed for access method %u", amoid); |
| 88 | + aform = (Form_pg_am) GETSTRUCT(tuple); |
| 89 | + Assert(aform->amtype == AMTYPE_SEQUENCE); |
| 90 | + amhandleroid = aform->amhandler; |
| 91 | + ReleaseSysCache(tuple); |
| 92 | + |
| 93 | + return amhandleroid; |
| 94 | +} |
| 95 | + |
| 96 | +/* check_hook: validate new default_sequence_access_method */ |
| 97 | +bool |
| 98 | +check_default_sequence_access_method(char **newval, void **extra, |
| 99 | + GucSource source) |
| 100 | +{ |
| 101 | + if (**newval == '\0') |
| 102 | + { |
| 103 | + GUC_check_errdetail("%s cannot be empty.", |
| 104 | + "default_sequence_access_method"); |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if (strlen(*newval) >= NAMEDATALEN) |
| 109 | + { |
| 110 | + GUC_check_errdetail("%s is too long (maximum %d characters).", |
| 111 | + "default_sequence_access_method", NAMEDATALEN - 1); |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * If we aren't inside a transaction, or not connected to a database, we |
| 117 | + * cannot do the catalog access necessary to verify the method. Must |
| 118 | + * accept the value on faith. |
| 119 | + */ |
| 120 | + if (IsTransactionState() && MyDatabaseId != InvalidOid) |
| 121 | + { |
| 122 | + if (!OidIsValid(get_sequence_am_oid(*newval, true))) |
| 123 | + { |
| 124 | + /* |
| 125 | + * When source == PGC_S_TEST, don't throw a hard error for a |
| 126 | + * nonexistent sequence access method, only a NOTICE. See comments |
| 127 | + * in guc.h. |
| 128 | + */ |
| 129 | + if (source == PGC_S_TEST) |
| 130 | + { |
| 131 | + ereport(NOTICE, |
| 132 | + (errcode(ERRCODE_UNDEFINED_OBJECT), |
| 133 | + errmsg("sequence access method \"%s\" does not exist", |
| 134 | + *newval))); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + GUC_check_errdetail("sequence access method \"%s\" does not exist.", |
| 139 | + *newval); |
| 140 | + return false; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | +} |
0 commit comments