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 3064 of file extension.c.

3065{
3066 Oid extensionOid;
3067 Oid nspOid;
3068 Oid oldNspOid;
3069 AclResult aclresult;
3070 Relation extRel;
3071 ScanKeyData key[2];
3072 SysScanDesc extScan;
3073 HeapTuple extTup;
3074 Form_pg_extension extForm;
3075 Relation depRel;
3076 SysScanDesc depScan;
3077 HeapTuple depTup;
3078 ObjectAddresses *objsMoved;
3079 ObjectAddress extAddr;
3080
3081 extensionOid = get_extension_oid(extensionName, false);
3082
3083 nspOid = LookupCreationNamespace(newschema);
3084
3085 /*
3086 * Permission check: must own extension. Note that we don't bother to
3087 * check ownership of the individual member objects ...
3088 */
3089 if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
3091 extensionName);
3092
3093 /* Permission check: must have creation rights in target namespace */
3094 aclresult = object_aclcheck(NamespaceRelationId, nspOid, GetUserId(), ACL_CREATE);
3095 if (aclresult != ACLCHECK_OK)
3096 aclcheck_error(aclresult, OBJECT_SCHEMA, newschema);
3097
3098 /*
3099 * If the schema is currently a member of the extension, disallow moving
3100 * the extension into the schema. That would create a dependency loop.
3101 */
3102 if (getExtensionOfObject(NamespaceRelationId, nspOid) == extensionOid)
3103 ereport(ERROR,
3104 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3105 errmsg("cannot move extension \"%s\" into schema \"%s\" "
3106 "because the extension contains the schema",
3107 extensionName, newschema)));
3108
3109 /* Locate the pg_extension tuple */
3110 extRel = table_open(ExtensionRelationId, RowExclusiveLock);
3111
3112 ScanKeyInit(&key[0],
3113 Anum_pg_extension_oid,
3114 BTEqualStrategyNumber, F_OIDEQ,
3115 ObjectIdGetDatum(extensionOid));
3116
3117 extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
3118 NULL, 1, key);
3119
3120 extTup = systable_getnext(extScan);
3121
3122 if (!HeapTupleIsValid(extTup)) /* should not happen */
3123 elog(ERROR, "could not find tuple for extension %u",
3124 extensionOid);
3125
3126 /* Copy tuple so we can modify it below */
3127 extTup = heap_copytuple(extTup);
3128 extForm = (Form_pg_extension) GETSTRUCT(extTup);
3129
3130 systable_endscan(extScan);
3131
3132 /*
3133 * If the extension is already in the target schema, just silently do
3134 * nothing.
3135 */
3136 if (extForm->extnamespace == nspOid)
3137 {
3139 return InvalidObjectAddress;
3140 }
3141
3142 /* Check extension is supposed to be relocatable */
3143 if (!extForm->extrelocatable)
3144 ereport(ERROR,
3145 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3146 errmsg("extension \"%s\" does not support SET SCHEMA",
3147 NameStr(extForm->extname))));
3148
3149 objsMoved = new_object_addresses();
3150
3151 /* store the OID of the namespace to-be-changed */
3152 oldNspOid = extForm->extnamespace;
3153
3154 /*
3155 * Scan pg_depend to find objects that depend directly on the extension,
3156 * and alter each one's schema.
3157 */
3158 depRel = table_open(DependRelationId, AccessShareLock);
3159
3160 ScanKeyInit(&key[0],
3161 Anum_pg_depend_refclassid,
3162 BTEqualStrategyNumber, F_OIDEQ,
3163 ObjectIdGetDatum(ExtensionRelationId));
3164 ScanKeyInit(&key[1],
3165 Anum_pg_depend_refobjid,
3166 BTEqualStrategyNumber, F_OIDEQ,
3167 ObjectIdGetDatum(extensionOid));
3168
3169 depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
3170 NULL, 2, key);
3171
3172 while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
3173 {
3174 Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
3175 ObjectAddress dep;
3176 Oid dep_oldNspOid;
3177
3178 /*
3179 * If a dependent extension has a no_relocate request for this
3180 * extension, disallow SET SCHEMA. (XXX it's a bit ugly to do this in
3181 * the same loop that's actually executing the renames: we may detect
3182 * the error condition only after having expended a fair amount of
3183 * work. However, the alternative is to do two scans of pg_depend,
3184 * which seems like optimizing for failure cases. The rename work
3185 * will all roll back cleanly enough if we do fail here.)
3186 */
3187 if (pg_depend->deptype == DEPENDENCY_NORMAL &&
3188 pg_depend->classid == ExtensionRelationId)
3189 {
3190 char *depextname = get_extension_name(pg_depend->objid);
3191 ExtensionControlFile *dcontrol;
3192 ListCell *lc;
3193
3194 dcontrol = read_extension_control_file(depextname);
3195 foreach(lc, dcontrol->no_relocate)
3196 {
3197 char *nrextname = (char *) lfirst(lc);
3198
3199 if (strcmp(nrextname, NameStr(extForm->extname)) == 0)
3200 {
3201 ereport(ERROR,
3202 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3203 errmsg("cannot SET SCHEMA of extension \"%s\" because other extensions prevent it",
3204 NameStr(extForm->extname)),
3205 errdetail("Extension \"%s\" requests no relocation of extension \"%s\".",
3206 depextname,
3207 NameStr(extForm->extname))));
3208 }
3209 }
3210 }
3211
3212 /*
3213 * Otherwise, ignore non-membership dependencies. (Currently, the
3214 * only other case we could see here is a normal dependency from
3215 * another extension.)
3216 */
3217 if (pg_depend->deptype != DEPENDENCY_EXTENSION)
3218 continue;
3219
3220 dep.classId = pg_depend->classid;
3221 dep.objectId = pg_depend->objid;
3222 dep.objectSubId = pg_depend->objsubid;
3223
3224 if (dep.objectSubId != 0) /* should not happen */
3225 elog(ERROR, "extension should not have a sub-object dependency");
3226
3227 /* Relocate the object */
3228 dep_oldNspOid = AlterObjectNamespace_oid(dep.classId,
3229 dep.objectId,
3230 nspOid,
3231 objsMoved);
3232
3233 /*
3234 * If not all the objects had the same old namespace (ignoring any
3235 * that are not in namespaces or are dependent types), complain.
3236 */
3237 if (dep_oldNspOid != InvalidOid && dep_oldNspOid != oldNspOid)
3238 ereport(ERROR,
3239 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3240 errmsg("extension \"%s\" does not support SET SCHEMA",
3241 NameStr(extForm->extname)),
3242 errdetail("%s is not in the extension's schema \"%s\"",
3243 getObjectDescription(&dep, false),
3244 get_namespace_name(oldNspOid))));
3245 }
3246
3247 /* report old schema, if caller wants it */
3248 if (oldschema)
3249 *oldschema = oldNspOid;
3250
3251 systable_endscan(depScan);
3252
3254
3255 /* Now adjust pg_extension.extnamespace */
3256 extForm->extnamespace = nspOid;
3257
3258 CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
3259
3261
3262 /* update dependency to point to the new schema */
3263 if (changeDependencyFor(ExtensionRelationId, extensionOid,
3264 NamespaceRelationId, oldNspOid, nspOid) != 1)
3265 elog(ERROR, "could not change schema dependency for extension %s",
3266 NameStr(extForm->extname));
3267
3268 InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
3269
3270 ObjectAddressSet(extAddr, ExtensionRelationId, extensionOid);
3271
3272 return extAddr;
3273}
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:2652
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3834
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4088
Oid AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: alter.c:625
#define NameStr(name)
Definition: c.h:754
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2615
@ DEPENDENCY_EXTENSION
Definition: dependency.h:38
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
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, const ItemPointerData *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:3533
Oid GetUserId(void)
Definition: miscinit.c:469
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:3498
#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:2361
@ OBJECT_EXTENSION
Definition: parsenodes.h:2340
#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:262
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
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 1965 of file extension.c.

