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)
 
bool extension_file_exists (const char *extensionName)
 
ObjectAddress AlterExtensionNamespace (const char *extensionName, const char *newschema, Oid *oldschema)
 

Variables

PGDLLIMPORT bool creating_extension
 
PGDLLIMPORT Oid CurrentExtensionObject
 

Function Documentation

◆ AlterExtensionNamespace()

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

Definition at line 2787 of file extension.c.

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

Referenced by ExecAlterObjectSchemaStmt().

2788 {
2789  Oid extensionOid;
2790  Oid nspOid;
2791  Oid oldNspOid = InvalidOid;
2792  AclResult aclresult;
2793  Relation extRel;
2794  ScanKeyData key[2];
2795  SysScanDesc extScan;
2796  HeapTuple extTup;
2797  Form_pg_extension extForm;
2798  Relation depRel;
2799  SysScanDesc depScan;
2800  HeapTuple depTup;
2801  ObjectAddresses *objsMoved;
2802  ObjectAddress extAddr;
2803 
2804  extensionOid = get_extension_oid(extensionName, false);
2805 
2806  nspOid = LookupCreationNamespace(newschema);
2807 
2808  /*
2809  * Permission check: must own extension. Note that we don't bother to
2810  * check ownership of the individual member objects ...
2811  */
2812  if (!pg_extension_ownercheck(extensionOid, GetUserId()))
2814  extensionName);
2815 
2816  /* Permission check: must have creation rights in target namespace */
2817  aclresult = pg_namespace_aclcheck(nspOid, GetUserId(), ACL_CREATE);
2818  if (aclresult != ACLCHECK_OK)
2819  aclcheck_error(aclresult, OBJECT_SCHEMA, newschema);
2820 
2821  /*
2822  * If the schema is currently a member of the extension, disallow moving
2823  * the extension into the schema. That would create a dependency loop.
2824  */
2825  if (getExtensionOfObject(NamespaceRelationId, nspOid) == extensionOid)
2826  ereport(ERROR,
2827  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2828  errmsg("cannot move extension \"%s\" into schema \"%s\" "
2829  "because the extension contains the schema",
2830  extensionName, newschema)));
2831 
2832  /* Locate the pg_extension tuple */
2833  extRel = table_open(ExtensionRelationId, RowExclusiveLock);
2834 
2835  ScanKeyInit(&key[0],
2836  Anum_pg_extension_oid,
2837  BTEqualStrategyNumber, F_OIDEQ,
2838  ObjectIdGetDatum(extensionOid));
2839 
2840  extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
2841  NULL, 1, key);
2842 
2843  extTup = systable_getnext(extScan);
2844 
2845  if (!HeapTupleIsValid(extTup)) /* should not happen */
2846  elog(ERROR, "could not find tuple for extension %u",
2847  extensionOid);
2848 
2849  /* Copy tuple so we can modify it below */
2850  extTup = heap_copytuple(extTup);
2851  extForm = (Form_pg_extension) GETSTRUCT(extTup);
2852 
2853  systable_endscan(extScan);
2854 
2855  /*
2856  * If the extension is already in the target schema, just silently do
2857  * nothing.
2858  */
2859  if (extForm->extnamespace == nspOid)
2860  {
2861  table_close(extRel, RowExclusiveLock);
2862  return InvalidObjectAddress;
2863  }
2864 
2865  /* Check extension is supposed to be relocatable */
2866  if (!extForm->extrelocatable)
2867  ereport(ERROR,
2868  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2869  errmsg("extension \"%s\" does not support SET SCHEMA",
2870  NameStr(extForm->extname))));
2871 
2872  objsMoved = new_object_addresses();
2873 
2874  /*
2875  * Scan pg_depend to find objects that depend directly on the extension,
2876  * and alter each one's schema.
2877  */
2878  depRel = table_open(DependRelationId, AccessShareLock);
2879 
2880  ScanKeyInit(&key[0],
2881  Anum_pg_depend_refclassid,
2882  BTEqualStrategyNumber, F_OIDEQ,
2883  ObjectIdGetDatum(ExtensionRelationId));
2884  ScanKeyInit(&key[1],
2885  Anum_pg_depend_refobjid,
2886  BTEqualStrategyNumber, F_OIDEQ,
2887  ObjectIdGetDatum(extensionOid));
2888 
2889  depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
2890  NULL, 2, key);
2891 
2892  while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
2893  {
2894  Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
2895  ObjectAddress dep;
2896  Oid dep_oldNspOid;
2897 
2898  /*
2899  * Ignore non-membership dependencies. (Currently, the only other
2900  * case we could see here is a normal dependency from another
2901  * extension.)
2902  */
2903  if (pg_depend->deptype != DEPENDENCY_EXTENSION)
2904  continue;
2905 
2906  dep.classId = pg_depend->classid;
2907  dep.objectId = pg_depend->objid;
2908  dep.objectSubId = pg_depend->objsubid;
2909 
2910  if (dep.objectSubId != 0) /* should not happen */
2911  elog(ERROR, "extension should not have a sub-object dependency");
2912 
2913  /* Relocate the object */
2914  dep_oldNspOid = AlterObjectNamespace_oid(dep.classId,
2915  dep.objectId,
2916  nspOid,
2917  objsMoved);
2918 
2919  /*
2920  * Remember previous namespace of first object that has one
2921  */
2922  if (oldNspOid == InvalidOid && dep_oldNspOid != InvalidOid)
2923  oldNspOid = dep_oldNspOid;
2924 
2925  /*
2926  * If not all the objects had the same old namespace (ignoring any
2927  * that are not in namespaces), complain.
2928  */
2929  if (dep_oldNspOid != InvalidOid && dep_oldNspOid != oldNspOid)
2930  ereport(ERROR,
2931  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2932  errmsg("extension \"%s\" does not support SET SCHEMA",
2933  NameStr(extForm->extname)),
2934  errdetail("%s is not in the extension's schema \"%s\"",
2935  getObjectDescription(&dep, false),
2936  get_namespace_name(oldNspOid))));
2937  }
2938 
2939  /* report old schema, if caller wants it */
2940  if (oldschema)
2941  *oldschema = oldNspOid;
2942 
2943  systable_endscan(depScan);
2944 
2946 
2947  /* Now adjust pg_extension.extnamespace */
2948  extForm->extnamespace = nspOid;
2949 
2950  CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
2951 
2952  table_close(extRel, RowExclusiveLock);
2953 
2954  /* update dependencies to point to the new schema */
2955  changeDependencyFor(ExtensionRelationId, extensionOid,
2956  NamespaceRelationId, oldNspOid, nspOid);
2957 
2958  InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
2959 
2960  ObjectAddressSet(extAddr, ExtensionRelationId, extensionOid);
2961 
2962  return extAddr;
2963 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:659
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
Oid GetUserId(void)
Definition: miscinit.c:450
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:2936
#define DependReferenceIndexId
Definition: indexing.h:156
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:610
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
unsigned int Oid
Definition: postgres_ext.h:31
AclResult pg_namespace_aclcheck(Oid nsp_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4625
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define ACL_CREATE
Definition: parsenodes.h:84
ItemPointerData t_self
Definition: htup.h:65
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
bool pg_extension_ownercheck(Oid ext_oid, Oid roleid)
Definition: aclchk.c:5183
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:957
FormData_pg_depend * Form_pg_depend
Definition: pg_depend.h:71
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
#define ExtensionOidIndexId
Definition: indexing.h:328
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:50
AclResult
Definition: acl.h:177
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:140
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:824
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:346
Oid AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: alter.c:589
#define elog(elevel,...)
Definition: elog.h:214
#define NameStr(name)
Definition: c.h:622
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ CreateExtension()

ObjectAddress CreateExtension ( ParseState pstate,
CreateExtensionStmt stmt 
)

Definition at line 1680 of file extension.c.

References check_valid_extension_name(), CreateExtensionInternal(), creating_extension, defGetBoolean(), defGetString(), DefElem::defname, elog, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errmsg(), ERROR, CreateExtensionStmt::extname, get_extension_oid(), CreateExtensionStmt::if_not_exists, InvalidObjectAddress, InvalidOid, lfirst, DefElem::location, NIL, NOTICE, CreateExtensionStmt::options, and parser_errposition().

Referenced by ProcessUtilitySlow().

1681 {
1682  DefElem *d_schema = NULL;
1683  DefElem *d_new_version = NULL;
1684  DefElem *d_cascade = NULL;
1685  char *schemaName = NULL;
1686  char *versionName = NULL;
1687  bool cascade = false;
1688  ListCell *lc;
1689 
1690  /* Check extension name validity before any filesystem access */
1692 
1693  /*
1694  * Check for duplicate extension name. The unique index on
1695  * pg_extension.extname would catch this anyway, and serves as a backstop
1696  * in case of race conditions; but this is a friendlier error message, and
1697  * besides we need a check to support IF NOT EXISTS.
1698  */
1699  if (get_extension_oid(stmt->extname, true) != InvalidOid)
1700  {
1701  if (stmt->if_not_exists)
1702  {
1703  ereport(NOTICE,
1705  errmsg("extension \"%s\" already exists, skipping",
1706  stmt->extname)));
1707  return InvalidObjectAddress;
1708  }
1709  else
1710  ereport(ERROR,
1712  errmsg("extension \"%s\" already exists",
1713  stmt->extname)));
1714  }
1715 
1716  /*
1717  * We use global variables to track the extension being created, so we can
1718  * create only one extension at the same time.
1719  */
1720  if (creating_extension)
1721  ereport(ERROR,
1722  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1723  errmsg("nested CREATE EXTENSION is not supported")));
1724 
1725  /* Deconstruct the statement option list */
1726  foreach(lc, stmt->options)
1727  {
1728  DefElem *defel = (DefElem *) lfirst(lc);
1729 
1730  if (strcmp(defel->defname, "schema") == 0)
1731  {
1732  if (d_schema)
1733  ereport(ERROR,
1734  (errcode(ERRCODE_SYNTAX_ERROR),
1735  errmsg("conflicting or redundant options"),
1736  parser_errposition(pstate, defel->location)));
1737  d_schema = defel;
1738  schemaName = defGetString(d_schema);
1739  }
1740  else if (strcmp(defel->defname, "new_version") == 0)
1741  {
1742  if (d_new_version)
1743  ereport(ERROR,
1744  (errcode(ERRCODE_SYNTAX_ERROR),
1745  errmsg("conflicting or redundant options"),
1746  parser_errposition(pstate, defel->location)));
1747  d_new_version = defel;
1748  versionName = defGetString(d_new_version);
1749  }
1750  else if (strcmp(defel->defname, "cascade") == 0)
1751  {
1752  if (d_cascade)
1753  ereport(ERROR,
1754  (errcode(ERRCODE_SYNTAX_ERROR),
1755  errmsg("conflicting or redundant options"),
1756  parser_errposition(pstate, defel->location)));
1757  d_cascade = defel;
1758  cascade = defGetBoolean(d_cascade);
1759  }
1760  else
1761  elog(ERROR, "unrecognized option: %s", defel->defname);
1762  }
1763 
1764  /* Call CreateExtensionInternal to do the real work. */
1765  return CreateExtensionInternal(stmt->extname,
1766  schemaName,
1767  versionName,
1768  cascade,
1769  NIL,
1770  true);
1771 }
#define NIL
Definition: pg_list.h:65
static void check_valid_extension_name(const char *extensionname)
Definition: extension.c:261
int errcode(int sqlerrcode)
Definition: elog.c:610
bool defGetBoolean(DefElem *def)
Definition: define.c:111
#define ERROR
Definition: elog.h:43
char * defGetString(DefElem *def)
Definition: define.c:49
int location
Definition: parsenodes.h:735
static ObjectAddress CreateExtensionInternal(char *extensionName, char *schemaName, const char *versionName, bool cascade, List *parents, bool is_create)
Definition: extension.c:1370
#define InvalidOid
Definition: postgres_ext.h:36
bool creating_extension
Definition: extension.c:71
#define ereport(elevel,...)
Definition: elog.h:144
#define NOTICE
Definition: elog.h:37
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:140
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
char * defname
Definition: parsenodes.h:732
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31

