PostgreSQL Source Code git master
extension.h File Reference
Include dependency graph for extension.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ObjectAddress CreateExtension (ParseState *pstate, CreateExtensionStmt *stmt)
 
void RemoveExtensionById (Oid extId)
 
ObjectAddress InsertExtensionTuple (const char *extName, Oid extOwner, Oid schemaOid, bool relocatable, const char *extVersion, Datum extConfig, Datum extCondition, List *requiredExtensions)
 
ObjectAddress ExecAlterExtensionStmt (ParseState *pstate, AlterExtensionStmt *stmt)
 
ObjectAddress ExecAlterExtensionContentsStmt (AlterExtensionContentsStmt *stmt, ObjectAddress *objAddr)
 
Oid get_extension_oid (const char *extname, bool missing_ok)
 
char * get_extension_name (Oid ext_oid)
 
Oid get_extension_schema (Oid ext_oid)
 
bool extension_file_exists (const char *extensionName)
 
ObjectAddress AlterExtensionNamespace (const char *extensionName, const char *newschema, Oid *oldschema)
 

Variables

PGDLLIMPORT char * Extension_control_path
 
PGDLLIMPORT bool creating_extension
 
PGDLLIMPORT Oid CurrentExtensionObject
 

Function Documentation

◆ AlterExtensionNamespace()

ObjectAddress AlterExtensionNamespace ( const char *  extensionName,
const char *  newschema,
Oid oldschema 
)

Definition at line 3042 of file extension.c.

