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 bool creating_extension
 
PGDLLIMPORT Oid CurrentExtensionObject
 

Function Documentation

◆ AlterExtensionNamespace()

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

Definition at line 2772 of file extension.c.

2773 {
2774  Oid extensionOid;
2775  Oid nspOid;
2776  Oid oldNspOid;
2777  AclResult aclresult;
2778  Relation extRel;
2779  ScanKeyData key[2];
2780  SysScanDesc extScan;
2781  HeapTuple extTup;
2782  Form_pg_extension extForm;
2783  Relation depRel;
2784  SysScanDesc depScan;
2785  HeapTuple depTup;
2786  ObjectAddresses *objsMoved;
2787  ObjectAddress extAddr;
2788 
2789  extensionOid = get_extension_oid(extensionName, false);
2790 
2791  nspOid = LookupCreationNamespace(newschema);
2792 
2793  /*
2794  * Permission check: must own extension. Note that we don't bother to
2795  * check ownership of the individual member objects ...
2796  */
2797  if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
2799  extensionName);
2800 
2801  /* Permission check: must have creation rights in target namespace */
2802  aclresult = object_aclcheck(NamespaceRelationId, nspOid, GetUserId(), ACL_CREATE);
2803  if (aclresult != ACLCHECK_OK)
2804  aclcheck_error(aclresult, OBJECT_SCHEMA, newschema);
2805 
2806  /*
2807  * If the schema is currently a member of the extension, disallow moving
2808  * the extension into the schema. That would create a dependency loop.
2809  */
2810  if (getExtensionOfObject(NamespaceRelationId, nspOid) == extensionOid)
2811  ereport(ERROR,
2812  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2813  errmsg("cannot move extension \"%s\" into schema \"%s\" "
2814  "because the extension contains the schema",
2815  extensionName, newschema)));
2816 
2817  /* Locate the pg_extension tuple */
2818  extRel = table_open(ExtensionRelationId, RowExclusiveLock);
2819 
2820  ScanKeyInit(&key[0],
2821  Anum_pg_extension_oid,
2822  BTEqualStrategyNumber, F_OIDEQ,
2823  ObjectIdGetDatum(extensionOid));
2824 
2825  extScan = systable_beginscan(extRel, ExtensionOidIndexId, true,
2826  NULL, 1, key);
2827 
2828  extTup = systable_getnext(extScan);
2829 
2830  if (!HeapTupleIsValid(extTup)) /* should not happen */
2831  elog(ERROR, "could not find tuple for extension %u",
2832  extensionOid);
2833 
2834  /* Copy tuple so we can modify it below */
2835  extTup = heap_copytuple(extTup);
2836  extForm = (Form_pg_extension) GETSTRUCT(extTup);
2837 
2838  systable_endscan(extScan);
2839 
2840  /*
2841  * If the extension is already in the target schema, just silently do
2842  * nothing.
2843  */
2844  if (extForm->extnamespace == nspOid)
2845  {
2846  table_close(extRel, RowExclusiveLock);
2847  return InvalidObjectAddress;
2848  }
2849 
2850  /* Check extension is supposed to be relocatable */
2851  if (!extForm->extrelocatable)
2852  ereport(ERROR,
2853  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2854  errmsg("extension \"%s\" does not support SET SCHEMA",
2855  NameStr(extForm->extname))));
2856 
2857  objsMoved = new_object_addresses();
2858 
2859  /* store the OID of the namespace to-be-changed */
2860  oldNspOid = extForm->extnamespace;
2861 
2862  /*
2863  * Scan pg_depend to find objects that depend directly on the extension,
2864  * and alter each one's schema.
2865  */
2866  depRel = table_open(DependRelationId, AccessShareLock);
2867 
2868  ScanKeyInit(&key[0],
2869  Anum_pg_depend_refclassid,
2870  BTEqualStrategyNumber, F_OIDEQ,
2871  ObjectIdGetDatum(ExtensionRelationId));
2872  ScanKeyInit(&key[1],
2873  Anum_pg_depend_refobjid,
2874  BTEqualStrategyNumber, F_OIDEQ,
2875  ObjectIdGetDatum(extensionOid));
2876 
2877  depScan = systable_beginscan(depRel, DependReferenceIndexId, true,
2878  NULL, 2, key);
2879 
2880  while (HeapTupleIsValid(depTup = systable_getnext(depScan)))
2881  {
2882  Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
2883  ObjectAddress dep;
2884  Oid dep_oldNspOid;
2885 
2886  /*
2887  * If a dependent extension has a no_relocate request for this
2888  * extension, disallow SET SCHEMA. (XXX it's a bit ugly to do this in
2889  * the same loop that's actually executing the renames: we may detect
2890  * the error condition only after having expended a fair amount of
2891  * work. However, the alternative is to do two scans of pg_depend,
2892  * which seems like optimizing for failure cases. The rename work
2893  * will all roll back cleanly enough if we do fail here.)
2894  */
2895  if (pg_depend->deptype == DEPENDENCY_NORMAL &&
2896  pg_depend->classid == ExtensionRelationId)
2897  {
2898  char *depextname = get_extension_name(pg_depend->objid);
2899  ExtensionControlFile *dcontrol;
2900  ListCell *lc;
2901 
2902  dcontrol = read_extension_control_file(depextname);
2903  foreach(lc, dcontrol->no_relocate)
2904  {
2905  char *nrextname = (char *) lfirst(lc);
2906 
2907  if (strcmp(nrextname, NameStr(extForm->extname)) == 0)
2908  {
2909  ereport(ERROR,
2910  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2911  errmsg("cannot SET SCHEMA of extension \"%s\" because other extensions prevent it",
2912  NameStr(extForm->extname)),
2913  errdetail("Extension \"%s\" requests no relocation of extension \"%s\".",
2914  depextname,
2915  NameStr(extForm->extname))));
2916  }
2917  }
2918  }
2919 
2920  /*
2921  * Otherwise, ignore non-membership dependencies. (Currently, the
2922  * only other case we could see here is a normal dependency from
2923  * another extension.)
2924  */
2925  if (pg_depend->deptype != DEPENDENCY_EXTENSION)
2926  continue;
2927 
2928  dep.classId = pg_depend->classid;
2929  dep.objectId = pg_depend->objid;
2930  dep.objectSubId = pg_depend->objsubid;
2931 
2932  if (dep.objectSubId != 0) /* should not happen */
2933  elog(ERROR, "extension should not have a sub-object dependency");
2934 
2935  /* Relocate the object */
2936  dep_oldNspOid = AlterObjectNamespace_oid(dep.classId,
2937  dep.objectId,
2938  nspOid,
2939  objsMoved);
2940 
2941  /*
2942  * If not all the objects had the same old namespace (ignoring any
2943  * that are not in namespaces or are dependent types), complain.
2944  */
2945  if (dep_oldNspOid != InvalidOid && dep_oldNspOid != oldNspOid)
2946  ereport(ERROR,
2947  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2948  errmsg("extension \"%s\" does not support SET SCHEMA",
2949  NameStr(extForm->extname)),
2950  errdetail("%s is not in the extension's schema \"%s\"",
2951  getObjectDescription(&dep, false),
2952  get_namespace_name(oldNspOid))));
2953  }
2954 
2955  /* report old schema, if caller wants it */
2956  if (oldschema)
2957  *oldschema = oldNspOid;
2958 
2959  systable_endscan(depScan);
2960 
2962 
2963  /* Now adjust pg_extension.extnamespace */
2964  extForm->extnamespace = nspOid;
2965 
2966  CatalogTupleUpdate(extRel, &extTup->t_self, extTup);
2967 
2968  table_close(extRel, RowExclusiveLock);
2969 
2970  /* update dependency to point to the new schema */
2971  if (changeDependencyFor(ExtensionRelationId, extensionOid,
2972  NamespaceRelationId, oldNspOid, nspOid) != 1)
2973  elog(ERROR, "could not change schema dependency for extension %s",
2974  NameStr(extForm->extname));
2975 
2976  InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
2977 
2978  ObjectAddressSet(extAddr, ExtensionRelationId, extensionOid);
2979 
2980  return extAddr;
2981 }
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:2702
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3890
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition: aclchk.c:4144
Oid AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid, ObjectAddresses *objsMoved)
Definition: alter.c:613
#define NameStr(name)
Definition: c.h:746
ObjectAddresses * new_object_addresses(void)
Definition: dependency.c:2485
@ DEPENDENCY_EXTENSION
Definition: dependency.h:38
@ DEPENDENCY_NORMAL
Definition: dependency.h:33
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
static ExtensionControlFile * read_extension_control_file(const char *extname)
Definition: extension.c:648
Oid get_extension_oid(const char *extname, bool missing_ok)
Definition: extension.c:145
char * get_extension_name(Oid ext_oid)
Definition: extension.c:190
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:776
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
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:3366
Oid GetUserId(void)
Definition: miscinit.c:514
Oid LookupCreationNamespace(const char *nspname)
Definition: namespace.c:3413
#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:2297
@ OBJECT_EXTENSION
Definition: parsenodes.h:2276
#define ACL_CREATE
Definition: parsenodes.h:85
long changeDependencyFor(Oid classId, Oid objectId, Oid refClassId, Oid oldRefObjectId, Oid newRefObjectId)
Definition: pg_depend.c:458
Oid getExtensionOfObject(Oid classId, Oid objectId)
Definition: pg_depend.c:733
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:252
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
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
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 1768 of file extension.c.

