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 1mkoWd-0007oo-UH for pgsql-novice@arkaria.postgresql.org; Wed, 10 Nov 2021 14:24: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 1mkoWc-0004f9-O5 for pgsql-novice@arkaria.postgresql.org; Wed, 10 Nov 2021 14:24: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 1mkoWc-0004f0-Cs for pgsql-novice@lists.postgresql.org; Wed, 10 Nov 2021 14:24:30 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mkoWU-0004mR-SR for pgsql-novice@lists.postgresql.org; Wed, 10 Nov 2021 14:24:29 +0000 Received: by mail-lf1-x129.google.com with SMTP id b1so538066lfs.13 for ; Wed, 10 Nov 2021 06:24:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=EbS2HYBOjIueyio7iKHdvvTiRPGkb8C4Re8BTIEOfx0=; b=Z6nMyda4V0C45dqwfZcyubtt45f+jEiGppY/aQ2eF/ey8Jzo0Uw6Qr0xKE8pEPM7w+ mqz6Y/C8Y1aSdNQu0VPu221AVqNoTrXa3LiCI+ybopG3j1Xw+4o5B1QT6QXPc1ocWtRh eLfD9jxW7VsxR6xzrLCfLLUITS2hHrIr0HiN30hMlrtRp8IigecN1h748ifqb7qv2EY0 tFePqNzH14xplzPS2m4NNt3Fq7Xn76R8AuxWxkWNCFZ24mEW1shioC4lHORf1mHsYr1C 13w6M7blLYHm7vu/JBwNlRexLdBcVs+gOg1AvE2nXL0CNCT4hMYnr7sai21ha9bEz0HL wl3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=EbS2HYBOjIueyio7iKHdvvTiRPGkb8C4Re8BTIEOfx0=; b=LInxvvt6Kfch5EJq6S1CJUF7zx7kEPbnTmbT1W8eTmaT4UuDWCbRoKs3sBcv6Id4/j nh5ksGFT/xLXFgCoi5qSQMRNd8kWFWSqoDQLVkjliuY+TkmdlpAHdvFPlfdT5jwSHu7z 1IJJn3RbZpM2qrxbegNe+CsMrij4D51Uts9YATpiUVFmgStF6y6JiskOFkORQfhbvgMn fPafkmx7llFYWMUgfTu3vhJdbUsNuqQWua9Ux6pBjVoWLZr/aGLRrPyEfQPXPOjF4aeZ /553tZOEW/XNa9FGjC0els2RPog37+xKEkns3bEE0NTw8+ExDUW8X8dYWfWyNIf2/0UG nJXg== X-Gm-Message-State: AOAM532fOzdFjvYyzP5T81NKhwYV2uQffKg+inEs2NUONusOpQFpza/V lrT8LwmBtu/G4drbwenws7lx3ypTVO0nqyFxUYj1VXUswAA= X-Google-Smtp-Source: ABdhPJzsEXLTxATjPGks/bKC7pmK/AIlAvApKtM89xecyJQg2ZjFPnMmk4ZueAlVlkCvjTGtJqk7tdAOCVdLtmEuoro= X-Received: by 2002:ac2:4568:: with SMTP id k8mr149647lfm.80.1636554261867; Wed, 10 Nov 2021 06:24:21 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Rafia Sabih Date: Wed, 10 Nov 2021 15:24:11 +0100 Message-ID: Subject: Re: Check explain plan of a running query in other session To: Jonas Gassenmeyer Cc: pgsql-novice@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 8 Nov 2021 at 09:20, Jonas Gassenmeyer wrote: > > Hello, > > I am asking myself for a while if there is an easy option (via dictionary views like pg_stat_activity or similar) to check what execution plan was chosen for a long running SQL statement while it is running in a different session? > I have a performance problem in a plpgsql procedure that executes an update within a loop. My guess is that it chose a generic plan instead of bind peeking and then does not use an index. > > I am not able to proof my theory, since I don't know how to get the explain plan for it. > For me the easiest would be to check the running statement in a different session and "hook into" the other plpgsql session to check what the optimizer chose. > > Is that something I can do easily? If not: What is the PostgreSQL way of doing it? It is for reasons like these that we at Zalando developed an extension pg_mon (https://github.com/RafiaSabih/pg_mon), There you can find details like what are the scan methods and joins etc used by the query. You can give this a try. -- Regards, Rafia Sabih