3043{
3044 Oid extensionOid;
3045 Oid nspOid;
3046 Oid oldNspOid;
3047 AclResult aclresult;
3048 Relation extRel;
3049 ScanKeyData key[2];
3050 SysScanDesc extScan;
3051 HeapTuple extTup;
3052 Form_pg_extension extForm;
3053 Relation depRel;
3054 SysScanDesc depScan;
3055 HeapTuple depTup;
3056 ObjectAddresses *objsMoved;
3057 ObjectAddress extAddr;
3058
3059 extensionOid = get_extension_oid(extensionName, false);
3060
3061 nspOid = LookupCreationNamespace(newschema);
3062
3063 /*
3064 * Permission check: must own extension. Note that we don't bother to
3065 * check ownership of the individual member objects ...
3066 */
3067 if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
3069 extensionName);
3070
3071 /* Permission check: must have creation rights in target namespace */
3072 aclresult = object_aclcheck(NamespaceRelationId, nspOid, GetUserId(), ACL_CREATE);
3073 if (aclresult != ACLCHECK_OK)
3074 aclcheck_error(aclresult, OBJECT_SCHEMA, newschema);
3075
3076 /*
3077 * If the schema is currently a member of the extension, disallow moving
3078 * the extension into the schema. That would create a dependency loop.
3079 */
3080 if (getExtensionOfObject(NamespaceRelationId, nspOid) == extensionOid)
3081 ereport(ERROR,
3082 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3083 errmsg("cannot move extension \"%s\" into schema \"%s\" "
3084 "because the extension contains the schema",
3085 extensionName, newschema)));
3086
3087 /* Locate the pg_extension tuple */
3088 extRel = table_open(ExtensionRelationId, RowExclusiveLock);
3089
3090 ScanKeyInit(&key[0],
3091 Anum_pg_extension_oid,
3092 BTEqualStrategyNumber, F_OIDEQ,
3093 ObjectIdGetDatum(extensionOid));
3094
3095 extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
3096 NULL, 1, key);
3097
3098 extTup = systable_getnext(extScan);
3099
3100 if (!HeapTupleIsValid(extTup)) /* should not happen */
3101 elog(ERROR, "could not find tuple for extension %u",
3102 extensionOid);
3103
3104 /* Copy tuple so we can modify it below */
3105 extTup = heap_copytuple(extTup);
3106 extForm = (Form_pg_extension) GETSTRUCT(extTup);
3107
3108 systable_endscan(extScan);
3109
3110 /*
3111 * If the extension is already in the target schema, just silently do
3112 * nothing.
3113 */
3114 if (extForm->extnamespace == nspOid)
3115 {
3117 return InvalidObjectAddress;
3118 }
3119
3120 /* Check extension is supposed to be relocatable */
3121 if (!extForm->extrelocatable)
3122 ereport(ERROR,
3123 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3124 errmsg("extension \"%s\" does not support SET SCHEMA",
3125 NameStr(extForm->extname))));
3126
3127 objsMoved = new_object_addresses();
3128
3129 /* store the OID of the namespace to-be-changed */
3130 oldNspOid = extForm->extnamespace;
3131
3132 /*
3133 * Scan pg_depend to find objects that depend directly on the extension,
3134 * and alter each one's schema.
3135 */
3136 depRel = table_open(DependRelationId, AccessShareLock);
3137
3138 ScanKeyInit(&key[0],
3139 Anum_pg_depend_refclassid,
3140 BTEqualStrategyNumber, F_OIDEQ,
3141 ObjectIdGetDatum(ExtensionRelationId));
3142 ScanKeyInit(&key[1],
3143 Anum_pg_depend_refobjid,
3144 BTEqualStrategyNumber, F_OIDEQ,
3145 ObjectIdGetDatum(extensionOid));
3146
3147 depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
3148 NULL, 2, key);
3149
3150 while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
3151 {
3152 Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
3153 ObjectAddress dep;
3154 Oid dep_oldNspOid;
3155
3156 /*
3157 * If a dependent extension has a no_relocate request for this
3158 * extension, disallow SET SCHEMA. (XXX it's a bit ugly to do this in
3159 * the same loop that's actually executing the renames: we may detect
3160 * the error condition only after having expended a fair amount of
3161 * work. However, the alternative is to do two scans of pg_depend,
3162 * which seems like optimizing for failure cases. The rename work
3163 * will all roll back cleanly enough if we do fail here.)
3164 */
3165 if (pg_depend->deptype == DEPENDENCY_NORMAL &&
3166 pg_depend->classid == ExtensionRelationId)
3167 {
3168 char *depextname = get_extension_name(pg_depend->objid);
3169 ExtensionControlFile *dcontrol;
3170 ListCell *lc;
3171
3172 dcontrol = read_extension_control_file(depextname);
3173 foreach(lc, dcontrol->no_relocate)
3174 {
3175 char *nrextname = (char *) lfirst(lc);
3176
3177 if (strcmp(nrextname, NameStr(extForm->extname)) == 0)
3178 {
3179 ereport(ERROR,
3180 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3181 errmsg("cannot SET SCHEMA of extension \"%s\" because other extensions prevent it",
3182 NameStr(extForm->extname)),
3183 errdetail("Extension \"%s\" requests no relocation of extension \"%s\".",
3184 depextname,
3185 NameStr(extForm->extname))));
3186 }
3187 }
3188 }
3189
3190 /*
3191 * Otherwise, ignore non-membership dependencies. (Currently, the
3192 * only other case we could see here is a normal dependency from
3193 * another extension.)
3194 */
3195 if (pg_depend->deptype != DEPENDENCY_EXTENSION)
3196 continue;
3197
3198 dep.classId = pg_depend->classid;
3199 dep.objectId = pg_depend->objid;
3200 dep.objectSubId = pg_depend->objsubid;
3201
3202 if (dep.objectSubId != 0) /* should not happen */
3203 elog(ERROR, "extension should not have a sub-object dependency");
3204
3205 /* Relocate the object */
3206 dep_oldNspOid = AlterObjectNamespace_oid(dep.classId,
3207 dep.objectId,
3208 nspOid,
3209 objsMoved);
3210
3211 /*
3212 * If not all the objects had the same old namespace (ignoring any
3213 * that are not in namespaces or are dependent types), complain.
3214 */
3215 if (dep_oldNspOid != InvalidOid && dep_oldNspOid != oldNspOid)
3216 ereport(ERROR,
3217 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3218 errmsg("extension \"%s\" does not support SET SCHEMA",
3219 NameStr(extForm->extname)),
3220 errdetail("%s is not in the extension's schema \"%s\"",
3221 getObjectDescription(&dep, false),
3222 get_namespace_name(oldNspOid))));
3223 }
3224
3225 /* report old schema, if caller wants it */
3226 if (oldschema)
3227 *oldschema = oldNspOid;
3228
3229 systable_endscan(depScan);
3230
3232
3233 /* Now adjust pg_extension.extnamespace */
3234 extForm->extnamespace = nspOid;
3235
3236 CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
3237
3239
3240 /* update dependency to point to the new schema */
3241 if (changeDependencyFor(ExtensionRelationId, extensionOid,
3242 NamespaceRelationId, oldNspOid, nspOid) != 1)
3243 elog(ERROR, "could not change schema dependency for extension %s",
3244 NameStr(extForm->extname));
3245
3246 InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
3247
3248 ObjectAddressSet(extAddr, ExtensionRelationId, extensionOid);
3249
3250 return extAddr;
3251}
AclResult
Definition: acl.h:182
@ ACLCHECK_OK
Definition: acl.h:183
@ ACLCHECK_NOT_OWNER
Definition: acl.h:185
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:2639
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3821
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4075
Oid AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: alter.c:625
#define NameStr(name)
Definition: c.h:717
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2502
@ DEPENDENCY_EXTENSION
Definition: dependency.h:38
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
static ExtensionControlFile * read_extension_control_file(const char *extname)
Definition: extension.c:700
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:167
char * get_extension_name(Oid ext_oid)
Definition: extension.c:189
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:388
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:778
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:313
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3506
Oid GetUserId(void)
Definition: miscinit.c:520
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:3428
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:197
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
const ObjectAddress InvalidObjectAddress
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
@ OBJECT_SCHEMA
Definition: parsenodes.h:2353
@ OBJECT_EXTENSION
Definition: parsenodes.h:2332
#define ACL_CREATE
Definition: parsenodes.h:85
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:457
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:732
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:72
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:52
#define lfirst(lc)
Definition: pg_list.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
#define BTEqualStrategyNumber
Definition: stratnum.h:31
List *List * no_relocate
Definition: extension.c:100
ItemPointerData t_self
Definition: htup.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