1769 {
1770  DefElem *d_schema = NULL;
1771  DefElem *d_new_version = NULL;
1772  DefElem *d_cascade = NULL;
1773  char *schemaName = NULL;
1774  char *versionName = NULL;
1775  bool cascade = false;
1776  ListCell *lc;
1777 
1778  /* Check extension name validity before any filesystem access */
1779  check_valid_extension_name(stmt->extname);
1780 
1781  /*
1782  * Check for duplicate extension name. The unique index on
1783  * pg_extension.extname would catch this anyway, and serves as a backstop
1784  * in case of race conditions; but this is a friendlier error message, and
1785  * besides we need a check to support IF NOT EXISTS.
1786  */
1787  if (get_extension_oid(stmt->extname, true) != InvalidOid)
1788  {
1789  if (stmt->if_not_exists)
1790  {
1791  ereport(NOTICE,
1793  errmsg("extension \"%s\" already exists, skipping",
1794  stmt->extname)));
1795  return InvalidObjectAddress;
1796  }
1797  else
1798  ereport(ERROR,
1800  errmsg("extension \"%s\" already exists",
1801  stmt->extname)));
1802  }
1803 
1804  /*
1805  * We use global variables to track the extension being created, so we can
1806  * create only one extension at the same time.
1807  */
1808  if (creating_extension)
1809  ereport(ERROR,
1810  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1811  errmsg("nested CREATE EXTENSION is not supported")));
1812 
1813  /* Deconstruct the statement option list */
1814  foreach(lc, stmt->options)
1815  {
1816  DefElem *defel = (DefElem *) lfirst(lc);
1817 
1818  if (strcmp(defel->defname, "schema") == 0)
1819  {
1820  if (d_schema)
1821  errorConflictingDefElem(defel, pstate);
1822  d_schema = defel;
1823  schemaName = defGetString(d_schema);
1824  }
1825  else if (strcmp(defel->defname, "new_version") == 0)
1826  {
1827  if (d_new_version)
1828  errorConflictingDefElem(defel, pstate);
1829  d_new_version = defel;
1830  versionName = defGetString(d_new_version);
1831  }
1832  else if (strcmp(defel->defname, "cascade") == 0)
1833  {
1834  if (d_cascade)
1835  errorConflictingDefElem(defel, pstate);
1836  d_cascade = defel;
1837  cascade = defGetBoolean(d_cascade);
1838  }
1839  else
1840  elog(ERROR, "unrecognized option: %s", defel->defname);
1841  }
1842 
1843  /* Call CreateExtensionInternal to do the real work. */
1844  return CreateExtensionInternal(stmt->extname,
1845  schemaName,
1846  versionName,
1847  cascade,
1848  NIL,
1849  true);
1850 }
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
void errorConflictingDefElem(DefElem *defel, ParseState *pstate)
Definition: define.c:384
#define NOTICE
Definition: elog.h:35
static void check_valid_extension_name(const char *extensionname)
Definition: extension.c:266
bool creating_extension
Definition: extension.c:71
static ObjectAddress CreateExtensionInternal(char *extensionName, char *schemaName, const char *versionName, bool cascade, List *parents, bool is_create)
Definition: extension.c:1458
#define stmt
Definition: indent_codes.h:59
#define NIL
Definition: pg_list.h:68
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
char * defname
Definition: parsenodes.h:815

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