◆ ExecAlterExtensionContentsStmt()

ObjectAddress ExecAlterExtensionContentsStmt ( AlterExtensionContentsStmt stmt,
ObjectAddress objAddr 
)

Definition at line 3277 of file extension.c.

References aclcheck_error(), ACLCHECK_NOT_OWNER, AlterExtensionContentsStmt::action, Assert, check_object_ownership(), ObjectAddress::classId, deleteDependencyRecordsForClass(), DEPENDENCY_EXTENSION, elog, ereport, errcode(), errmsg(), ERROR, extension_config_remove(), AlterExtensionContentsStmt::extname, get_extension_name(), get_extension_oid(), get_extension_schema(), get_namespace_name(), get_object_address(), getExtensionOfObject(), getObjectDescription(), GetUserId(), InvokeObjectPostAlterHook, NoLock, AlterExtensionContentsStmt::object, OBJECT_DATABASE, OBJECT_EXTENSION, OBJECT_INDEX, OBJECT_PUBLICATION, OBJECT_ROLE, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABLESPACE, ObjectAddress::objectId, ObjectAddress::objectSubId, AlterExtensionContentsStmt::objtype, OidIsValid, pg_extension_ownercheck(), recordDependencyOn(), recordExtObjInitPriv(), relation_close(), removeExtObjInitPriv(), and ShareUpdateExclusiveLock.