References AccessShareLock, ACL_CREATE, aclcheck_error(), ACLCHECK_NOT_OWNER, ACLCHECK_OK, AlterObjectNamespace_oid(), BTEqualStrategyNumber, CatalogTupleUpdate(), changeDependencyFor(), ObjectAddress::classId, DEPENDENCY_EXTENSION, DEPENDENCY_NORMAL, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, get_extension_name(), get_extension_oid(), get_namespace_name(), getExtensionOfObject(), getObjectDescription(), GETSTRUCT(), GetUserId(), heap_copytuple(), HeapTupleIsValid, InvalidObjectAddress, InvalidOid, InvokeObjectPostAlterHook, sort-test::key, lfirst, LookupCreationNamespace(), NameStr, new_object_addresses(), ExtensionControlFile::no_relocate, object_aclcheck(), OBJECT_EXTENSION, object_ownercheck(), OBJECT_SCHEMA, ObjectAddressSet, ObjectAddress::objectId, ObjectIdGetDatum(), ObjectAddress::objectSubId, read_extension_control_file(), relation_close(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by ExecAlterObjectSchemaStmt().

◆ CreateExtension()

ObjectAddress CreateExtension ( ParseState pstate,
CreateExtensionStmt stmt 
)

Definition at line 1967 of file extension.c.

1968{
1969 DefElem *d_schema = NULL;
1970 DefElem *d_new_version = NULL;
1971 DefElem *d_cascade = NULL;
1972 char *schemaName = NULL;
1973 char *versionName = NULL;
1974 bool cascade = false;
1975 ListCell *lc;
1976
1977 /* Check extension name validity before any filesystem access */
1979
1980 /*
1981 * Check for duplicate extension name. The unique index on
1982 * pg_extension.extname would catch this anyway, and serves as a backstop
1983 * in case of race conditions; but this is a friendlier error message, and
1984 * besides we need a check to support IF NOT EXISTS.
1985 */
1986 if (get_extension_oid(stmt->extname, true) != InvalidOid)
1987 {
1988 if (stmt->if_not_exists)
1989 {
1992 errmsg("extension \"%s\" already exists, skipping",
1993 stmt->extname)));
1994 return InvalidObjectAddress;
1995 }
1996 else
1997 ereport(ERROR,
1999 errmsg("extension \"%s\" already exists",
2000 stmt->extname)));
2001 }
2002
2003 /*
2004 * We use global variables to track the extension being created, so we can
2005 * create only one extension at the same time.
2006 */
2008 ereport(ERROR,
2009 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2010 errmsg("nested CREATE EXTENSION is not supported")));
2011
2012 /* Deconstruct the statement option list */
2013 foreach(lc, stmt->options)
2014 {
2015 DefElem *defel = (DefElem *) lfirst(lc);
2016
2017 if (strcmp(defel->defname, "schema") == 0)
2018 {
2019 if (d_schema)
2020 errorConflictingDefElem(defel, pstate);
2021 d_schema = defel;
2022 schemaName = defGetString(d_schema);
2023 }
2024 else if (strcmp(defel->defname, "new_version") == 0)
2025 {
2026 if (d_new_version)
2027 errorConflictingDefElem(defel, pstate);
2028 d_new_version = defel;
2029 versionName = defGetString(d_new_version);
2030 }
2031 else if (strcmp(defel->defname, "cascade") == 0)
2032 {
2033 if (d_cascade)
2034 errorConflictingDefElem(defel, pstate);
2035 d_cascade = defel;
2036 cascade = defGetBoolean(d_cascade);
2037 }
2038 else
2039 elog(ERROR, "unrecognized option: %s", defel->defname);
2040 }
2041
2042 /* Call CreateExtensionInternal to do the real work. */
2043 return CreateExtensionInternal(stmt->extname,
2044 schemaName,
2045 versionName,
2046 cascade,
2047 NIL,
2048 true);
2049}
char * defGetString(DefElem *def)
Definition: define.c:35
bool defGetBoolean(DefElem *def)
Definition: define.c:94
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:371
#define NOTICE
Definition: elog.h:35
static void check_valid_extension_name(const char *extensionname)
Definition: extension.c:231
bool creating_extension
Definition: extension.c:77
static ObjectAddress CreateExtensionInternal(char *extensionName, char *schemaName, const char *versionName, bool cascade, List *parents, bool is_create)
Definition: extension.c:1657
#define stmt
Definition: indent_codes.h:59
#define NIL
Definition: pg_list.h:68
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:30
char * defname
Definition: parsenodes.h:826

