Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rz7zd-0021Qy-MT for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Apr 2024 04:42:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rz7zb-006A7A-Fq for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Apr 2024 04:42:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rz7zb-006A71-4S for pgsql-hackers@lists.postgresql.org; Tue, 23 Apr 2024 04:42:55 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rz7zW-002SbY-9Y for pgsql-hackers@lists.postgresql.org; Tue, 23 Apr 2024 04:42:54 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6ecff9df447so4967535b3a.1 for ; Mon, 22 Apr 2024 21:42:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713847367; x=1714452167; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ExlXdjL+Xq8PrlSTPtWSNl2NOT4TIbxbt7DNu4ZFMRE=; b=k5kMVtSjt9F7VSy2Qww1/iE+L3lpWzI+ttJU0trlptmBNYHZ+zx6UijRdogkjngKTU ajVuCQkmKgtcxLViEe18wOoc5DuUHW8Oayqnf+TlfrSRHUhZacW4pZ4VAJplBxNBeR/t d4AeCPs7oQeOr3kXL0xQEsq0tmd3HFH5LqO/XWAK4yOb6Fg7tuygOBtdUhUcXwAtWdrF 6hvZaPFaJG9pA3Rto6IrTke/9eopgMnaLotbD4HEXCV6TW1nrdToE5pbdbUGskPWH8kn 941XVDw7g97zPKp+w5xqnw3M8YMZ6RRaZU/HLiLTIKD1iNDh19jGDB5+xFzu8bky5PMT OMKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713847367; x=1714452167; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ExlXdjL+Xq8PrlSTPtWSNl2NOT4TIbxbt7DNu4ZFMRE=; b=SOuoalU3fPIwQJyY5XkgSRf2t6Sj9ssl6SnikOuUenRR8iRow7VyF4AW5zt4pK3XQN ZJwi2mbvPEf7sAT0E2zuYk2usJAT/Pv5a/q2jXxYw9c8671+sjng8vkwGTRueVa0IzU+ t8va4HFzEGxxN5JX82/O3g94KO5tLrgCCxhi1PMsmWQpUhUB0prjBy2EFyWf5ekWIZmF j03YxAn3nnetLAxX0MUN75dPW/T5ejCp+RuARSiHV192JgADPFakkHFMtWZteRSjOCc1 OOHAB9HOwu9gn9gDeLVEGMLG42AownGjecYvfEsZzrZqbmpCpOB/oNKfmwNhyIhdLXS+ 8XNg== X-Gm-Message-State: AOJu0YwdBK32PowMc2UMw4zSEShdFqqKBLQWDh2t9IXDxwFspPYYEc/Z bT3C+rfcv8qyJXxuYM5p5Xvq6piFrkliGyBe9QoB/3ZdPGUODYac X-Google-Smtp-Source: AGHT+IERs/2hdIYNb1MIYbBjHQacDj//6x8NgWSbQg+PD7HNcpyAzUvf94GseDXzWg/6DFwU8ZbqmA== X-Received: by 2002:a05:6a21:8807:b0:1a7:7358:f111 with SMTP id ta7-20020a056a21880700b001a77358f111mr13425278pzc.31.1713847367317; Mon, 22 Apr 2024 21:42:47 -0700 (PDT) Received: from [172.20.10.3] (ppp-49-237-202-94.revip6.asianet.co.th. [49.237.202.94]) by smtp.gmail.com with ESMTPSA id a5-20020aa78e85000000b006e554afa254sm8659853pfr.38.2024.04.22.21.42.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 22 Apr 2024 21:42:46 -0700 (PDT) Message-ID: Date: Tue, 23 Apr 2024 11:42:41 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: query_id, pg_stat_activity, extended query protocol To: "Imseih (AWS), Sami" , Michael Paquier , kaido vaikla Cc: "pgsql-hackers@lists.postgresql.org" References: Content-Language: en-US From: Andrei Lepikhov In-Reply-To: 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 23/4/2024 11:16, Imseih (AWS), Sami wrote: >> FWIW, I'd like to think that we could improve the situation, requiring >> a mix of calling pgstat_report_query_id() while feeding on some query >> IDs retrieved from CachedPlanSource->query_list. I have not in >> details looked at how much could be achieved, TBH. > > I was dealing with this today and found this thread. I spent some time > looking at possible solutions. > > In the flow of extended query protocol, the exec_parse_message > reports the queryId, but subsequent calls to exec_bind_message > and exec_execute_message reset the queryId when calling > pgstat_report_activity(STATE_RUNNING,..) as you can see below. > > /* > * If a new query is started, we reset the query identifier as it'll only > * be known after parse analysis, to avoid reporting last query's > * identifier. > */ > if (state == STATE_RUNNING) > beentry->st_query_id = UINT64CONST(0); > > > So, I think the simple answer is something like the below. > Inside exec_bind_message and exec_execute_message, > the query_id should be reported after pg_report_activity. > > diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c > index 76f48b13d2..7ec2df91d5 100644 > --- a/src/backend/tcop/postgres.c > +++ b/src/backend/tcop/postgres.c > @@ -1678,6 +1678,7 @@ exec_bind_message(StringInfo input_message) > debug_query_string = psrc->query_string; > > pgstat_report_activity(STATE_RUNNING, psrc->query_string); > + pgstat_report_query_id(linitial_node(Query, psrc->query_list)->queryId, true); > > set_ps_display("BIND"); > > @@ -2146,6 +2147,7 @@ exec_execute_message(const char *portal_name, long max_rows) > debug_query_string = sourceText; > > pgstat_report_activity(STATE_RUNNING, sourceText); > + pgstat_report_query_id(portal->queryDesc->plannedstmt->queryId, true); > > cmdtagname = GetCommandTagNameAndLen(portal->commandTag, &cmdtaglen); > > > thoughts? In exec_bind_message, how can you be sure that queryId exists in query_list before the call of GetCachedPlan(), which will validate and lock the plan? What if some OIDs were altered in the middle? -- regards, Andrei Lepikhov