3294 {
3295  ObjectAddress extension;
3296  ObjectAddress object;
3297  Relation relation;
3298 
3299  switch (stmt->objtype)
3300  {
3301  case OBJECT_DATABASE:
3302  case OBJECT_EXTENSION:
3303  case OBJECT_INDEX:
3304  case OBJECT_PUBLICATION:
3305  case OBJECT_ROLE:
3306  case OBJECT_STATISTIC_EXT:
3307  case OBJECT_SUBSCRIPTION:
3308  case OBJECT_TABLESPACE:
3309  ereport(ERROR,
3310  (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
3311  errmsg("cannot add an object of this type to an extension")));
3312  break;
3313  default:
3314  /* OK */
3315  break;
3316  }
3317 
3318  /*
3319  * Find the extension and acquire a lock on it, to ensure it doesn't get
3320  * dropped concurrently. A sharable lock seems sufficient: there's no
3321  * reason not to allow other sorts of manipulations, such as add/drop of
3322  * other objects, to occur concurrently. Concurrently adding/dropping the
3323  * *same* object would be bad, but we prevent that by using a non-sharable
3324  * lock on the individual object, below.
3325  */
3327  (Node *) makeString(stmt->extname),
3328  &relation, AccessShareLock, false);
3329 
3330  /* Permission check: must own extension */
3331  if (!object_ownercheck(ExtensionRelationId, extension.objectId, GetUserId()))
3333  stmt->extname);
3334 
3335  /*
3336  * Translate the parser representation that identifies the object into an
3337  * ObjectAddress. get_object_address() will throw an error if the object
3338  * does not exist, and will also acquire a lock on the object to guard
3339  * against concurrent DROP and ALTER EXTENSION ADD/DROP operations.
3340  */
3341  object = get_object_address(stmt->objtype, stmt->object,
3342  &relation, ShareUpdateExclusiveLock, false);
3343 
3344  Assert(object.objectSubId == 0);
3345  if (objAddr)
3346  *objAddr = object;
3347 
3348  /* Permission check: must own target object, too */
3349  check_object_ownership(GetUserId(), stmt->objtype, object,
3350  stmt->object, relation);
3351 
3352  /* Do the update, recursing to any dependent objects */
3353  ExecAlterExtensionContentsRecurse(stmt, extension, object);
3354 
3355  /* Finish up */
3356  InvokeObjectPostAlterHook(ExtensionRelationId, extension.objectId, 0);
3357 
3358  /*
3359  * If get_object_address() opened the relation for us, we close it to keep
3360  * the reference count correct - but we retain any locks acquired by
3361  * get_object_address() until commit time, to guard against concurrent
3362  * activity.
3363  */
3364  if (relation != NULL)
3365  relation_close(relation, NoLock);
3366 
3367  return extension;
3368 }
#define Assert(condition)
Definition: c.h:858
static void ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt, ObjectAddress extension, ObjectAddress object)
Definition: extension.c:3378
#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:2303
@ OBJECT_ROLE
Definition: parsenodes.h:2294
@ OBJECT_INDEX
Definition: parsenodes.h:2281
@ OBJECT_DATABASE
Definition: parsenodes.h:2270
@ OBJECT_PUBLICATION
Definition: parsenodes.h:2291
@ OBJECT_SUBSCRIPTION
Definition: parsenodes.h:2299
@ OBJECT_STATISTIC_EXT
Definition: parsenodes.h:2300
Definition: nodes.h:129
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 2987 of file extension.c.