Referenced by ProcessUtilitySlow().

3279 {
3280  ObjectAddress extension;
3281  ObjectAddress object;
3282  Relation relation;
3283  Oid oldExtension;
3284 
3285  switch (stmt->objtype)
3286  {
3287  case OBJECT_DATABASE:
3288  case OBJECT_EXTENSION:
3289  case OBJECT_INDEX:
3290  case OBJECT_PUBLICATION:
3291  case OBJECT_ROLE:
3292  case OBJECT_STATISTIC_EXT:
3293  case OBJECT_SUBSCRIPTION:
3294  case OBJECT_TABLESPACE:
3295  ereport(ERROR,
3296  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3297  errmsg("cannot add an object of this type to an extension")));
3298  break;
3299  default:
3300  /* OK */
3301  break;
3302  }
3303 
3304  extension.classId = ExtensionRelationId;
3305  extension.objectId = get_extension_oid(stmt->extname, false);
3306  extension.objectSubId = 0;
3307 
3308  /* Permission check: must own extension */
3309  if (!pg_extension_ownercheck(extension.objectId, GetUserId()))
3311  stmt->extname);
3312 
3313  /*
3314  * Translate the parser representation that identifies the object into an
3315  * ObjectAddress. get_object_address() will throw an error if the object
3316  * does not exist, and will also acquire a lock on the object to guard
3317  * against concurrent DROP and ALTER EXTENSION ADD/DROP operations.
3318  */
3319  object = get_object_address(stmt->objtype, stmt->object,
3320  &relation, ShareUpdateExclusiveLock, false);
3321 
3322  Assert(object.objectSubId == 0);
3323  if (objAddr)
3324  *objAddr = object;
3325 
3326  /* Permission check: must own target object, too */
3327  check_object_ownership(GetUserId(), stmt->objtype, object,
3328  stmt->object, relation);
3329 
3330  /*
3331  * Check existing extension membership.
3332  */
3333  oldExtension = getExtensionOfObject(object.classId, object.objectId);
3334 
3335  if (stmt->action > 0)
3336  {
3337  /*
3338  * ADD, so complain if object is already attached to some extension.
3339  */
3340  if (OidIsValid(oldExtension))
3341  ereport(ERROR,
3342  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3343  errmsg("%s is already a member of extension \"%s\"",
3344  getObjectDescription(&object, false),
3345  get_extension_name(oldExtension))));
3346 
3347  /*
3348  * Prevent a schema from being added to an extension if the schema
3349  * contains the extension. That would create a dependency loop.
3350  */
3351  if (object.classId == NamespaceRelationId &&
3352  object.objectId == get_extension_schema(extension.objectId))
3353  ereport(ERROR,
3354  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3355  errmsg("cannot add schema \"%s\" to extension \"%s\" "
3356  "because the schema contains the extension",
3357  get_namespace_name(object.objectId),
3358  stmt->extname)));
3359 
3360  /*
3361  * OK, add the dependency.
3362  */
3363  recordDependencyOn(&object, &extension, DEPENDENCY_EXTENSION);
3364 
3365  /*
3366  * Also record the initial ACL on the object, if any.
3367  *
3368  * Note that this will handle the object's ACLs, as well as any ACLs
3369  * on object subIds. (In other words, when the object is a table,
3370  * this will record the table's ACL and the ACLs for the columns on
3371  * the table, if any).
3372  */
3373  recordExtObjInitPriv(object.objectId, object.classId);
3374  }
3375  else
3376  {
3377  /*
3378  * DROP, so complain if it's not a member.
3379  */
3380  if (oldExtension != extension.objectId)
3381  ereport(ERROR,
3382  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3383  errmsg("%s is not a member of extension \"%s\"",
3384  getObjectDescription(&object, false),
3385  stmt->extname)));
3386 
3387  /*
3388  * OK, drop the dependency.
3389  */
3390  if (deleteDependencyRecordsForClass(object.classId, object.objectId,
3391  ExtensionRelationId,
3392  DEPENDENCY_EXTENSION) != 1)
3393  elog(ERROR, "unexpected number of extension dependency records");
3394 
3395  /*
3396  * If it's a relation, it might have an entry in the extension's
3397  * extconfig array, which we must remove.
3398  */
3399  if (object.classId == RelationRelationId)
3400  extension_config_remove(extension.objectId, object.objectId);
3401 
3402  /*
3403  * Remove all the initial ACLs, if any.
3404  *
3405  * Note that this will remove the object's ACLs, as well as any ACLs
3406  * on object subIds. (In other words, when the object is a table,
3407  * this will remove the table's ACL and the ACLs for the columns on
3408  * the table, if any).
3409  */
3410  removeExtObjInitPriv(object.objectId, object.classId);
3411  }
3412 
3413  InvokeObjectPostAlterHook(ExtensionRelationId, extension.objectId, 0);
3414 
3415  /*
3416  * If get_object_address() opened the relation for us, we close it to keep
3417  * the reference count correct - but we retain any locks acquired by
3418  * get_object_address() until commit time, to guard against concurrent
3419  * activity.
3420  */
3421  if (relation != NULL)
3422  relation_close(relation, NoLock);
3423 
3424  return extension;
3425 }
void recordExtObjInitPriv(Oid objoid, Oid classoid)
Definition: aclchk.c:5496
char * getObjectDescription(const ObjectAddress *object, bool missing_ok)
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:659
static Oid get_extension_schema(Oid ext_oid)
Definition: extension.c:224
Oid GetUserId(void)
Definition: miscinit.c:450
char * get_extension_name(Oid ext_oid)
Definition: extension.c:185
int errcode(int sqlerrcode)
Definition: elog.c:610
static void extension_config_remove(Oid extensionoid, Oid tableoid)
Definition: extension.c:2618
void recordDependencyOn(const ObjectAddress *depender, const ObjectAddress *referenced, DependencyType behavior)
Definition: pg_depend.c:43
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
#define ERROR
Definition: elog.h:43
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#define NoLock
Definition: lockdefs.h:34
bool pg_extension_ownercheck(Oid ext_oid, Oid roleid)
Definition: aclchk.c:5183
#define InvokeObjectPostAlterHook(classId, objectId, subId)
Definition: objectaccess.h:175
long deleteDependencyRecordsForClass(Oid classId, Oid objectId, Oid refclassId, char deptype)
Definition: pg_depend.c:240
#define ereport(elevel,...)
Definition: elog.h:144
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define Assert(condition)
Definition: c.h:745
ObjectAddress get_object_address(ObjectType objtype, Node *object, Relation *relp, LOCKMODE lockmode, bool missing_ok)
void check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, Node *object, Relation relation)
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:140
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
void removeExtObjInitPriv(Oid objoid, Oid classoid)
Definition: aclchk.c:5794

