PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 3040 of file extension.c.

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

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

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

2470{
2471 bool result = false;
2472 List *locations;
2473 DIR *dir;
2474 struct dirent *de;
2475
2477
2478 foreach_ptr(char, location, locations)
2479 {
2480 dir = AllocateDir(location);
2481
2482 /*
2483 * If the control directory doesn't exist, we want to silently return
2484 * false. Any other error will be reported by ReadDir.
2485 */
2486 if (dir == NULL && errno == ENOENT)
2487 {
2488 /* do nothing */
2489 }
2490 else
2491 {
2492 while ((de = ReadDir(dir, location)) != NULL)
2493 {
2494 char *extname;
2495
2497 continue;
2498
2499 /* extract extension name from 'name.control' filename */
2500 extname = pstrdup(de->d_name);
2501 *strrchr(extname, '.') = '\0';
2502
2503 /* ignore it if it's an auxiliary control file */
2504 if (strstr(extname, "--"))
2505 continue;
2506
2507 /* done if it matches request */
2508 if (strcmp(extname, extensionName) == 0)
2509 {
2510 result = true;
2511 break;
2512 }
2513 }
2514
2515 FreeDir(dir);
2516 }
2517 if (result)
2518 break;
2519 }
2520
2521 return result;
2522}
static bool is_extension_control_filename(const char *filename)
Definition: extension.c:322
static List * get_extension_control_directories(void)
Definition: extension.c:341
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:2321
#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 186 of file extension.c.

187{
188 char *result;
189 HeapTuple tuple;
190
191 tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
192
193 if (!HeapTupleIsValid(tuple))
194 return NULL;
195
196 result = pstrdup(NameStr(((Form_pg_extension) GETSTRUCT(tuple))->extname));
197 ReleaseSysCache(tuple);
198
199 return result;
200}
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 164 of file extension.c.

165{
166 Oid result;
167
168 result = GetSysCacheOid1(EXTENSIONNAME, Anum_pg_extension_oid,
169 CStringGetDatum(extname));
170
171 if (!OidIsValid(result) && !missing_ok)
173 (errcode(ERRCODE_UNDEFINED_OBJECT),
174 errmsg("extension \"%s\" does not exist",
175 extname)));
176
177 return result;
178}
#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 208 of file extension.c.

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

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: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 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, 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