@@ -522,7 +522,7 @@ ObjectTypeMap[] =
522522 /* OCLASS_USER_MAPPING */
523523 { "user mapping" , OBJECT_USER_MAPPING },
524524 /* OCLASS_DEFACL */
525- { "default acl" , -1 }, /* unmapped */
525+ { "default acl" , OBJECT_DEFACL },
526526 /* OCLASS_EXTENSION */
527527 { "extension" , OBJECT_EXTENSION },
528528 /* OCLASS_EVENT_TRIGGER */
@@ -557,6 +557,8 @@ static ObjectAddress get_object_address_opcf(ObjectType objtype, List *objname,
557557 List * objargs , bool missing_ok );
558558static ObjectAddress get_object_address_usermapping (List * objname ,
559559 List * objargs , bool missing_ok );
560+ static ObjectAddress get_object_address_defacl (List * objname , List * objargs ,
561+ bool missing_ok );
560562static const ObjectPropertyType * get_object_property_data (Oid class_id );
561563
562564static void getRelationDescription (StringInfo buffer , Oid relid );
@@ -775,6 +777,10 @@ get_object_address(ObjectType objtype, List *objname, List *objargs,
775777 address = get_object_address_usermapping (objname , objargs ,
776778 missing_ok );
777779 break ;
780+ case OBJECT_DEFACL :
781+ address = get_object_address_defacl (objname , objargs ,
782+ missing_ok );
783+ break ;
778784 default :
779785 elog (ERROR , "unrecognized objtype: %d" , (int ) objtype );
780786 /* placate compiler, in case it thinks elog might return */
@@ -1447,6 +1453,113 @@ get_object_address_usermapping(List *objname, List *objargs, bool missing_ok)
14471453 return address ;
14481454}
14491455
1456+ /*
1457+ * Find the ObjectAddress for a default ACL.
1458+ */
1459+ static ObjectAddress
1460+ get_object_address_defacl (List * objname , List * objargs , bool missing_ok )
1461+ {
1462+ HeapTuple tp ;
1463+ Oid userid ;
1464+ Oid schemaid ;
1465+ char * username ;
1466+ char * schema ;
1467+ char objtype ;
1468+ char * objtype_str ;
1469+ ObjectAddress address ;
1470+
1471+ ObjectAddressSet (address , DefaultAclRelationId , InvalidOid );
1472+
1473+ /*
1474+ * First figure out the textual attributes so that they can be used for
1475+ * error reporting.
1476+ */
1477+ username = strVal (linitial (objname ));
1478+ if (list_length (objname ) >= 2 )
1479+ schema = (char * ) strVal (lsecond (objname ));
1480+ else
1481+ schema = NULL ;
1482+
1483+ /*
1484+ * Decode defaclobjtype. Only first char is considered; the rest of the
1485+ * string, if any, is blissfully ignored.
1486+ */
1487+ objtype = ((char * ) strVal (linitial (objargs )))[0 ];
1488+ switch (objtype )
1489+ {
1490+ case DEFACLOBJ_RELATION :
1491+ objtype_str = "tables" ;
1492+ break ;
1493+ case DEFACLOBJ_SEQUENCE :
1494+ objtype_str = "sequences" ;
1495+ break ;
1496+ case DEFACLOBJ_FUNCTION :
1497+ objtype_str = "functions" ;
1498+ break ;
1499+ case DEFACLOBJ_TYPE :
1500+ objtype_str = "types" ;
1501+ break ;
1502+ default :
1503+ ereport (ERROR ,
1504+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1505+ errmsg ("unrecognized default ACL object type %c" , objtype ),
1506+ errhint ("Valid object types are 'r', 'S', 'f', and 'T'." )));
1507+ }
1508+
1509+ /*
1510+ * Look up user ID. Behave as "default ACL not found" if the user doesn't
1511+ * exist.
1512+ */
1513+ tp = SearchSysCache1 (AUTHNAME ,
1514+ CStringGetDatum (username ));
1515+ if (!HeapTupleIsValid (tp ))
1516+ goto not_found ;
1517+ userid = HeapTupleGetOid (tp );
1518+ ReleaseSysCache (tp );
1519+
1520+ /*
1521+ * If a schema name was given, look up its OID. If it doesn't exist,
1522+ * behave as "default ACL not found".
1523+ */
1524+ if (schema )
1525+ {
1526+ schemaid = get_namespace_oid (schema , true);
1527+ if (schemaid == InvalidOid )
1528+ goto not_found ;
1529+ }
1530+ else
1531+ schemaid = InvalidOid ;
1532+
1533+ /* Finally, look up the pg_default_acl object */
1534+ tp = SearchSysCache3 (DEFACLROLENSPOBJ ,
1535+ ObjectIdGetDatum (userid ),
1536+ ObjectIdGetDatum (schemaid ),
1537+ CharGetDatum (objtype ));
1538+ if (!HeapTupleIsValid (tp ))
1539+ goto not_found ;
1540+
1541+ address .objectId = HeapTupleGetOid (tp );
1542+ ReleaseSysCache (tp );
1543+
1544+ return address ;
1545+
1546+ not_found :
1547+ if (!missing_ok )
1548+ {
1549+ if (schema )
1550+ ereport (ERROR ,
1551+ (errcode (ERRCODE_UNDEFINED_OBJECT ),
1552+ errmsg ("default ACL for user \"%s\" in schema \"%s\" on %s does not exist" ,
1553+ username , schema , objtype_str )));
1554+ else
1555+ ereport (ERROR ,
1556+ (errcode (ERRCODE_UNDEFINED_OBJECT ),
1557+ errmsg ("default ACL for user \"%s\" on %s does not exist" ,
1558+ username , objtype_str )));
1559+ }
1560+ return address ;
1561+ }
1562+
14501563/*
14511564 * Convert an array of TEXT into a List of string Values, as emitted by the
14521565 * parser, which is what get_object_address uses as input.
@@ -1599,6 +1712,7 @@ pg_get_object_address(PG_FUNCTION_ARGS)
15991712 case OBJECT_OPFAMILY :
16001713 case OBJECT_CAST :
16011714 case OBJECT_USER_MAPPING :
1715+ case OBJECT_DEFACL :
16021716 if (list_length (args ) != 1 )
16031717 ereport (ERROR ,
16041718 (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -4024,10 +4138,8 @@ getObjectIdentityParts(const ObjectAddress *object,
40244138 SysScanDesc rcscan ;
40254139 HeapTuple tup ;
40264140 Form_pg_default_acl defacl ;
4027-
4028- /* no objname support */
4029- if (objname )
4030- * objname = NIL ;
4141+ char * schema ;
4142+ char * username ;
40314143
40324144 defaclrel = heap_open (DefaultAclRelationId , AccessShareLock );
40334145
@@ -4047,19 +4159,20 @@ getObjectIdentityParts(const ObjectAddress *object,
40474159
40484160 defacl = (Form_pg_default_acl ) GETSTRUCT (tup );
40494161
4162+ username = GetUserNameFromId (defacl -> defaclrole );
40504163 appendStringInfo (& buffer ,
40514164 "for role %s" ,
4052- quote_identifier (GetUserNameFromId ( defacl -> defaclrole ) ));
4165+ quote_identifier (username ));
40534166
40544167 if (OidIsValid (defacl -> defaclnamespace ))
40554168 {
4056- char * schema ;
4057-
40584169 schema = get_namespace_name (defacl -> defaclnamespace );
40594170 appendStringInfo (& buffer ,
40604171 " in schema %s" ,
40614172 quote_identifier (schema ));
40624173 }
4174+ else
4175+ schema = NULL ;
40634176
40644177 switch (defacl -> defaclobjtype )
40654178 {
@@ -4081,6 +4194,14 @@ getObjectIdentityParts(const ObjectAddress *object,
40814194 break ;
40824195 }
40834196
4197+ if (objname )
4198+ {
4199+ * objname = list_make1 (username );
4200+ if (schema )
4201+ * objname = lappend (* objname , schema );
4202+ * objargs = list_make1 (psprintf ("%c" , defacl -> defaclobjtype ));
4203+ }
4204+
40844205 systable_endscan (rcscan );
40854206 heap_close (defaclrel , AccessShareLock );
40864207 break ;
0 commit comments