References check_valid_extension_name(), CreateExtensionInternal(), creating_extension, defGetBoolean(), defGetString(), DefElem::defname, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, errorConflictingDefElem(), get_extension_oid(), InvalidObjectAddress, InvalidOid, lfirst, NIL, NOTICE, and stmt.

Referenced by ProcessUtilitySlow().

◆ ExecAlterExtensionContentsStmt()

ObjectAddress ExecAlterExtensionContentsStmt ( AlterExtensionContentsStmt stmt,
ObjectAddress objAddr 
)

Definition at line 3562 of file extension.c.

3564{
3565 ObjectAddress extension;
3566 ObjectAddress object;
3567 Relation relation;
3568
3569 switch (stmt->objtype)
3570 {
3571 case OBJECT_DATABASE:
3572 case OBJECT_EXTENSION:
3573 case OBJECT_INDEX:
3574 case OBJECT_PUBLICATION:
3575 case OBJECT_ROLE:
3578 case OBJECT_TABLESPACE:
3579 ereport(ERROR,
3580 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3581 errmsg("cannot add an object of this type to an extension")));
3582 break;
3583 default:
3584 /* OK */
3585 break;
3586 }
3587
3588 /*
3589 * Find the extension and acquire a lock on it, to ensure it doesn't get
3590 * dropped concurrently. A sharable lock seems sufficient: there's no
3591 * reason not to allow other sorts of manipulations, such as add/drop of
3592 * other objects, to occur concurrently. Concurrently adding/dropping the
3593 * *same* object would be bad, but we prevent that by using a non-sharable
3594 * lock on the individual object, below.
3595 */
3597 (Node *) makeString(stmt->extname),
3598 &relation, AccessShareLock, false);
3599
3600 /* Permission check: must own extension */
3601 if (!object_ownercheck(ExtensionRelationId, extension.objectId, GetUserId()))
3603 stmt->extname);
3604
3605 /*
3606 * Translate the parser representation that identifies the object into an
3607 * ObjectAddress. get_object_address() will throw an error if the object
3608 * does not exist, and will also acquire a lock on the object to guard
3609 * against concurrent DROP and ALTER EXTENSION ADD/DROP operations.
3610 */
3611 object = get_object_address(stmt->objtype, stmt->object,
3612 &relation, ShareUpdateExclusiveLock, false);
3613
3614 Assert(object.objectSubId == 0);
3615 if (objAddr)
3616 *objAddr = object;
3617
3618 /* Permission check: must own target object, too */
3619 check_object_ownership(GetUserId(), stmt->objtype, object,
3620 stmt->object, relation);
3621
3622 /* Do the update, recursing to any dependent objects */
3623 ExecAlterExtensionContentsRecurse(stmt, extension, object);
3624
3625 /* Finish up */
3626 InvokeObjectPostAlterHook(ExtensionRelationId, extension.objectId, 0);
3627
3628 /*
3629 * If get_object_address() opened the relation for us, we close it to keep
3630 * the reference count correct - but we retain any locks acquired by
3631 * get_object_address() until commit time, to guard against concurrent
3632 * activity.
3633 */
3634 if (relation != NULL)
3635 relation_close(relation, NoLock);
3636
3637 return extension;
3638}
static void ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt, ObjectAddress extension, ObjectAddress object)
Definition: extension.c:3648
Assert(PointerIsAligned(start, uint64))
#define NoLock
Definition: lockdefs.h:34
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
@ OBJECT_TABLESPACE
Definition: parsenodes.h:2359
@ OBJECT_ROLE
Definition: parsenodes.h:2350
@ OBJECT_INDEX
Definition: parsenodes.h:2337
@ OBJECT_DATABASE
Definition: parsenodes.h:2326
@ OBJECT_PUBLICATION
Definition: parsenodes.h:2347
@ OBJECT_SUBSCRIPTION
Definition: parsenodes.h:2355
@ OBJECT_STATISTIC_EXT
Definition: parsenodes.h:2356
Definition: nodes.h:135
String * makeString(char *str)
Definition: value.c:63