2988 {
2989  DefElem *d_new_version = NULL;
2990  char *versionName;
2991  char *oldVersionName;
2992  ExtensionControlFile *control;
2993  Oid extensionOid;
2994  Relation extRel;
2995  ScanKeyData key[1];
2996  SysScanDesc extScan;
2997  HeapTuple extTup;
2998  List *updateVersions;
2999  Datum datum;
3000  bool isnull;
3001  ListCell *lc;
3002  ObjectAddress address;
3003 
3004  /*
3005  * We use global variables to track the extension being created, so we can
3006  * create/update only one extension at the same time.
3007  */
3008  if (creating_extension)
3009  ereport(ERROR,
3010  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3011  errmsg("nested ALTER EXTENSION is not supported")));
3012 
3013  /*
3014  * Look up the extension --- it must already exist in pg_extension
3015  */
3016  extRel = table_open(ExtensionRelationId, AccessShareLock);
3017 
3018  ScanKeyInit(&key[0],
3019  Anum_pg_extension_extname,
3020  BTEqualStrategyNumber, F_NAMEEQ,
3021  CStringGetDatum(stmt->extname));
3022 
3023  extScan = systable_beginscan(extRel, ExtensionNameIndexId, true,
3024  NULL, 1, key);
3025 
3026  extTup = systable_getnext(extScan);
3027 
3028  if (!HeapTupleIsValid(extTup))
3029  ereport(ERROR,
3030  (errcode(ERRCODE_UNDEFINED_OBJECT),
3031  errmsg("extension \"%s\" does not exist",
3032  stmt->extname)));
3033 
3034  extensionOid = ((Form_pg_extension) GETSTRUCT(extTup))->oid;
3035 
3036  /*
3037  * Determine the existing version we are updating from
3038  */
3039  datum = heap_getattr(extTup, Anum_pg_extension_extversion,
3040  RelationGetDescr(extRel), &isnull);
3041  if (isnull)
3042  elog(ERROR, "extversion is null");
3043  oldVersionName = text_to_cstring(DatumGetTextPP(datum));
3044 
3045  systable_endscan(extScan);
3046 
3047  table_close(extRel, AccessShareLock);
3048 
3049  /* Permission check: must own extension */
3050  if (!object_ownercheck(ExtensionRelationId, extensionOid, GetUserId()))
3052  stmt->extname);
3053 
3054  /*
3055  * Read the primary control file. Note we assume that it does not contain
3056  * any non-ASCII data, so there is no need to worry about encoding at this
3057  * point.
3058  */
3059  control = read_extension_control_file(stmt->extname);
3060 
3061  /*
3062  * Read the statement option list
3063  */
3064  foreach(lc, stmt->options)
3065  {
3066  DefElem *defel = (DefElem *) lfirst(lc);
3067 
3068  if (strcmp(defel->defname, "new_version") == 0)
3069  {
3070  if (d_new_version)
3071  errorConflictingDefElem(defel, pstate);
3072  d_new_version = defel;
3073  }
3074  else
3075  elog(ERROR, "unrecognized option: %s", defel->defname);
3076  }
3077 
3078  /*
3079  * Determine the version to update to
3080  */
3081  if (d_new_version && d_new_version->arg)
3082  versionName = strVal(d_new_version->arg);
3083  else if (control->default_version)
3084  versionName = control->default_version;
3085  else
3086  {
3087  ereport(ERROR,
3088  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3089  errmsg("version to install must be specified")));
3090  versionName = NULL; /* keep compiler quiet */
3091  }
3092  check_valid_version_name(versionName);
3093 
3094  /*
3095  * If we're already at that version, just say so
3096  */
3097  if (strcmp(oldVersionName, versionName) == 0)
3098  {
3099  ereport(NOTICE,
3100  (errmsg("version \"%s\" of extension \"%s\" is already installed",
3101  versionName, stmt->extname)));
3102  return InvalidObjectAddress;
3103  }
3104 
3105  /*
3106  * Identify the series of update script files we need to execute
3107  */
3108  updateVersions = identify_update_path(control,
3109  oldVersionName,
3110  versionName);
3111 
3112  /*
3113  * Update the pg_extension row and execute the update scripts, one at a
3114  * time
3115  */
3116  ApplyExtensionUpdates(extensionOid, control,
3117  oldVersionName, updateVersions,
3118  NULL, false, false);
3119 
3120  ObjectAddressSet(address, ExtensionRelationId, extensionOid);
3121 
3122  return address;
3123 }
static List * identify_update_path(ExtensionControlFile *control, const char *oldVersion, const char *newVersion)
Definition: extension.c:1267
static void check_valid_version_name(const char *versionname)
Definition: extension.c:313
static void ApplyExtensionUpdates(Oid extensionOid, ExtensionControlFile *pcontrol, const char *initialVersion, List *updateVersions, char *origSchemaName, bool cascade, bool is_create)
Definition: extension.c:3134
#define DatumGetTextPP(X)
Definition: fmgr.h:292
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:792
uintptr_t Datum
Definition: postgres.h:64
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
#define RelationGetDescr(relation)
Definition: rel.h:531
Node * arg
Definition: parsenodes.h:816
char * default_version
Definition: extension.c:81
Definition: pg_list.h:54
#define strVal(v)
Definition: value.h:82
char * text_to_cstring(const text *t)
Definition: varlena.c:217

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

