1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

Add pg_get_acl() to get the ACL for a database object

This function returns the ACL for a database object, specified by
catalog OID and object OID.  This is useful to be able to
retrieve the ACL associated to an object specified with a
(class_id,objid) couple, similarly to the other functions for object
identification, when joined with pg_depend or pg_shdepend.

Original idea by Álvaro Herrera.

Bump catalog version.

Author: Joel Jacobson
Reviewed-by: Isaac Morland, Michael Paquier, Ranier Vilela
Discussion: https://postgr.es/m/80b16434-b9b1-4c3d-8f28-569f21c2c102@app.fastmail.com
This commit is contained in:
Michael Paquier
2024-07-04 17:09:06 +09:00
parent 3a8a1f3254
commit 4564f1cebd
6 changed files with 130 additions and 1 deletions

View File

@@ -4362,6 +4362,54 @@ pg_identify_object_as_address(PG_FUNCTION_ARGS)
PG_RETURN_DATUM(HeapTupleGetDatum(htup));
}
/*
* SQL-level callable function to obtain the ACL of a specified object, given
* its catalog OID and object OID.
*/
Datum
pg_get_acl(PG_FUNCTION_ARGS)
{
Oid classId = PG_GETARG_OID(0);
Oid objectId = PG_GETARG_OID(1);
Oid catalogId;
AttrNumber Anum_acl;
Relation rel;
HeapTuple tup;
Datum datum;
bool isnull;
/* for "pinned" items in pg_depend, return null */
if (!OidIsValid(classId) && !OidIsValid(objectId))
PG_RETURN_NULL();
/* for large objects, the catalog to look at is pg_largeobject_metadata */
catalogId = (classId == LargeObjectRelationId) ?
LargeObjectMetadataRelationId : classId;
Anum_acl = get_object_attnum_acl(catalogId);
/* return NULL if no ACL field for this catalog */
if (Anum_acl == InvalidAttrNumber)
PG_RETURN_NULL();
rel = table_open(catalogId, AccessShareLock);
tup = get_catalog_object_by_oid(rel, get_object_attnum_oid(catalogId),
objectId);
if (!HeapTupleIsValid(tup))
{
table_close(rel, AccessShareLock);
PG_RETURN_NULL();
}
datum = heap_getattr(tup, Anum_acl, RelationGetDescr(rel), &isnull);
table_close(rel, AccessShareLock);
if (isnull)
PG_RETURN_NULL();
PG_RETURN_DATUM(datum);
}
/*
* Return a palloc'ed string that describes the type of object that the
* passed address is for.