References AccessShareLock, aclcheck_error(), ACLCHECK_NOT_OWNER, Assert(), check_object_ownership(), ereport, errcode(), errmsg(), ERROR, ExecAlterExtensionContentsRecurse(), get_object_address(), GetUserId(), InvokeObjectPostAlterHook, makeString(), NoLock, OBJECT_DATABASE, OBJECT_EXTENSION, OBJECT_INDEX, object_ownercheck(), OBJECT_PUBLICATION, OBJECT_ROLE, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABLESPACE, ObjectAddress::objectId, relation_close(), ShareUpdateExclusiveLock, and stmt.

Referenced by ProcessUtilitySlow().

◆ ExecAlterExtensionStmt()

ObjectAddress ExecAlterExtensionStmt ( ParseState pstate,
AlterExtensionStmt stmt 
)

Definition at line 3257 of file extension.c.

3258{
3259 DefElem *d_new_version = NULL;
3260 char *versionName;
3261 char *oldVersionName;
3262 ExtensionControlFile *control;
3263 Oid extensionOid;
3264 Relation extRel;
3265 ScanKeyData key[1];
3266 SysScanDesc extScan;
3267 HeapTuple extTup;
3268 List *updateVersions;
3269 Datum datum;
3270 bool isnull;
3271 ListCell *lc;
3272 ObjectAddress address;
3273
3274 /*
3275 * We use global variables to track the extension being created, so we can
3276 * create/update only one extension at the same time.
3277 */
3279 ereport(ERROR,
3280 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3281 errmsg("nested ALTER EXTENSION is not supported")));
3282
3283 /*
3284 * Look up the extension --- it must already exist in pg_extension
3285 */
3286 extRel = table_open(ExtensionRelationId, AccessShareLock);
3287
3288 ScanKeyInit(&key[0],
3289 Anum_pg_extension_extname,
3290 BTEqualStrategyNumber, F_NAMEEQ,
3291 CStringGetDatum(stmt->extname));
3292
3293 extScan = systable_beginscan(extRel, ExtensionNameIndexId, true,
3294 NULL, 1, key);
3295
3296 extTup = systable_getnext(extScan);
3297
3298 if (!HeapTupleIsValid(extTup))
3299 ereport(ERROR,
3300 (errcode(ERRCODE_UNDEFINED_OBJECT),
3301 errmsg("extension \"%s\" does not exist",
3302 stmt->extname)));
3303
3304 extensionOid = ((Form_pg_extension) GETSTRUCT(extTup))->oid;
3305
3306 /*
3307 * Determine the existing version we are updating from
3308 */
3309 datum = heap_getattr(extTup, Anum_pg_extension_extversion,
3310 RelationGetDescr(extRel), &isnull);
3311 if (isnull)
3312 elog(ERROR, "extversion is null");
3313 oldVersionName = text_to_cstring(DatumGetTextPP(datum));
3314
3315 systable_endscan(extScan);
3316
3318
3319 /* Permission check: must own extension */
3320 if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
3322 stmt->extname);
3323
3324 /*
3325 * Read the primary control file. Note we assume that it does not contain
3326 * any non-ASCII data, so there is no need to worry about encoding at this
3327 * point.
3328 */
3329 control = read_extension_control_file(stmt->extname);
3330
3331 /*
3332 * Read the statement option list
3333 */
3334 foreach(lc, stmt->options)
3335 {
3336 DefElem *defel = (DefElem *) lfirst(lc);
3337
3338 if (strcmp(defel->defname, "new_version") == 0)
3339 {
3340 if (d_new_version)
3341 errorConflictingDefElem(defel, pstate);
3342 d_new_version = defel;
3343 }
3344 else
3345 elog(ERROR, "unrecognized option: %s", defel->defname);
3346 }
3347
3348 /*
3349 * Determine the version to update to
3350 */
3351 if (d_new_version && d_new_version->arg)
3352 versionName = strVal(d_new_version->arg);
3353 else if (control->default_version)
3354 versionName = control->default_version;
3355 else
3356 {
3357 ereport(ERROR,
3358 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3359 errmsg("version to install must be specified")));
3360 versionName = NULL; /* keep compiler quiet */
3361 }
3362 check_valid_version_name(versionName);
3363
3364 /*
3365 * If we're already at that version, just say so
3366 */
3367 if (strcmp(oldVersionName, versionName) == 0)
3368 {
3370 (errmsg("version \"%s\" of extension \"%s\" is already installed",
3371 versionName, stmt->extname)));
3372 return InvalidObjectAddress;
3373 }
3374
3375 /*
3376 * Identify the series of update script files we need to execute
3377 */
3378 updateVersions = identify_update_path(control,
3379 oldVersionName,
3380 versionName);
3381
3382 /*
3383 * Update the pg_extension row and execute the update scripts, one at a
3384 * time
3385 */
3386 ApplyExtensionUpdates(extensionOid, control,
3387 oldVersionName, updateVersions,
3388 NULL, false, false);
3389
3390 ObjectAddressSet(address, ExtensionRelationId, extensionOid);
3391
3392 return address;
3393}
static void check_valid_version_name(const char *versionname)
Definition: extension.c:278
static List * identify_update_path(ExtensionControlFile *control, const char *oldVersion, const char *newVersion)
Definition: extension.c:1466
static void ApplyExtensionUpdates(Oid extensionOid, ExtensionControlFile *pcontrol, const char *initialVersion, List *updateVersions, char *origSchemaName, bool cascade, bool is_create)
Definition: extension.c:3404
#define DatumGetTextPP(X)
Definition: fmgr.h:292
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:904
uintptr_t Datum
Definition: postgres.h:69
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
#define RelationGetDescr(relation)
Definition: rel.h:542
Node * arg
Definition: parsenodes.h:827
char * default_version
Definition: extension.c:90
Definition: pg_list.h:54
#define strVal(v)
Definition: value.h:82
char * text_to_cstring(const text *t)
Definition: varlena.c:225