2261 {
2262  bool result = false;
2263  char *location;
2264  DIR *dir;
2265  struct dirent *de;
2266 
2267  location = get_extension_control_directory();
2268  dir = AllocateDir(location);
2269 
2270  /*
2271  * If the control directory doesn't exist, we want to silently return
2272  * false. Any other error will be reported by ReadDir.
2273  */
2274  if (dir == NULL && errno == ENOENT)
2275  {
2276  /* do nothing */
2277  }
2278  else
2279  {
2280  while ((de = ReadDir(dir, location)) != NULL)
2281  {
2282  char *extname;
2283 
2285  continue;
2286 
2287  /* extract extension name from 'name.control' filename */
2288  extname = pstrdup(de->d_name);
2289  *strrchr(extname, '.') = '\0';
2290 
2291  /* ignore it if it's an auxiliary control file */
2292  if (strstr(extname, "--"))
2293  continue;
2294 
2295  /* done if it matches request */
2296  if (strcmp(extname, extensionName) == 0)
2297  {
2298  result = true;
2299  break;
2300  }
2301  }
2302 
2303  FreeDir(dir);
2304  }
2305 
2306  return result;
2307 }
static bool is_extension_control_filename(const char *filename)
Definition: extension.c:360
static char * get_extension_control_directory(void)
Definition: extension.c:376
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2909
int FreeDir(DIR *dir)
Definition: fd.c:2961
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2843
char * pstrdup(const char *in)
Definition: mcxt.c:1695
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References AllocateDir(), dirent::d_name, FreeDir(), get_extension_control_directory(), 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 190 of file extension.c.