◆ ExecAlterExtensionStmt()

ObjectAddress ExecAlterExtensionStmt ( ParseState pstate,
AlterExtensionStmt stmt 
)

Definition at line 2969 of file extension.c.

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, ExtensionNameIndexId, AlterExtensionStmt::extname, GETSTRUCT, GetUserId(), heap_getattr, HeapTupleIsValid, identify_update_path(), InvalidObjectAddress, sort-test::key, lfirst, DefElem::location, NOTICE, OBJECT_EXTENSION, ObjectAddressSet, AlterExtensionStmt::options, parser_errposition(), pg_extension_ownercheck(), read_extension_control_file(), RelationGetDescr, ScanKeyInit(), strVal, systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), and text_to_cstring().

Referenced by ProcessUtilitySlow().

2970 {
2971  DefElem *d_new_version = NULL;
2972  char *versionName;
2973  char *oldVersionName;
2974  ExtensionControlFile *control;
2975  Oid extensionOid;
2976  Relation extRel;
2977  ScanKeyData key[1];
2978  SysScanDesc extScan;
2979  HeapTuple extTup;
2980  List *updateVersions;
2981  Datum datum;
2982  bool isnull;
2983  ListCell *lc;
2984  ObjectAddress address;
2985 
2986  /*
2987  * We use global variables to track the extension being created, so we can
2988  * create/update only one extension at the same time.
2989  */
2990  if (creating_extension)
2991  ereport(ERROR,
2992  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2993  errmsg("nested ALTER EXTENSION is not supported")));
2994 
2995  /*
2996  * Look up the extension --- it must already exist in pg_extension
2997  */
2998  extRel = table_open(ExtensionRelationId, AccessShareLock);
2999 
3000  ScanKeyInit(&key[0],
3001  Anum_pg_extension_extname,
3002  BTEqualStrategyNumber, F_NAMEEQ,
3003  CStringGetDatum(stmt->extname));
3004 
3005  extScan = systable_beginscan(extRel, ExtensionNameIndexId, true,
3006  NULL, 1, key);
3007 
3008  extTup = systable_getnext(extScan);
3009 
3010  if (!HeapTupleIsValid(extTup))
3011  ereport(ERROR,
3012  (errcode(ERRCODE_UNDEFINED_OBJECT),
3013  errmsg("extension \"%s\" does not exist",
3014  stmt->extname)));
3015 
3016  extensionOid = ((Form_pg_extension) GETSTRUCT(extTup))->oid;
3017 
3018  /*
3019  * Determine the existing version we are updating from
3020  */
3021  datum = heap_getattr(extTup, Anum_pg_extension_extversion,
3022  RelationGetDescr(extRel), &isnull);
3023  if (isnull)
3024  elog(ERROR, "extversion is null");
3025  oldVersionName = text_to_cstring(DatumGetTextPP(datum));
3026 
3027  systable_endscan(extScan);
3028 
3029  table_close(extRel, AccessShareLock);
3030 
3031  /* Permission check: must own extension */
3032  if (!pg_extension_ownercheck(extensionOid, GetUserId()))
3034  stmt->extname);
3035 
3036  /*
3037  * Read the primary control file. Note we assume that it does not contain
3038  * any non-ASCII data, so there is no need to worry about encoding at this
3039  * point.
3040  */
3041  control = read_extension_control_file(stmt->extname);
3042 
3043  /*
3044  * Read the statement option list
3045  */
3046  foreach(lc, stmt->options)
3047  {
3048  DefElem *defel = (DefElem *) lfirst(lc);
3049 
3050  if (strcmp(defel->defname, "new_version") == 0)
3051  {
3052  if (d_new_version)
3053  ereport(ERROR,
3054  (errcode(ERRCODE_SYNTAX_ERROR),
3055  errmsg("conflicting or redundant options"),
3056  parser_errposition(pstate, defel->location)));
3057  d_new_version = defel;
3058  }
3059  else
3060  elog(ERROR, "unrecognized option: %s", defel->defname);
3061  }
3062 
3063  /*
3064  * Determine the version to update to
3065  */
3066  if (d_new_version && d_new_version->arg)
3067  versionName = strVal(d_new_version->arg);
3068  else if (control->default_version)
3069  versionName = control->default_version;
3070  else
3071  {
3072  ereport(ERROR,
3073  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3074  errmsg("version to install must be specified")));
3075  versionName = NULL; /* keep compiler quiet */
3076  }
3077  check_valid_version_name(versionName);
3078 
3079  /*
3080  * If we're already at that version, just say so
3081  */
3082  if (strcmp(oldVersionName, versionName) == 0)
3083  {
3084  ereport(NOTICE,
3085  (errmsg("version \"%s\" of extension \"%s\" is already installed",
3086  versionName, stmt->extname)));
3087  return InvalidObjectAddress;
3088  }
3089 
3090  /*
3091  * Identify the series of update script files we need to execute
3092  */
3093  updateVersions = identify_update_path(control,
3094  oldVersionName,
3095  versionName);
3096 
3097  /*
3098  * Update the pg_extension row and execute the update scripts, one at a
3099  * time
3100  */
3101  ApplyExtensionUpdates(extensionOid, control,
3102  oldVersionName, updateVersions,
3103  NULL, false, false);
3104 
3105  ObjectAddressSet(address, ExtensionRelationId, extensionOid);
3106 
3107  return address;
3108 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define ExtensionNameIndexId
Definition: indexing.h:330
static ExtensionControlFile * read_extension_control_file(const char *extname)
Definition: extension.c:616
#define RelationGetDescr(relation)
Definition: rel.h:482
Oid GetUserId(void)
Definition: miscinit.c:450
#define DatumGetTextPP(X)
Definition: fmgr.h:291
static void ApplyExtensionUpdates(Oid extensionOid, ExtensionControlFile *pcontrol, const char *initialVersion, List *updateVersions, char *origSchemaName, bool cascade, bool is_create)
Definition: extension.c:3119
#define AccessShareLock
Definition: lockdefs.h:36
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:610
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3294
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ERROR
Definition: elog.h:43
static List * identify_update_path(ExtensionControlFile *control, const char *oldVersion, const char *newVersion)
Definition: extension.c:1179
bool pg_extension_ownercheck(Oid ext_oid, Oid roleid)
Definition: aclchk.c:5183
int location
Definition: parsenodes.h:735
#define CStringGetDatum(X)
Definition: postgres.h:578
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:50
Node * arg
Definition: parsenodes.h:733
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
char * default_version
Definition: extension.c:81
uintptr_t Datum
Definition: postgres.h:367
static void check_valid_version_name(const char *versionname)
Definition: extension.c:308
bool creating_extension
Definition: extension.c:71
#define ereport(elevel,...)
Definition: elog.h:144
#define NOTICE
Definition: elog.h:37
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:190
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:110
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
char * text_to_cstring(const text *t)
Definition: varlena.c:205
const ObjectAddress InvalidObjectAddress
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
char * defname
Definition: parsenodes.h:732
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ extension_file_exists()