References AccessShareLock, aclcheck_error(), ACLCHECK_NOT_OWNER, ApplyExtensionUpdates(), DefElem::arg, BTEqualStrategyNumber, check_valid_version_name(), creating_extension, CStringGetDatum(), DatumGetTextPP, ExtensionControlFile::default_version, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, errorConflictingDefElem(), GETSTRUCT(), GetUserId(), heap_getattr(), HeapTupleIsValid, identify_update_path(), InvalidObjectAddress, sort-test::key, lfirst, NOTICE, OBJECT_EXTENSION, object_ownercheck(), ObjectAddressSet, read_extension_control_file(), RelationGetDescr, ScanKeyInit(), stmt, strVal, systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), and text_to_cstring().

Referenced by ProcessUtilitySlow().

◆ extension_file_exists()

bool extension_file_exists ( const char *  extensionName)

Definition at line 2471 of file extension.c.

2472{
2473 bool result = false;
2474 List *locations;
2475 DIR *dir;
2476 struct dirent *de;
2477
2479
2480 foreach_ptr(char, location, locations)
2481 {
2482 dir = AllocateDir(location);
2483
2484 /*
2485 * If the control directory doesn't exist, we want to silently return
2486 * false. Any other error will be reported by ReadDir.
2487 */
2488 if (dir == NULL && errno == ENOENT)
2489 {
2490 /* do nothing */
2491 }
2492 else
2493 {
2494 while ((de = ReadDir(dir, location)) != NULL)
2495 {
2496 char *extname;
2497
2499 continue;
2500
2501 /* extract extension name from 'name.control' filename */
2502 extname = pstrdup(de->d_name);
2503 *strrchr(extname, '.') = '\0';
2504
2505 /* ignore it if it's an auxiliary control file */
2506 if (strstr(extname, "--"))
2507 continue;
2508
2509 /* done if it matches request */
2510 if (strcmp(extname, extensionName) == 0)
2511 {
2512 result = true;
2513 break;
2514 }
2515 }
2516
2517 FreeDir(dir);
2518 }
2519 if (result)
2520 break;
2521 }
2522
2523 return result;
2524}
static bool is_extension_control_filename(const char *filename)
Definition: extension.c:325
static List * get_extension_control_directories(void)
Definition: extension.c:344
int FreeDir(DIR *dir)
Definition: fd.c:3025
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2907
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2973
char * pstrdup(const char *in)
Definition: mcxt.c:2327
#define foreach_ptr(type, var, lst)
Definition: pg_list.h:469
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References AllocateDir(), dirent::d_name, foreach_ptr, FreeDir(), get_extension_control_directories(), is_extension_control_filename(), pstrdup(), and ReadDir().