191 {
192  char *result;
193  Relation rel;
194  SysScanDesc scandesc;
195  HeapTuple tuple;
196  ScanKeyData entry[1];
197 
198  rel = table_open(ExtensionRelationId, AccessShareLock);
199 
200  ScanKeyInit(&entry[0],
201  Anum_pg_extension_oid,
202  BTEqualStrategyNumber, F_OIDEQ,
203  ObjectIdGetDatum(ext_oid));
204 
205  scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
206  NULL, 1, entry);
207 
208  tuple = systable_getnext(scandesc);
209 
210  /* We assume that there can be at most one matching tuple */
211  if (HeapTupleIsValid(tuple))
212  result = pstrdup(NameStr(((Form_pg_extension) GETSTRUCT(tuple))->extname));
213  else
214  result = NULL;
215 
216  systable_endscan(scandesc);
217 
219 
220  return result;
221 }

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

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

146 {
147  Oid result;
148  Relation rel;
149  SysScanDesc scandesc;
150  HeapTuple tuple;
151  ScanKeyData entry[1];
152 
153  rel = table_open(ExtensionRelationId, AccessShareLock);
154 
155  ScanKeyInit(&entry[0],
156  Anum_pg_extension_extname,
157  BTEqualStrategyNumber, F_NAMEEQ,
158  CStringGetDatum(extname));
159 
160  scandesc = systable_beginscan(rel, ExtensionNameIndexId, true,
161  NULL, 1, entry);
162 
163  tuple = systable_getnext(scandesc);
164 
165  /* We assume that there can be at most one matching tuple */
166  if (HeapTupleIsValid(tuple))
167  result = ((Form_pg_extension) GETSTRUCT(tuple))->oid;
168  else
169  result = InvalidOid;
170 
171  systable_endscan(scandesc);
172 
174 
175  if (!OidIsValid(result) && !missing_ok)
176  ereport(ERROR,
177  (errcode(ERRCODE_UNDEFINED_OBJECT),
178  errmsg("extension \"%s\" does not exist",
179  extname)));
180 
181  return result;
182 }
#define OidIsValid(objectId)
Definition: c.h:775

