@@ -1436,14 +1436,15 @@ AlterExtensionNamespace(List *names, const char *newschema)
1436
1436
}
1437
1437
1438
1438
/*
1439
- * Execute ALTER EXTENSION ADD
1439
+ * Execute ALTER EXTENSION ADD/DROP
1440
1440
*/
1441
1441
void
1442
- ExecAlterExtensionAddStmt ( AlterExtensionAddStmt * stmt )
1442
+ ExecAlterExtensionContentsStmt ( AlterExtensionContentsStmt * stmt )
1443
1443
{
1444
1444
ObjectAddress extension ;
1445
1445
ObjectAddress object ;
1446
1446
Relation relation ;
1447
+ Oid oldExtension ;
1447
1448
1448
1449
/*
1449
1450
* For now, insist on superuser privilege. Later we might want to
@@ -1462,25 +1463,54 @@ ExecAlterExtensionAddStmt(AlterExtensionAddStmt *stmt)
1462
1463
/*
1463
1464
* Translate the parser representation that identifies the object into
1464
1465
* an ObjectAddress. get_object_address() will throw an error if the
1465
- * object does not exist, and will also acquire a lock on the object
1466
- * to guard against concurrent DROP and ALTER EXTENSION ADD operations.
1466
+ * object does not exist, and will also acquire a lock on the object to
1467
+ * guard against concurrent DROP and ALTER EXTENSION ADD/DROP operations.
1467
1468
*/
1468
1469
object = get_object_address (stmt -> objtype , stmt -> objname , stmt -> objargs ,
1469
1470
& relation , ShareUpdateExclusiveLock );
1470
1471
1471
1472
/*
1472
- * Complain if object is already attached to some extension.
1473
+ * Check existing extension membership .
1473
1474
*/
1474
- if (getExtensionOfObject (object .classId , object .objectId ) != InvalidOid )
1475
- ereport (ERROR ,
1476
- (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1477
- errmsg ("%s is already a member of an extension" ,
1478
- getObjectDescription (& object ))));
1475
+ oldExtension = getExtensionOfObject (object .classId , object .objectId );
1479
1476
1480
- /*
1481
- * OK, add the dependency.
1482
- */
1483
- recordDependencyOn (& object , & extension , DEPENDENCY_EXTENSION );
1477
+ if (stmt -> action > 0 )
1478
+ {
1479
+ /*
1480
+ * ADD, so complain if object is already attached to some extension.
1481
+ */
1482
+ if (OidIsValid (oldExtension ))
1483
+ ereport (ERROR ,
1484
+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1485
+ errmsg ("%s is already a member of extension \"%s\"" ,
1486
+ getObjectDescription (& object ),
1487
+ get_extension_name (oldExtension ))));
1488
+
1489
+ /*
1490
+ * OK, add the dependency.
1491
+ */
1492
+ recordDependencyOn (& object , & extension , DEPENDENCY_EXTENSION );
1493
+ }
1494
+ else
1495
+ {
1496
+ /*
1497
+ * DROP, so complain if it's not a member.
1498
+ */
1499
+ if (oldExtension != extension .objectId )
1500
+ ereport (ERROR ,
1501
+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1502
+ errmsg ("%s is not a member of extension \"%s\"" ,
1503
+ getObjectDescription (& object ),
1504
+ stmt -> extname )));
1505
+
1506
+ /*
1507
+ * OK, drop the dependency.
1508
+ */
1509
+ if (deleteDependencyRecordsForClass (object .classId , object .objectId ,
1510
+ ExtensionRelationId ,
1511
+ DEPENDENCY_EXTENSION ) != 1 )
1512
+ elog (ERROR , "unexpected number of extension dependency records" );
1513
+ }
1484
1514
1485
1515
/*
1486
1516
* If get_object_address() opened the relation for us, we close it to keep
0 commit comments