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 1n6HvE-00026F-KU for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Jan 2022 20:02:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n6HvC-0008UT-IF for pgsql-hackers@arkaria.postgresql.org; Sat, 08 Jan 2022 20:02:38 +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 1n6HvC-0008UK-6r for pgsql-hackers@lists.postgresql.org; Sat, 08 Jan 2022 20:02:38 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n6Hv6-0003Bu-0Q for pgsql-hackers@lists.postgresql.org; Sat, 08 Jan 2022 20:02:37 +0000 Received: from [192.168.0.192] (unknown [77.222.120.235]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 2E06D21C5676; Sat, 8 Jan 2022 23:02:29 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1641672150; bh=r10UB/gfSahTponrg3Lfs1rd/QEHecP+ilw7/4xuLlo=; h=Date:From:Subject:To; b=hZAvBDHnPQJCfKwA0jBjje0QbfV9QFgiq/5Ot5FFzmh6nw+X68Igj67WM7yAuwKEG wuAhE0CHYygVO/6ZRzzgRbypVxvBTnN3gviQuYQcZC6k+zj6BX2cP4xLpYU09iqlbw msPLEU/pgA6yxdEev9M+BWfs6LqZfnyVP4Eis2TU= Message-ID: Date: Sun, 9 Jan 2022 01:02:23 +0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Content-Language: en-US From: Andrey Lepikhov Subject: Multiple Query IDs for a rewritten parse tree Organization: Postgres Professional To: PostgreSQL Hackers , Tom Lane 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 On 5/1/2022 10:13, Tom Lane wrote: > I feel like we need to get away from the idea that there is just > one query hash, and somehow let different extensions attach > differently-calculated hashes to a query. I don't have any immediate > ideas about how to do that in a reasonably inexpensive way. Now, queryId field represents an query class (depending on an jumbling implementation). It is used by extensions as the way for simple tracking a query from a parse tree creation point to the end of its life along all hook calls, which an extension uses (remember about possible plan caching). I know at least two different cases of using queryId: 1) for monitoring purposes - pg_stat_statements is watching how often queries of a class emerge in the database and collects a stat on each class. 2) adaptive purposes - some extensions influence a planner decision during the optimization stage and want to learn on a performance shift at the end of execution stage. Different purposes may require different jumbling implementations. But users can want to use such extensions at the same time. So we should allow to attach many different query IDs to a query (maybe better to call it as 'query label'?). Thinking for a while I invented three different ways to implement it: 1. queryId will be a trivial 64-bit counter. So, each extension can differ each query from any other, track it along all hooks, use an jumbling code and store an queryId internally. Here only one big problem I see - increasing overhead in the case of many consumers of queryId feature. 2. Instead of simple queryId we can store a list of pairs (QueryId, funcOid). An extension can register a callback for queryId generation and the core will form a list of queryIds right after an query tree rewriting. funcOid is needed to differ jumbling implementations. Here we should invent an additional node type for an element of the list. 3. Instead of queryId we could add a multi-purpose 'private' list in the Query struct. Any extension can add to this list additional object(s) (with registered object type, of course). As an example, i can imagine a kind of convention for queryIds in such case - store a String node with value: ' - '. This way we should implement a registered callback mechanism too. I think, third way is the cheapest, flexible and simple for implementation. Any thoughts, comments, criticism ? -- regards, Andrey Lepikhov Postgres Professional