Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p2TVW-0007gm-Fy for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Dec 2022 08:40:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1p2TVV-0007OY-CB for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Dec 2022 08:40:53 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p2TVU-0007OO-Uz for pgsql-hackers@lists.postgresql.org; Tue, 06 Dec 2022 08:40:53 +0000 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1p2TVR-0003Uz-QH for pgsql-hackers@postgresql.org; Tue, 06 Dec 2022 08:40:51 +0000 Received: by mail-ej1-x629.google.com with SMTP id kw15so995473ejc.10 for ; Tue, 06 Dec 2022 00:40:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:date:mime-version:comments:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=hNoTphmcSJs1VN1tcAn+slcP3GRzxNun1rZdImnoMoE=; b=7vxK4Wnsyuv5x4zMuiPIVEKquaGyC54h7Hf/Oztjl/QcBDExvb1m0KslUW+oDrmOuQ +XhIeri9E8+JyCQt27qrluSWw/a8F4cNBVJk/pnnEXHiUWuzcutkgm2Z0MJ61QTJhS8n q+LaFqWSF3EhpAa06rmYYV/JR4KPr9H40J8iXND5SVDyNlGtSLXCskDCUBJceZuMCcP6 kdLwegzNtC54i5aO0P9zjRTuZvOm4ZNLggEWlUjm9nkRelFWjmO3MB7+P/wLgqCwtWJF Utm6wSA2z/DIPyWfVAJG5GFMWqiF3N7UwZ9kOvNOuOgcS2kIVkzA6BoW7AMEAmSBVN9/ 4osA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=message-id:date:mime-version:comments:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hNoTphmcSJs1VN1tcAn+slcP3GRzxNun1rZdImnoMoE=; b=QOMlMW4/vauXm+CnbM7aEduAT2sZvxSlH7PfI7hyn8xugY4AMhH6461lu4AarWEBmp aB3/uuYbvoUzypXdF961C3Oio1iZisoPx0ljAzuegGdcA9CZe+ZyDqfUU2I5RubN4AYN nqu4me6z4m52Xfyj1PkJgMAKQfPw+Yj04uGxYgs/ztI5nJSDrC6gfw0P8sxgJ0bP5SQx NceQnMW6bahM5QS6sIVWBqSwBfsHRxw67qrkzzsc+4DiMqwpetL9eEKf9bssIQ/cP1Mz 9Wk9MirXsmGE35l7NIZIdA+fJSlO4NxA9u5chxl1sCrI5vUkwm6s4LcmwxXE5ogANG92 mV6w== X-Gm-Message-State: ANoB5plvN1V3QniBMXpzOdA2OcYgzNoQbzMlhUoTfYIle+Ha9qW49AE9 lq9W0StPCJU00cO/m2hG5c5lPw== X-Google-Smtp-Source: AA0mqf63PH8W3e7Wkw+9vARGU5Bl4qMiaO1zn5Fhas6vMl9aCr8p1/JC4Y6L7ZI/bHfITLoovME+/w== X-Received: by 2002:a17:906:d963:b0:7c1:98f:be57 with SMTP id rp3-20020a170906d96300b007c1098fbe57mr2269652ejb.97.1670316048089; Tue, 06 Dec 2022 00:40:48 -0800 (PST) Received: from antos ([88.103.134.108]) by smtp.gmail.com with ESMTPSA id k2-20020a170906680200b0077077c62cadsm7044031ejr.31.2022.12.06.00.40.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 00:40:47 -0800 (PST) From: Antonin Houska To: Peter Eisentraut cc: pgsql-hackers Subject: Re: refactor ExecGrant_*() functions In-reply-to: <22c7e802-4e7d-8d87-8b71-cba95e6f4bcf@enterprisedb.com> References: <22c7e802-4e7d-8d87-8b71-cba95e6f4bcf@enterprisedb.com> Comments: In-reply-to Peter Eisentraut message dated "Fri, 02 Dec 2022 08:30:55 +0100." X-Mailer: MH-E 8.6+git; nmh 1.7+dev; GNU Emacs 28.2.50 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Tue, 06 Dec 2022 09:41:58 +0100 Message-ID: <24706.1670316118@antos> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain Peter Eisentraut wrote: > Continuing the ideas in [0], this patch refactors the ExecGrant_Foo() > functions and replaces many of them by a common function that is driven by the > ObjectProperty table. > > It would be nice to do more here, for example ExecGrant_Language(), which has > additional non-generalizable checks, but I think this is already a good start. > For example, the work being discussed on privileges on publications [1] would > be able to take good advantage of this. Right, I mostly copy and pasted the code when writing ExecGrant_Publication(). I agree that your refactoring is very useful. Attached are my proposals for improvements. One is to avoid memory leak, the other tries to improve readability a little bit. > [0]: > https://www.postgresql.org/message-id/95c30f96-4060-2f48-98b5-a4392d3b6066@enterprisedb.com > [1]: https://www.postgresql.org/message-id/flat/20330.1652105397@antos -- Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=free_local_variables.diff diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c index d3121a469f..ac4490c0b8 100644 --- a/src/backend/catalog/aclchk.c +++ b/src/backend/catalog/aclchk.c @@ -2228,6 +2234,9 @@ ExecGrant_generic(InternalGrant *istmt, Oid classid, AclMode default_privs) newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values, nulls, replaces); + pfree(values); + pfree(nulls); + pfree(replaces); CatalogTupleUpdate(relation, &newtuple->t_self, newtuple); --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=introduce_nameDatum_variable.diff diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c index d3121a469f..ac4490c0b8 100644 --- a/src/backend/catalog/aclchk.c +++ b/src/backend/catalog/aclchk.c @@ -2144,6 +2144,7 @@ ExecGrant_generic(InternalGrant *istmt, Oid classid, AclMode default_privs) { Oid objectid = lfirst_oid(cell); Datum aclDatum; + Datum nameDatum; bool isNull; AclMode avail_goptions; AclMode this_privileges; @@ -2197,6 +2198,11 @@ ExecGrant_generic(InternalGrant *istmt, Oid classid, AclMode default_privs) old_acl, ownerId, &grantorId, &avail_goptions); + nameDatum = SysCacheGetAttr(cacheid, tuple, + get_object_attnum_name(classid), + &isNull); + Assert(!isNull); + /* * Restrict the privileges to what we can actually grant, and emit the * standards-mandated warning and error messages. @@ -2205,7 +2211,7 @@ ExecGrant_generic(InternalGrant *istmt, Oid classid, AclMode default_privs) restrict_and_check_grant(istmt->is_grant, avail_goptions, istmt->all_privs, istmt->privileges, objectid, grantorId, get_object_type(classid, objectid), - NameStr(*DatumGetName(SysCacheGetAttr(cacheid, tuple, get_object_attnum_name(classid), &isNull))), + NameStr(*DatumGetName(nameDatum)), 0, NULL); /* --=-=-=--