1966{
1967 DefElem *d_schema = NULL;
1968 DefElem *d_new_version = NULL;
1969 DefElem *d_cascade = NULL;
1970 char *schemaName = NULL;
1971 char *versionName = NULL;
1972 bool cascade = false;
1973 ListCell *lc;
1974
1975 /* Check extension name validity before any filesystem access */
1977
1978 /*
1979 * Check for duplicate extension name. The unique index on
1980 * pg_extension.extname would catch this anyway, and serves as a backstop
1981 * in case of race conditions; but this is a friendlier error message, and
1982 * besides we need a check to support IF NOT EXISTS.
1983 */
1984 if (get_extension_oid(stmt->extname, true) != InvalidOid)
1985 {
1986 if (stmt->if_not_exists)
1987 {
1990 errmsg("extension \"%s\" already exists, skipping",
1991 stmt->extname)));
1992 return InvalidObjectAddress;
1993 }
1994 else
1995 ereport(ERROR,
1997 errmsg("extension \"%s\" already exists",
1998 stmt->extname)));
1999 }
2000
2001 /*
2002 * We use global variables to track the extension being created, so we can
2003 * create only one extension at the same time.
2004 */
2006 ereport(ERROR,
2007 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2008 errmsg("nested CREATE EXTENSION is not supported")));
2009
2010 /* Deconstruct the statement option list */
2011 foreach(lc, stmt->options)
2012 {
2013 DefElem *defel = (DefElem *) lfirst(lc);
2014
2015 if (strcmp(defel->defname, "schema") == 0)
2016 {
2017 if (d_schema)
2018 errorConflictingDefElem(defel, pstate);
2019 d_schema = defel;
2020 schemaName = defGetString(d_schema);
2021 }
2022 else if (strcmp(defel->defname, "new_version") == 0)
2023 {
2024 if (d_new_version)
2025 errorConflictingDefElem(defel, pstate);
2026 d_new_version = defel;
2027 versionName = defGetString(d_new_version);
2028 }
2029 else if (strcmp(defel->defname, "cascade") == 0)
2030 {
2031 if (d_cascade)
2032 errorConflictingDefElem(defel, pstate);
2033 d_cascade = defel;
2034 cascade = defGetBoolean(d_cascade);
2035 }
2036 else
2037 elog(ERROR, "unrecognized option: %s", defel->defname);
2038 }
2039
2040 /* Call CreateExtensionInternal to do the real work. */
2041 return CreateExtensionInternal(stmt->extname,
2042 schemaName,
2043 versionName,
2044 cascade,
2045 NIL,
2046 true);
2047}
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:1655
#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:843

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 3584 of file extension.c.

