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 1oOZBj-0007TU-NS for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Aug 2022 06:39:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oOZBi-0007YE-2r for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Aug 2022 06:39:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOZBh-0007W0-OP for pgsql-hackers@lists.postgresql.org; Thu, 18 Aug 2022 06:39:29 +0000 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oOZBd-0006HC-Pu for pgsql-hackers@lists.postgresql.org; Thu, 18 Aug 2022 06:39:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1660804766; x=1692340766; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=bbDFMfQp+VguYshH1lkVghpoTIZhG5fuUBIUt/FmwzQ=; b=bjk/G31MVGXyi9fbhOdsQCMGI5OGglH7NSEzQCErJeKzp1BIH57X/c82 07hqsRppHtwrWKCLhSo9RfqLPIU/BbR55+yfDh2yQN+vyd2W1gSZe/ttB t75X0IhKkdFp5Bh/W48achJecqsadAxn6MpdV/cUPw4XU6wuJ75YSh+Cs U=; X-IronPort-AV: E=Sophos;i="5.93,245,1654560000"; d="scan'208";a="234390454" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-54c9d11f.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Aug 2022 06:39:13 +0000 Received: from EX13D03EUC003.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1e-54c9d11f.us-east-1.amazon.com (Postfix) with ESMTPS id 1E316C0413; Thu, 18 Aug 2022 06:39:10 +0000 (UTC) Received: from [192.168.13.168] (10.43.162.158) by EX13D03EUC003.ant.amazon.com (10.43.164.192) with Microsoft SMTP Server (TLS) id 15.0.1497.38; Thu, 18 Aug 2022 06:39:06 +0000 Message-ID: <394d309c-ece2-75b4-9959-9f4537001359@amazon.com> Date: Thu, 18 Aug 2022 08:39:02 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Subject: Re: shared-memory based stats collector - v70 Content-Language: en-US To: Andres Freund , Greg Stark CC: Tom Lane , Melanie Plageman , Kyotaro Horiguchi , Justin Pryzby , Thomas Munro , "David G. Johnston" , PostgreSQL Hackers References: <83D73EC0-E3ED-4C98-A528-5E122278D553@anarazel.de> <5db2b670-f3df-57ea-bdc6-962a582ff2e4@amazon.com> <0befffb3-8323-f2cd-915f-0ab64396f500@amazon.com> <1c56f4c6-61ce-d131-553c-297326e20bf1@amazon.com> <20220817233053.sppiwk4a32u3x4rd@awork3.anarazel.de> From: "Drouvot, Bertrand" In-Reply-To: <20220817233053.sppiwk4a32u3x4rd@awork3.anarazel.de> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.43.162.158] X-ClientProxiedBy: EX13D15UWA002.ant.amazon.com (10.43.160.218) To EX13D03EUC003.ant.amazon.com (10.43.164.192) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 8/18/22 1:30 AM, Andres Freund wrote: > Hi, > > On 2022-08-17 15:46:42 -0400, Greg Stark wrote: >> Isn't there also a local hash table used to find the entries to reduce >> traffic on the shared hash table? Even if you don't take a snapshot >> does it get entered there? There are definitely still parts of this >> I'm working on a pretty vague understanding of :/ > Yes, there is. But it's more about code that generates stats, rather than > reporting functions. While there's backend local pending stats we need to have > a refcount on the shared stats item so that the stats item can't be dropped > and then revived when those local stats are flushed. What do you think about something along those lines for the reporting part only? Datum pgstat_fetch_all_tables_stats(PG_FUNCTION_ARGS) {     int         dbid = PG_ARGISNULL(0) ? -1 : (int) PG_GETARG_OID(0);     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;     dshash_seq_status hstat;     PgStatShared_HashEntry *p;     PgStatShared_Common *stats_data;     /* Only members of pg_read_all_stats can use this function */     if (!has_privs_of_role(GetUserId(), ROLE_PG_READ_ALL_STATS))     {         aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_FUNCTION, "pgstat_fetch_all_tables_stats");     }     pgstat_assert_is_up();     SetSingleFuncCall(fcinfo, 0);     dshash_seq_init(&hstat, pgStatLocal.shared_hash, false);     while ((p = dshash_seq_next(&hstat)) != NULL)     {         Datum           values[PG_STAT_GET_ALL_TABLES_STATS_COLS];         bool            nulls[PG_STAT_GET_ALL_TABLES_STATS_COLS];         PgStat_StatTabEntry * tabentry = NULL;         MemSet(values, 0, sizeof(values));         MemSet(nulls, false, sizeof(nulls));         /* If looking for specific dbid, ignore all the others */         if (dbid != -1 && p->key.dboid != (Oid) dbid)             continue;         /* If the entry is not of kind relation then ignore it */         if (p->key.kind != PGSTAT_KIND_RELATION)             continue;         /* If the entry has been dropped then ignore it */         if (p->dropped)             continue;         stats_data = dsa_get_address(pgStatLocal.dsa, p->body);         LWLockAcquire(&stats_data->lock, LW_SHARED);         tabentry = pgstat_get_entry_data(p->key.kind, stats_data);         values[0] = ObjectIdGetDatum(p->key.dboid);         values[1] = ObjectIdGetDatum(p->key.objoid);         values[2]= DatumGetInt64(tabentry->tuples_inserted);         values[3]= DatumGetInt64(tabentry->tuples_updated);         values[4]= DatumGetInt64(tabentry->tuples_deleted);         .         .         .         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);         LWLockRelease(&stats_data->lock);     }     dshash_seq_term(&hstat);     return (Datum) 0; } I also tried to make use of pgstat_get_entry_ref() but went into a failed assertion: pgstat_get_entry_ref -> dshash_find -> ASSERT_NO_PARTITION_LOCKS_HELD_BY_ME(hash_table) due to lock acquired by dshash_seq_next(). Regards, -- Bertrand Drouvot Amazon Web Services: https://aws.amazon.com