References AccessShareLock, BTEqualStrategyNumber, CStringGetDatum(), ereport, errcode(), errmsg(), ERROR, 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(), ExtractExtensionList(), get_object_address_unqualified(), and get_required_extension().

◆ get_extension_schema()

Oid get_extension_schema ( Oid  ext_oid)

Definition at line 229 of file extension.c.

230 {
231  Oid result;
232  Relation rel;
233  SysScanDesc scandesc;
234  HeapTuple tuple;
235  ScanKeyData entry[1];
236 
237  rel = table_open(ExtensionRelationId, AccessShareLock);
238 
239  ScanKeyInit(&entry[0],
240  Anum_pg_extension_oid,
241  BTEqualStrategyNumber, F_OIDEQ,
242  ObjectIdGetDatum(ext_oid));
243 
244  scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
245  NULL, 1, entry);
246 
247  tuple = systable_getnext(scandesc);
248 
249  /* We assume that there can be at most one matching tuple */
250  if (HeapTupleIsValid(tuple))
251  result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
252  else
253  result = InvalidOid;
254 
255  systable_endscan(scandesc);
256 
258 
259  return result;
260 }

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

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

1870 {
1871  Oid extensionOid;
1872  Relation rel;
1873  Datum values[Natts_pg_extension];
1874  bool nulls[Natts_pg_extension];
1875  HeapTuple tuple;
1876  ObjectAddress myself;
1877  ObjectAddress nsp;
1878  ObjectAddresses *refobjs;
1879  ListCell *lc;
1880 
1881  /*
1882  * Build and insert the pg_extension tuple
1883  */
1884  rel = table_open(ExtensionRelationId, RowExclusiveLock);
1885 
1886  memset(values, 0, sizeof(values));
1887  memset(nulls, 0, sizeof(nulls));
1888 
1889  extensionOid = GetNewOidWithIndex(rel, ExtensionOidIndexId,
1890  Anum_pg_extension_oid);
1891  values[Anum_pg_extension_oid - 1] = ObjectIdGetDatum(extensionOid);
1892  values[Anum_pg_extension_extname - 1] =
1894  values[Anum_pg_extension_extowner - 1] = ObjectIdGetDatum(extOwner);
1895  values[Anum_pg_extension_extnamespace - 1] = ObjectIdGetDatum(schemaOid);
1896  values[Anum_pg_extension_extrelocatable - 1] = BoolGetDatum(relocatable);
1897  values[Anum_pg_extension_extversion - 1] = CStringGetTextDatum(extVersion);
1898 
1899  if (extConfig == PointerGetDatum(NULL))
1900  nulls[Anum_pg_extension_extconfig - 1] = true;
1901  else
1902  values[Anum_pg_extension_extconfig - 1] = extConfig;
1903 
1904  if (extCondition == PointerGetDatum(NULL))
1905  nulls[Anum_pg_extension_extcondition - 1] = true;
1906  else
1907  values[Anum_pg_extension_extcondition - 1] = extCondition;
1908 
1909  tuple = heap_form_tuple(rel->rd_att, values, nulls);
1910 
1911  CatalogTupleInsert(rel, tuple);
1912 
1913  heap_freetuple(tuple);
1915 
1916  /*
1917  * Record dependencies on owner, schema, and prerequisite extensions
1918  */
1919  recordDependencyOnOwner(ExtensionRelationId, extensionOid, extOwner);
1920 
1921  refobjs = new_object_addresses();
1922 
1923  ObjectAddressSet(myself, ExtensionRelationId, extensionOid);
1924 
1925  ObjectAddressSet(nsp, NamespaceRelationId, schemaOid);
1926  add_exact_object_address(&nsp, refobjs);
1927 
1928  foreach(lc, requiredExtensions)
1929  {
1930  Oid reqext = lfirst_oid(lc);
1931  ObjectAddress otherext;
1932 
1933  ObjectAddressSet(otherext, ExtensionRelationId, reqext);
1934  add_exact_object_address(&otherext, refobjs);
1935  }
1936 
1937  /* Record all of them (this includes duplicate elimination) */
1939  free_object_addresses(refobjs);
1940 
1941  /* Post creation hook for new extension */
1942  InvokeObjectPostCreateHook(ExtensionRelationId, extensionOid, 0);
1943 
1944  return myself;
1945 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define CStringGetTextDatum(s)
Definition: builtins.h:97
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:391
void record_object_address_dependencies(const ObjectAddress *depender, ObjectAddresses *referenced, DependencyType behavior)
Definition: dependency.c:2740
void add_exact_object_address(const ObjectAddress *object, ObjectAddresses *addrs)
Definition: dependency.c:2531
void free_object_addresses(ObjectAddresses *addrs)
Definition: dependency.c:2771
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
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:165
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
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 1954 of file extension.c.

1955 {
1956  Relation rel;
1957  SysScanDesc scandesc;
1958  HeapTuple tuple;
1959  ScanKeyData entry[1];
1960 
1961  /*
1962  * Disallow deletion of any extension that's currently open for insertion;
1963  * else subsequent executions of recordDependencyOnCurrentExtension()
1964  * could create dangling pg_depend records that refer to a no-longer-valid
1965  * pg_extension OID. This is needed not so much because we think people
1966  * might write "DROP EXTENSION foo" in foo's own script files, as because
1967  * errors in dependency management in extension script files could give
1968  * rise to cases where an extension is dropped as a result of recursing
1969  * from some contained object. Because of that, we must test for the case
1970  * here, not at some higher level of the DROP EXTENSION command.
1971  */
1972  if (extId == CurrentExtensionObject)
1973  ereport(ERROR,
1974  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1975  errmsg("cannot drop extension \"%s\" because it is being modified",
1976  get_extension_name(extId))));
1977 
1978  rel = table_open(ExtensionRelationId, RowExclusiveLock);
1979 
1980  ScanKeyInit(&entry[0],
1981  Anum_pg_extension_oid,
1982  BTEqualStrategyNumber, F_OIDEQ,
1983  ObjectIdGetDatum(extId));
1984  scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
1985  NULL, 1, entry);
1986 
1987  tuple = systable_getnext(scandesc);
1988 
1989  /* We assume that there can be at most one matching tuple */
1990  if (HeapTupleIsValid(tuple))
1991  CatalogTupleDelete(rel, &tuple->t_self);
1992 
1993  systable_endscan(scandesc);
1994 
1996 }
Oid CurrentExtensionObject
Definition: extension.c:72
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