3586{
3587 ObjectAddress extension;
3588 ObjectAddress object;
3589 Relation relation;
3590
3591 switch (stmt->objtype)
3592 {
3593 case OBJECT_DATABASE:
3594 case OBJECT_EXTENSION:
3595 case OBJECT_INDEX:
3596 case OBJECT_PUBLICATION:
3597 case OBJECT_ROLE:
3600 case OBJECT_TABLESPACE:
3601 ereport(ERROR,
3602 (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3603 errmsg("cannot add an object of this type to an extension")));
3604 break;
3605 default:
3606 /* OK */
3607 break;
3608 }
3609
3610 /*
3611 * Find the extension and acquire a lock on it, to ensure it doesn't get
3612 * dropped concurrently. A sharable lock seems sufficient: there's no
3613 * reason not to allow other sorts of manipulations, such as add/drop of
3614 * other objects, to occur concurrently. Concurrently adding/dropping the
3615 * *same* object would be bad, but we prevent that by using a non-sharable
3616 * lock on the individual object, below.
3617 */
3619 (Node *) makeString(stmt->extname),
3620 &relation, AccessShareLock, false);
3621
3622 /* Permission check: must own extension */
3623 if (!object_ownercheck(ExtensionRelationId, extension.objectId, GetUserId()))
3625 stmt->extname);
3626
3627 /*
3628 * Translate the parser representation that identifies the object into an
3629 * ObjectAddress. get_object_address() will throw an error if the object
3630 * does not exist, and will also acquire a lock on the object to guard
3631 * against concurrent DROP and ALTER EXTENSION ADD/DROP operations.
3632 */
3633 object = get_object_address(stmt->objtype, stmt->object,
3634 &relation, ShareUpdateExclusiveLock, false);
3635
3636 Assert(object.objectSubId == 0);
3637 if (objAddr)
3638 *objAddr = object;
3639
3640 /* Permission check: must own target object, too */
3641 check_object_ownership(GetUserId(), stmt->objtype, object,
3642 stmt->object, relation);
3643
3644 /* Do the update, recursing to any dependent objects */
3645 ExecAlterExtensionContentsRecurse(stmt, extension, object);
3646
3647 /* Finish up */
3648 InvokeObjectPostAlterHook(ExtensionRelationId, extension.objectId, 0);
3649
3650 /*
3651 * If get_object_address() opened the relation for us, we close it to keep
3652 * the reference count correct - but we retain any locks acquired by
3653 * get_object_address() until commit time, to guard against concurrent
3654 * activity.
3655 */
3656 if (relation != NULL)
3657 relation_close(relation, NoLock);
3658
3659 return extension;
3660}
static void ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt, ObjectAddress extension, ObjectAddress object)
Definition: extension.c:3670
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:2367
@ OBJECT_ROLE
Definition: parsenodes.h:2358
@ OBJECT_INDEX
Definition: parsenodes.h:2345
@ OBJECT_DATABASE
Definition: parsenodes.h:2334
@ OBJECT_PUBLICATION
Definition: parsenodes.h:2355
@ OBJECT_SUBSCRIPTION
Definition: parsenodes.h:2363
@ OBJECT_STATISTIC_EXT
Definition: parsenodes.h:2364
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 3279 of file extension.c.

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

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 2493 of file extension.c.

