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 1sRwOk-002I0q-MR for pgsql-admin@arkaria.postgresql.org; Thu, 11 Jul 2024 16:11:58 +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 1sRwOj-00Ft7r-Bj for pgsql-admin@arkaria.postgresql.org; Thu, 11 Jul 2024 16:11:57 +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 1sRwOi-00Ft7d-SO for pgsql-admin@lists.postgresql.org; Thu, 11 Jul 2024 16:11:56 +0000 Received: from mailout.easymail.ca ([64.68.200.34]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRwOf-001aFU-Cj for pgsql-admin@lists.postgresql.org; Thu, 11 Jul 2024 16:11:55 +0000 Received: from localhost (localhost [127.0.0.1]) by mailout.easymail.ca (Postfix) with ESMTP id 3E6D361687; Thu, 11 Jul 2024 16:11:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=elevated-dev.com; s=easymail; t=1720714311; bh=QNHtZLqzaGxVutEAeL6LFzOLWLMvB1rHi7fNaIDlkkc=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=0Q3I8uA7lieVojxxlvAmtLFsYt1mmRRUUAHYbg6nQYsJFNuviveSVPcYIpcO6L3d8 nmeqn2NTdEBldVAoe4FrAxgdtRuli3nfeY3+IPmSny2wkOzb0/q6gA7DGYTgCSohlG 1hNooIRCfQR53io8PfLwXX4G7H26O1OdXWpPmlvcKs8t3IFpVW2iBlGKgpLQ06Uo5i q87KX1grD8CgxOrwyR4XHadHCJbKmc64+tIr6FBr0H/fsCwj5uSuMQcPDqw0APoTtb 5j5pz6md9Pb7n2E3My4vh0mr87eomPk9+FguPafv4uh3Fzl4jOopRQBzMKiqYyBgvT iJhRe+DArQzsQ== X-Virus-Scanned: Debian amavisd-new at emo09-pco.easydns.vpn Received: from mailout.easymail.ca ([127.0.0.1]) by localhost (emo09-pco.easydns.vpn [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZYSGO0ficZRb; Thu, 11 Jul 2024 16:11:50 +0000 (UTC) Received: from smtpclient.apple (unknown [165.140.184.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mailout.easymail.ca (Postfix) with ESMTPSA id A43FB61680; Thu, 11 Jul 2024 16:11:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=elevated-dev.com; s=easymail; t=1720714310; bh=QNHtZLqzaGxVutEAeL6LFzOLWLMvB1rHi7fNaIDlkkc=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=L0jhi+fgVLLxLsjLIWWcId91UyDKAvQBl1ueAAS5VKR2W3ISzJLS77Q+/nIqKIsep QTnJ8hctmIAYJ3sIiGnIPbWmOKkV00yhD1pRIWfSCpUdepKVyhBIUBFipcfdPYIukU W4fJhiTPqHAJnXZoHXDXJpr7lv0nHDwhrIWt8ThnsOu560kGvjIw1cvMnWB1G5uLUr oFMveTmovFn9tSf8jS6txWK2aTdV/3123DYKVeioiiQ5Sy9Kcb7ULmTXRyIM2vu3o1 UNTcXwxYBHMFBLWo9kPtowalkhLAfVl5urjUQrUGiVVEBsIGSdX5JsjOpwWBLxV5lS i1xo3Hl2dR6Pg== Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: Better way to find long-running queries? From: Scott Ribe In-Reply-To: Date: Thu, 11 Jul 2024 10:11:39 -0600 Cc: Pgsql-admin Content-Transfer-Encoding: quoted-printable Message-Id: <2B0E4039-5871-4E31-920D-772C2377C78E@elevated-dev.com> References: To: Ron Johnson X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Well, in the past I have approached it from the other end: (AND query NOT ILIKE ('insert') AND query NOT ILIKE...) excluding queries I didn't care about -- Scott Ribe scott_ribe@elevated-dev.com https://www.linkedin.com/in/scottribe/ > On Jul 11, 2024, at 10:03=E2=80=AFAM, Ron Johnson = wrote: >=20 > This query works, and works quite well, but fails if the query starts = with a comment. >=20 > So far, I've accepted that "false negative" error, because being too = aggressive at finding the word SELECT in a query is a worse problem. = (For example, the string "select" might be in a column name that's part = of a long-running COPY or ALTER.) >=20 > But I've always hoped for something better. Thus: is there any way in = SQL to parse pg_stat_activity.query for the purpose of excluding = comments? >=20 > PG versions 9.6.24 (yes, it's EOL), 14.12, 15.7 and 16.3, if it makes = a difference. >=20 > SELECT datname,=20 > pid,=20 > client_addr,=20 > client_hostname,=20 > query_start,=20 > to_char(EXTRACT(epoch FROM now()-query_start), '99,999.99') as = elapsed_secs,=20 > md5(query) > pg_stat_activity=20 > WHERE datname not in ('postgres', 'template0', 'template1')=20 > AND state !=3D 'idle' > AND client_hostname !~ 'db[1-8].example.com' > AND EXTRACT(epoch FROM now() - query_start) > 1800 > AND SUBSTRING(upper(query) from 1 for 6) =3D 'SELECT'; >=20