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 1ovzPg-0000Lm-SC for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Nov 2022 11:20:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ovzPf-0004UQ-Pg for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Nov 2022 11:20:03 +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 1ovzPf-0004KT-E8 for pgsql-hackers@lists.postgresql.org; Fri, 18 Nov 2022 11:20:03 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ovzPY-00016a-KW for pgsql-hackers@lists.postgresql.org; Fri, 18 Nov 2022 11:20:02 +0000 Received: by mail-wm1-x335.google.com with SMTP id o30so3419156wms.2 for ; Fri, 18 Nov 2022 03:19:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=WxIFFUYobfTBx/eN7yUKsEjqA7lOzJCKpU72+zk18tc=; b=KPKSwd8HuOZy0xDAEz5fAqPQxntouES4JfDhRAGSyORpw26ohfZL9OgLXvzSkTCt84 v8X6f2vEOScLG5I7YeMcYzkE6/qSqmkNeMVhZfK/bbbIGPf9qMQvOjch3a9gAWMBZu1l q4VPVNmhrckxDJidjOxtYg01qwPXuvVgnBpybyyNp+UI9h0NWD6xF8xSiBCr1E1EyOrE 8o8fZl/E4b8t1NRFwurU5tNdpGNnJ035oVzVj896e+oj7ZGb22H6krcYziuS8xLzR3gw Tr+fO/wER21KxG2bgPfeyQ1vOdX0ozBTjqHeEUhNSe5qJsU6FIQ8AnQ9f+OCG5ppD/Sg w3Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WxIFFUYobfTBx/eN7yUKsEjqA7lOzJCKpU72+zk18tc=; b=JU3o+KAx1SsFql/GPPizpCjQFNvjJmfZEBXJAF4Fv6LKxdOHknQYhlnOHJPlU9J3JP bxDYm9Ukv+fq2Eius++WL+blw8Mf46f7SyfA+3rO5OXcE9MYO46QrTTK5BKFn4TkECu6 BtKCvOoitO6S53nzV5+eKw4ZB6whB93fCrpiO+wK+EQChFuPhOfMSdH3gP30chdIQpYG kVdImVgABW3ODBFOKlu7Z6MOti7TaFg0BxjXTKRAlZcFRvJbZwCUG5DHRLej7ajxADqR GImEHq5QLgg95w/JA5QHAO3kTzrfjmDgQt3de1FgKCybOXeqQllU/Y1qqhHiBrAGx9fq U76A== X-Gm-Message-State: ANoB5pmGPA8tr/H/l9TuYCu1OHR0HW+hgnqpxAhpVMFwl+a8bbIADKxX puFqFiDjkC1FCIFCJlaaMeJAX7jhvKXnXA== X-Google-Smtp-Source: AA0mqf7kDIvUYz5SQRgEfM570qGEmYE9UnBn4qk6+ObzYjlTx+9yl8nrb6hT99wZ2sAWzl0KCIHxMw== X-Received: by 2002:a05:600c:1c0a:b0:3cf:894b:fe6a with SMTP id j10-20020a05600c1c0a00b003cf894bfe6amr8204776wms.198.1668770395022; Fri, 18 Nov 2022 03:19:55 -0800 (PST) Received: from [192.168.19.184] ([54.239.6.190]) by smtp.gmail.com with ESMTPSA id p2-20020a1c7402000000b003cfe6fd7c60sm4134565wmc.8.2022.11.18.03.19.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 18 Nov 2022 03:19:54 -0800 (PST) Message-ID: <76dece9f-160a-0210-0396-9146c8e4831a@gmail.com> Date: Fri, 18 Nov 2022 12:18:38 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: Split index and table statistics into different types of stats Content-Language: en-US To: Andres Freund Cc: Melanie Plageman , PostgreSQL Hackers References: <20221101003006.3ehvn2prai26rkrf@awork3.anarazel.de> <4cc5476f-4360-1bed-c60d-2c21adf7db13@gmail.com> <06517d27-7a81-3b60-60c4-9a92c6498981@gmail.com> <8e8713ed-1663-f39b-effc-bd3e1632039a@gmail.com> <20221116201202.3k74ajawyom2c3eq@awork3.anarazel.de> From: "Drouvot, Bertrand" In-Reply-To: <20221116201202.3k74ajawyom2c3eq@awork3.anarazel.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 11/16/22 9:12 PM, Andres Freund wrote: > Hi, > > On 2022-11-15 10:48:40 +0100, Drouvot, Bertrand wrote: >> diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c >> index ae3365d917..be7f175bf1 100644 >> --- a/src/backend/utils/adt/pgstatfuncs.c >> +++ b/src/backend/utils/adt/pgstatfuncs.c >> @@ -36,24 +36,34 @@ >> >> #define HAS_PGSTAT_PERMISSIONS(role) (has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS) || has_privs_of_role(GetUserId(), role)) >> >> +#define PGSTAT_FETCH_STAT_ENTRY(entry, stat_name) ((entry == NULL) ? 0 : (int64) (entry->stat_name)) >> + >> Datum >> -pg_stat_get_numscans(PG_FUNCTION_ARGS) >> +pg_stat_get_index_numscans(PG_FUNCTION_ARGS) >> { >> Oid relid = PG_GETARG_OID(0); >> int64 result; >> - PgStat_StatTabEntry *tabentry; >> + PgStat_StatIndEntry *indentry = pgstat_fetch_stat_indentry(relid); >> >> - if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL) >> - result = 0; >> - else >> - result = (int64) (tabentry->numscans); >> + result = PGSTAT_FETCH_STAT_ENTRY(indentry, numscans); >> >> PG_RETURN_INT64(result); >> } > > This still leaves a fair bit of boilerplate. ISTM that the function body > really should just be a single line. > > Might even be worth defining the whole function via a macro. Perhaps something like > > PGSTAT_DEFINE_REL_FIELD_ACCESSOR(PGSTAT_KIND_INDEX, pg_stat_get_index, numscans); Thanks for the feedback! Right, what about something like the following? " #define PGSTAT_FETCH_STAT_ENTRY(pgstat_entry_kind, pgstat_fetch_stat_function, relid, stat_name) \ do { \ pgstat_entry_kind *entry = pgstat_fetch_stat_function(relid); \ PG_RETURN_INT64(entry == NULL ? 0 : (int64) (entry->stat_name)); \ } while (0) Datum pg_stat_get_index_numscans(PG_FUNCTION_ARGS) { PGSTAT_FETCH_STAT_ENTRY(PgStat_StatIndEntry, pgstat_fetch_stat_indentry, PG_GETARG_OID(0), numscans); } " > > I think the logic to infer which DB oid to use for a stats entry could be > shared between different kinds of stats. We don't need to duplicate it. > Agree, will provide a new patch version once [1] is committed. > Is there any reason to not replace the "double lookup" in > pgstat_fetch_stat_tabentry() with IsSharedRelation()? > > Thanks for the suggestion! > This should probably be done in a preparatory commit. Proposal submitted in [1]. [1]: https://www.postgresql.org/message-id/flat/2e4a0ae1-2696-9f0c-301c-2330e447133f%40gmail.com#e47bf5d2902121461b61ed47413628fc Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com