Referenced by CreateFunction(), and ExecuteDoStmt().

◆ get_extension_name()

char * get_extension_name ( Oid  ext_oid)

Definition at line 189 of file extension.c.

190{
191 char *result;
192 HeapTuple tuple;
193
194 tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
195
196 if (!HeapTupleIsValid(tuple))
197 return NULL;
198
199 result = pstrdup(NameStr(((Form_pg_extension) GETSTRUCT(tuple))->extname));
200 ReleaseSysCache(tuple);
201
202 return result;
203}
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221

References GETSTRUCT(), HeapTupleIsValid, NameStr, ObjectIdGetDatum(), pstrdup(), ReleaseSysCache(), and SearchSysCache1().

Referenced by AlterExtensionNamespace(), checkMembershipInCurrentExtension(), ExecAlterExtensionContentsRecurse(), getObjectDescription(), getObjectIdentityParts(), recordDependencyOnCurrentExtension(), and RemoveExtensionById().

◆ get_extension_oid()

Oid get_extension_oid ( const char *  extname,
bool  missing_ok 
)

Definition at line 167 of file extension.c.

168{
169 Oid result;
170
171 result = GetSysCacheOid1(EXTENSIONNAME, Anum_pg_extension_oid,
172 CStringGetDatum(extname));
173
174 if (!OidIsValid(result) && !missing_ok)
176 (errcode(ERRCODE_UNDEFINED_OBJECT),
177 errmsg("extension \"%s\" does not exist",
178 extname)));
179
180 return result;
181}
#define OidIsValid(objectId)
Definition: c.h:746
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:109

References CStringGetDatum(), ereport, errcode(), errmsg(), ERROR, GetSysCacheOid1, and OidIsValid.

Referenced by AlterExtensionNamespace(), binary_upgrade_create_empty_extension(), CreateExtension(), ExtractExtensionList(), get_object_address_unqualified(), and get_required_extension().

◆ get_extension_schema()

Oid get_extension_schema ( Oid  ext_oid)

Definition at line 211 of file extension.c.

212{
213 Oid result;
214 HeapTuple tuple;
215
216 tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
217
218 if (!HeapTupleIsValid(tuple))
219 return InvalidOid;
220
221 result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
222 ReleaseSysCache(tuple);
223
224 return result;
225}

References GETSTRUCT(), HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), ReleaseSysCache(), and SearchSysCache1().

Referenced by ApplyExtensionUpdates(), CreateExtensionInternal(), and ExecAlterExtensionContentsRecurse().

◆ InsertExtensionTuple()

ObjectAddress InsertExtensionTuple ( const char *  extName,
Oid  extOwner,
Oid  schemaOid,
bool  relocatable,
const char *  extVersion,
Datum  extConfig,
Datum  extCondition,
List requiredExtensions 
)

Definition at line 2065 of file extension.c.

2069{
2070 Oid extensionOid;
2071 Relation rel;
2072 Datum values[Natts_pg_extension];
2073 bool nulls[Natts_pg_extension];
2074 HeapTuple tuple;
2075 ObjectAddress myself;
2076 ObjectAddress nsp;
2077 ObjectAddresses *refobjs;
2078 ListCell *lc;
2079
2080 /*
2081 * Build and insert the pg_extension tuple
2082 */
2083 rel = table_open(ExtensionRelationId, RowExclusiveLock);
2084
2085 memset(values, 0, sizeof(values));
2086 memset(nulls, 0, sizeof(nulls));
2087
2088 extensionOid = GetNewOidWithIndex(rel, ExtensionOidIndexId,
2089 Anum_pg_extension_oid);
2090 values[Anum_pg_extension_oid - 1] = ObjectIdGetDatum(extensionOid);
2091 values[Anum_pg_extension_extname - 1] =
2093 values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extOwner);
2094 values[Anum_pg_extension_extnamespace - 1] = ObjectIdGetDatum(schemaOid);
2095 values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(relocatable);
2096 values[Anum_pg_extension_extversion - 1] = CStringGetTextDatum(extVersion);
2097
2098 if (extConfig == PointerGetDatum(NULL))
2099 nulls[Anum_pg_extension_extconfig - 1] = true;
2100 else
2101 values[Anum_pg_extension_extconfig - 1] = extConfig;
2102
2103 if (extCondition == PointerGetDatum(NULL))
2104 nulls[Anum_pg_extension_extcondition - 1] = true;
2105 else
2106 values[Anum_pg_extension_extcondition - 1] = extCondition;
2107
2108 tuple = heap_form_tuple(rel->rd_att, values, nulls);
2109
2110 CatalogTupleInsert(rel, tuple);
2111
2112 heap_freetuple(tuple);
2114
2115 /*
2116 * Record dependencies on owner, schema, and prerequisite extensions
2117 */
2118 recordDependencyOnOwner(ExtensionRelationId, extensionOid, extOwner);
2119
2120 refobjs = new_object_addresses();
2121
2122 ObjectAddressSet(myself, ExtensionRelationId, extensionOid);
2123
2124 ObjectAddressSet(nsp, NamespaceRelationId, schemaOid);
2125 add_exact_object_address(&nsp, refobjs);
2126
2127 foreach(lc, requiredExtensions)
2128 {
2129 Oid reqext = lfirst_oid(lc);
2130 ObjectAddress otherext;
2131
2132 ObjectAddressSet(otherext, ExtensionRelationId, reqext);
2133 add_exact_object_address(&otherext, refobjs);
2134 }
2135
2136 /* Record all of them (this includes duplicate elimination) */
2138 free_object_addresses(refobjs);
2139
2140 /* Post creation hook for new extension */
2141 InvokeObjectPostCreateHook(ExtensionRelationId, extensionOid, 0);
2142
2143 return myself;
2144}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:450
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2757
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2548
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2788
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:173
#define lfirst_oid(lc)
Definition: pg_list.h:174
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:168
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
TupleDesc rd_att
Definition: rel.h:112