bool extension_file_exists ( const char *  extensionName)

Definition at line 2237 of file extension.c.

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

Referenced by CreateFunction(), and ExecuteDoStmt().

2238 {
2239  bool result = false;
2240  char *location;
2241  DIR *dir;
2242  struct dirent *de;
2243 
2244  location = get_extension_control_directory();
2245  dir = AllocateDir(location);
2246 
2247  /*
2248  * If the control directory doesn't exist, we want to silently return
2249  * false. Any other error will be reported by ReadDir.
2250  */
2251  if (dir == NULL && errno == ENOENT)
2252  {
2253  /* do nothing */
2254  }
2255  else
2256  {
2257  while ((de = ReadDir(dir, location)) != NULL)
2258  {
2259  char *extname;
2260 
2262  continue;
2263 
2264  /* extract extension name from 'name.control' filename */
2265  extname = pstrdup(de->d_name);
2266  *strrchr(extname, '.') = '\0';
2267 
2268  /* ignore it if it's an auxiliary control file */
2269  if (strstr(extname, "--"))
2270  continue;
2271 
2272  /* done if it matches request */
2273  if (strcmp(extname, extensionName) == 0)
2274  {
2275  result = true;
2276  break;
2277  }
2278  }
2279 
2280  FreeDir(dir);
2281  }
2282 
2283  return result;
2284 }
char * pstrdup(const char *in)
Definition: mcxt.c:1186
Definition: dirent.h:9
Definition: dirent.c:25
static bool is_extension_control_filename(const char *filename)
Definition: extension.c:355
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
char d_name[MAX_PATH]
Definition: dirent.h:14
int FreeDir(DIR *dir)
Definition: fd.c:2701
static char * get_extension_control_directory(void)
Definition: extension.c:371