2494{
2495 bool result = false;
2496 List *locations;
2497 DIR *dir;
2498 struct dirent *de;
2499
2501
2502 foreach_ptr(char, location, locations)
2503 {
2504 dir = AllocateDir(location);
2505
2506 /*
2507 * If the control directory doesn't exist, we want to silently return
2508 * false. Any other error will be reported by ReadDir.
2509 */
2510 if (dir == NULL && errno == ENOENT)
2511 {
2512 /* do nothing */
2513 }
2514 else
2515 {
2516 while ((de = ReadDir(dir, location)) != NULL)
2517 {
2518 char *extname;
2519
2521 continue;
2522
2523 /* extract extension name from 'name.control' filename */
2524 extname = pstrdup(de->d_name);
2525 *strrchr(extname, '.') = '\0';
2526
2527 /* ignore it if it's an auxiliary control file */
2528 if (strstr(extname, "--"))
2529 continue;
2530
2531 /* done if it matches request */
2532 if (strcmp(extname, extensionName) == 0)
2533 {
2534 result = true;
2535 break;
2536 }
2537 }
2538
2539 FreeDir(dir);
2540 }
2541 if (result)
2542 break;
2543 }
2544
2545 return result;
2546}
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:3022
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2904
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2970
char * pstrdup(const char *in)
Definition: mcxt.c:1759
#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:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

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:777
#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 2063 of file extension.c.

2067{
2068 Oid extensionOid;
2069 Relation rel;
2070 Datum values[Natts_pg_extension];
2071 bool nulls[Natts_pg_extension];
2072 HeapTuple tuple;
2073 ObjectAddress myself;
2074 ObjectAddress nsp;
2075 ObjectAddresses *refobjs;
2076 ListCell *lc;
2077
2078 /*
2079 * Build and insert the pg_extension tuple
2080 */
2081 rel = table_open(ExtensionRelationId, RowExclusiveLock);
2082
2083 memset(values, 0, sizeof(values));
2084 memset(nulls, 0, sizeof(nulls));
2085
2086 extensionOid = GetNewOidWithIndex(rel, ExtensionOidIndexId,
2087 Anum_pg_extension_oid);
2088 values[Anum_pg_extension_oid - 1] = ObjectIdGetDatum(extensionOid);
2089 values[Anum_pg_extension_extname - 1] =
2091 values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extOwner);
2092 values[Anum_pg_extension_extnamespace - 1] = ObjectIdGetDatum(schemaOid);
2093 values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(relocatable);
2094 values[Anum_pg_extension_extversion - 1] = CStringGetTextDatum(extVersion);
2095
2096 if (extConfig == PointerGetDatum(NULL))
2097 nulls[Anum_pg_extension_extconfig - 1] = true;
2098 else
2099 values[Anum_pg_extension_extconfig - 1] = extConfig;
2100
2101 if (extCondition == PointerGetDatum(NULL))
2102 nulls[Anum_pg_extension_extcondition - 1] = true;
2103 else
2104 values[Anum_pg_extension_extcondition - 1] = extCondition;
2105
2106 tuple = heap_form_tuple(rel->rd_att, values, nulls);
2107
2108 CatalogTupleInsert(rel, tuple);
2109
2110 heap_freetuple(tuple);
2112
2113 /*
2114 * Record dependencies on owner, schema, and prerequisite extensions
2115 */
2116 recordDependencyOnOwner(ExtensionRelationId, extensionOid, extOwner);
2117
2118 refobjs = new_object_addresses();
2119
2120 ObjectAddressSet(myself, ExtensionRelationId, extensionOid);
2121
2122 ObjectAddressSet(nsp, NamespaceRelationId, schemaOid);
2123 add_exact_object_address(&nsp, refobjs);
2124
2125 foreach(lc, requiredExtensions)
2126 {
2127 Oid reqext = lfirst_oid(lc);
2128 ObjectAddress otherext;
2129
2130 ObjectAddressSet(otherext, ExtensionRelationId, reqext);
2131 add_exact_object_address(&otherext, refobjs);
2132 }
2133
2134 /* Record all of them (this includes duplicate elimination) */
2136 free_object_addresses(refobjs);
2137
2138 /* Post creation hook for new extension */
2139 InvokeObjectPostCreateHook(ExtensionRelationId, extensionOid, 0);
2140
2141 return myself;
2142}
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:448
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2870
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2661
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2901
#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:332
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
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 2151 of file extension.c.

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