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 1kkJOT-0001Kk-2v for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Dec 2020 04:05:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kkJOS-0007kn-1X for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Dec 2020 04:05:28 +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 1kkJOR-0007kc-Oo for pgsql-hackers@lists.postgresql.org; Wed, 02 Dec 2020 04:05:27 +0000 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kkJOP-0005G1-HT for pgsql-hackers@lists.postgresql.org; Wed, 02 Dec 2020 04:05:26 +0000 Received: by mail-pg1-x52a.google.com with SMTP id t37so268464pga.7 for ; Tue, 01 Dec 2020 20:05:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition; bh=41hY1meE8e25hoRJ0AThTF20ddE9DuESoCtBk9tjOKM=; b=N6o9riJT4CDPIkwEFA/rs/lvCNPQRnjZfU4dyymVvV/vdoRBR6IX4CvGccI9PmHrkg Zk6z2yEL1h1xzXZX2iv4zrlnMbD8h1niYDEn0ZaBLSNQFq6TpUMaKQbDY7kFnvc3Tdux ZoheGLbGhohgTwsETIvSN9htO8+bniVKy7F+vCnYoJq0T9RSYgR+RUAQgB9G/MgVA4K2 sbn4UsiheVRePsqJj+NTZlDBnJWA+Fx2yOg3RtOv7R7ctOeI2WPN/Gn7y8JWiHWJL+Hg /SWcfr4ZWYvAnwAG6hN4pfLZJg9ptGgJ/OzzI5T0oGi7W1UWta+e+Kah6MTE2U4BTMNZ iFww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=41hY1meE8e25hoRJ0AThTF20ddE9DuESoCtBk9tjOKM=; b=BtYqghaTIIuVCplxTZV+5J972xDp2MJaYI6jT1lzlwmI43r4+yps56X7Dd8Gr8yrcV CFgpPphxfbt5WIHJIyYnT+N4dpBgmR6ooFydaHs6+qSRYT4zHdES92IfwLKuhvryEi7e qcFZpA/0QidjtPnwQWOJ4X7STNDbgydiFDq3M4Spo4hr/cLxpa7OcDkMYoVnfaiPLgmk I1Jrr18S2sZqSItMPFREdN2cqpNSS21MYPcYvwDzGlJs3QEKqEzvVHOvx1/GFiyWEmWl 7MZZ5pIwJ3KThs+Ox9h1G0WYFOaU5C/aHyVu+y9GGTSjRATDoeqlo/qdqe85YTruHgkz JtwA== X-Gm-Message-State: AOAM532/yhGAQQ3KNuKtEomyI18RvPFwCYqCUMy2qlkRXJHsq5LI3kCQ LdncI3bJPQGs4QOX2Jnrjk7iFirJdVjpMA== X-Google-Smtp-Source: ABdhPJyn1RzaDdOyh4k5bFWWDi45/UohTLHKiWvtcTSjaXzsZRBnWsIeAbEkeUSaWdeHRam6/18qUw== X-Received: by 2002:aa7:97a2:0:b029:19a:e055:1d53 with SMTP id d2-20020aa797a20000b029019ae0551d53mr738875pfq.12.1606881923673; Tue, 01 Dec 2020 20:05:23 -0800 (PST) Received: from nol (36-228-92-107.dynamic-ip.hinet.net. [36.228.92.107]) by smtp.gmail.com with ESMTPSA id a81sm415916pfd.178.2020.12.01.20.05.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2020 20:05:23 -0800 (PST) Date: Wed, 2 Dec 2020 12:05:16 +0800 From: Julien Rouhaud To: pgsql-hackers@lists.postgresql.org Subject: pg_stat_statements oddity with track = all Message-ID: <20201202040516.GA43757@nol> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, Someone raised an interested point recently on pg_stat_kcache extension for handling nested statements, which also applies to pg_stat_statements. The root issue is that when pg_stat_statements tracks nested statements, there's no way to really make sense of the counters, as top level statements will also account for underlying statements. Using a naive example: =# CREATE FUNCTION f1() RETURNS VOID AS $$BEGIN PERFORM pg_sleep(5); END; $$ LANGUAGE plpgsql; CREATE FUNCTION =# SELECT pg_stat_statements_reset(); pg_stat_statements_reset -------------------------- (1 row) =# SELECT f1(); f1 ---- (1 row) =# select sum(total_exec_time) from pg_stat_statements; sum -------------- 10004.403601 (1 row) It looks like there was 10s total execution time overall all statements, which doesn't really make sense. It's of course possible to avoid that using track = top, but tracking all nested statements is usually quite useful so it could be better to find a way to better address that problem. The only idea I have for that is to add a new field to entry key, for instance is_toplevel. The immediate cons is obviously that it could amplify quite a lot the number of entries tracked, so people may need to increase pg_stat_statements.max to avoid slowdown if that makes them reach frequent entry eviction. Should we address the problem, and in that case do you see a better way for that, or should we just document this behavior?