◆ get_extension_name()

char* get_extension_name ( Oid  ext_oid)

Definition at line 185 of file extension.c.

References AccessShareLock, BTEqualStrategyNumber, ExtensionOidIndexId, GETSTRUCT, HeapTupleIsValid, NameStr, ObjectIdGetDatum, pstrdup(), ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by ExecAlterExtensionContentsStmt(), getObjectDescription(), getObjectIdentityParts(), recordDependencyOnCurrentExtension(), and RemoveExtensionById().

186 {
187  char *result;
188  Relation rel;
189  SysScanDesc scandesc;
190  HeapTuple tuple;
191  ScanKeyData entry[1];
192 
193  rel = table_open(ExtensionRelationId, AccessShareLock);
194 
195  ScanKeyInit(&entry[0],
196  Anum_pg_extension_oid,
197  BTEqualStrategyNumber, F_OIDEQ,
198  ObjectIdGetDatum(ext_oid));
199 
200  scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
201  NULL, 1, entry);
202 
203  tuple = systable_getnext(scandesc);
204 
205  /* We assume that there can be at most one matching tuple */
206  if (HeapTupleIsValid(tuple))
207  result = pstrdup(NameStr(((Form_pg_extension) GETSTRUCT(tuple))->extname));
208  else
209  result = NULL;
210 
211  systable_endscan(scandesc);
212 
214 
215  return result;
216 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
char * pstrdup(const char *in)
Definition: mcxt.c:1186
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ExtensionOidIndexId
Definition: indexing.h:328
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:50
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define NameStr(name)
Definition: c.h:622
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ get_extension_oid()