References add_exact_object_address(), BoolGetDatum(), CatalogTupleInsert(), CStringGetDatum(), CStringGetTextDatum, DEPENDENCY_NORMAL, DirectFunctionCall1, free_object_addresses(), GetNewOidWithIndex(), heap_form_tuple(), heap_freetuple(), InvokeObjectPostCreateHook, lfirst_oid, namein(), new_object_addresses(), ObjectAddressSet, ObjectIdGetDatum(), PointerGetDatum(), RelationData::rd_att, record_object_address_dependencies(), recordDependencyOnOwner(), RowExclusiveLock, table_close(), table_open(), and values.

Referenced by binary_upgrade_create_empty_extension(), and CreateExtensionInternal().

◆ RemoveExtensionById()

void RemoveExtensionById ( Oid  extId)

Definition at line 2153 of file extension.c.

2154{
2155 Relation rel;
2156 SysScanDesc scandesc;
2157 HeapTuple tuple;
2158 ScanKeyData entry[1];
2159
2160 /*
2161 * Disallow deletion of any extension that's currently open for insertion;
2162 * else subsequent executions of recordDependencyOnCurrentExtension()
2163 * could create dangling pg_depend records that refer to a no-longer-valid
2164 * pg_extension OID. This is needed not so much because we think people
2165 * might write "DROP EXTENSION foo" in foo's own script files, as because
2166 * errors in dependency management in extension script files could give
2167 * rise to cases where an extension is dropped as a result of recursing
2168 * from some contained object. Because of that, we must test for the case
2169 * here, not at some higher level of the DROP EXTENSION command.
2170 */
2171 if (extId == CurrentExtensionObject)
2172 ereport(ERROR,
2173 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2174 errmsg("cannot drop extension \"%s\" because it is being modified",
2175 get_extension_name(extId))));
2176
2177 rel = table_open(ExtensionRelationId, RowExclusiveLock);
2178
2179 ScanKeyInit(&entry[0],
2180 Anum_pg_extension_oid,
2181 BTEqualStrategyNumber, F_OIDEQ,
2182 ObjectIdGetDatum(extId));
2183 scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
2184 NULL, 1, entry);
2185
2186 tuple = systable_getnext(scandesc);
2187
2188 /* We assume that there can be at most one matching tuple */
2189 if (HeapTupleIsValid(tuple))
2190 CatalogTupleDelete(rel, &tuple->t_self);
2191
2192 systable_endscan(scandesc);
2193
2195}
Oid CurrentExtensionObject
Definition: extension.c:78
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365

References BTEqualStrategyNumber, CatalogTupleDelete(), CurrentExtensionObject, ereport, errcode(), errmsg(), ERROR, get_extension_name(), HeapTupleIsValid, ObjectIdGetDatum(), RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

Variable Documentation

◆ creating_extension

◆ CurrentExtensionObject

◆ Extension_control_path

PGDLLIMPORT char* Extension_control_path
extern

Definition at line 74 of file extension.c.

Referenced by get_extension_control_directories().