Oid get_extension_oid ( const char *  extname,
bool  missing_ok 
)

Definition at line 140 of file extension.c.

References AccessShareLock, BTEqualStrategyNumber, CStringGetDatum, ereport, errcode(), errmsg(), ERROR, ExtensionNameIndexId, GETSTRUCT, HeapTupleIsValid, InvalidOid, OidIsValid, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

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

141 {
142  Oid result;
143  Relation rel;
144  SysScanDesc scandesc;
145  HeapTuple tuple;
146  ScanKeyData entry[1];
147 
148  rel = table_open(ExtensionRelationId, AccessShareLock);
149 
150  ScanKeyInit(&entry[0],
151  Anum_pg_extension_extname,
152  BTEqualStrategyNumber, F_NAMEEQ,
153  CStringGetDatum(extname));
154 
155  scandesc = systable_beginscan(rel, ExtensionNameIndexId, true,
156  NULL, 1, entry);
157 
158  tuple = systable_getnext(scandesc);
159 
160  /* We assume that there can be at most one matching tuple */
161  if (HeapTupleIsValid(tuple))
162  result = ((Form_pg_extension) GETSTRUCT(tuple))->oid;
163  else
164  result = InvalidOid;
165 
166  systable_endscan(scandesc);
167 
169 
170  if (!OidIsValid(result) && !missing_ok)
171  ereport(ERROR,
172  (errcode(ERRCODE_UNDEFINED_OBJECT),
173  errmsg("extension \"%s\" does not exist",
174  extname)));
175 
176  return result;
177 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define ExtensionNameIndexId
Definition: indexing.h:330
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:610
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:651
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
FormData_pg_extension * Form_pg_extension
Definition: pg_extension.h:50
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:824
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ InsertExtensionTuple()

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

Definition at line 1787 of file extension.c.

References add_exact_object_address(), BoolGetDatum, CatalogTupleInsert(), CStringGetDatum, CStringGetTextDatum, DEPENDENCY_NORMAL, DirectFunctionCall1, ExtensionOidIndexId, 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().

1791 {
1792  Oid extensionOid;
1793  Relation rel;
1794  Datum values[Natts_pg_extension];
1795  bool nulls[Natts_pg_extension];
1796  HeapTuple tuple;
1797  ObjectAddress myself;
1798  ObjectAddress nsp;
1799  ObjectAddresses *refobjs;
1800  ListCell *lc;
1801 
1802  /*
1803  * Build and insert the pg_extension tuple
1804  */
1805  rel = table_open(ExtensionRelationId, RowExclusiveLock);
1806 
1807  memset(values, 0, sizeof(values));
1808  memset(nulls, 0, sizeof(nulls));
1809 
1810  extensionOid = GetNewOidWithIndex(rel, ExtensionOidIndexId,
1811  Anum_pg_extension_oid);
1812  values[Anum_pg_extension_oid - 1] = ObjectIdGetDatum(extensionOid);
1813  values[Anum_pg_extension_extname - 1] =
1815  values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extOwner);
1816  values[Anum_pg_extension_extnamespace - 1] = ObjectIdGetDatum(schemaOid);
1817  values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(relocatable);
1818  values[Anum_pg_extension_extversion - 1] = CStringGetTextDatum(extVersion);
1819 
1820  if (extConfig == PointerGetDatum(NULL))
1821  nulls[Anum_pg_extension_extconfig - 1] = true;
1822  else
1823  values[Anum_pg_extension_extconfig - 1] = extConfig;
1824 
1825  if (extCondition == PointerGetDatum(NULL))
1826  nulls[Anum_pg_extension_extcondition - 1] = true;
1827  else
1828  values[Anum_pg_extension_extcondition - 1] = extCondition;
1829 
1830  tuple = heap_form_tuple(rel->rd_att, values, nulls);
1831 
1832  CatalogTupleInsert(rel, tuple);
1833 
1834  heap_freetuple(tuple);
1836 
1837  /*
1838  * Record dependencies on owner, schema, and prerequisite extensions
1839  */
1840  recordDependencyOnOwner(ExtensionRelationId, extensionOid, extOwner);
1841 
1842  refobjs = new_object_addresses();
1843 
1844  ObjectAddressSet(myself, ExtensionRelationId, extensionOid);
1845 
1846  ObjectAddressSet(nsp, NamespaceRelationId, schemaOid);
1847  add_exact_object_address(&nsp, refobjs);
1848 
1849  foreach(lc, requiredExtensions)
1850  {
1851  Oid reqext = lfirst_oid(lc);
1852  ObjectAddress otherext;
1853 
1854  ObjectAddressSet(otherext, ExtensionRelationId, reqext);
1855  add_exact_object_address(&otherext, refobjs);
1856  }
1857 
1858  /* Record all of them (this includes duplicate elimination) */
1860  free_object_addresses(refobjs);
1861 
1862  /* Post creation hook for new extension */
1863  InvokeObjectPostCreateHook(ExtensionRelationId, extensionOid, 0);
1864 
1865  return myself;
1866 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:317
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define InvokeObjectPostCreateHook(classId, objectId, subId)
Definition: objectaccess.h:151
#define PointerGetDatum(X)
Definition: postgres.h:556
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2676
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2467
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
Definition: pg_shdepend.c:164
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2412
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2707
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define RowExclusiveLock
Definition: lockdefs.h:38
#define CStringGetDatum(X)
Definition: postgres.h:578
#define ExtensionOidIndexId
Definition: indexing.h:328
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:110
#define BoolGetDatum(X)
Definition: postgres.h:402
#define ObjectAddressSet(addr, class_id, object_id)
Definition: objectaddress.h:40
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define CStringGetTextDatum(s)
Definition: builtins.h:86
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
#define lfirst_oid(lc)
Definition: pg_list.h:192

◆ RemoveExtensionById()

void RemoveExtensionById ( Oid  extId)

Definition at line 1875 of file extension.c.

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

Referenced by doDeletion().

1876 {
1877  Relation rel;
1878  SysScanDesc scandesc;
1879  HeapTuple tuple;
1880  ScanKeyData entry[1];
1881 
1882  /*
1883  * Disallow deletion of any extension that's currently open for insertion;
1884  * else subsequent executions of recordDependencyOnCurrentExtension()
1885  * could create dangling pg_depend records that refer to a no-longer-valid
1886  * pg_extension OID. This is needed not so much because we think people
1887  * might write "DROP EXTENSION foo" in foo's own script files, as because
1888  * errors in dependency management in extension script files could give
1889  * rise to cases where an extension is dropped as a result of recursing
1890  * from some contained object. Because of that, we must test for the case
1891  * here, not at some higher level of the DROP EXTENSION command.
1892  */
1893  if (extId == CurrentExtensionObject)
1894  ereport(ERROR,
1895  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1896  errmsg("cannot drop extension \"%s\" because it is being modified",
1897  get_extension_name(extId))));
1898 
1899  rel = table_open(ExtensionRelationId, RowExclusiveLock);
1900 
1901  ScanKeyInit(&entry[0],
1902  Anum_pg_extension_oid,
1903  BTEqualStrategyNumber, F_OIDEQ,
1904  ObjectIdGetDatum(extId));
1905  scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
1906  NULL, 1, entry);
1907 
1908  tuple = systable_getnext(scandesc);
1909 
1910  /* We assume that there can be at most one matching tuple */
1911  if (HeapTupleIsValid(tuple))
1912  CatalogTupleDelete(rel, &tuple->t_self);
1913 
1914  systable_endscan(scandesc);
1915 
1917 }
Oid CurrentExtensionObject
Definition: extension.c:72
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:569
char * get_extension_name(Oid ext_oid)
Definition: extension.c:185
int errcode(int sqlerrcode)
Definition: elog.c:610
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:350
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:357
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:476
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ExtensionOidIndexId
Definition: indexing.h:328
#define ereport(elevel,...)
Definition: elog.h:144
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:824
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

Variable Documentation

◆ creating_extension

◆ CurrentExtensionObject