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 1pNelk-0000Gk-PB for pgsql-admin@arkaria.postgresql.org; Thu, 02 Feb 2023 18:57:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pNeli-0007mC-Ie for pgsql-admin@arkaria.postgresql.org; Thu, 02 Feb 2023 18:57:10 +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 1pNeli-0007m0-4T for pgsql-admin@lists.postgresql.org; Thu, 02 Feb 2023 18:57:10 +0000 Received: from mail-oo1-xc29.google.com ([2607:f8b0:4864:20::c29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pNelf-0001Vh-PO for pgsql-admin@lists.postgresql.org; Thu, 02 Feb 2023 18:57:09 +0000 Received: by mail-oo1-xc29.google.com with SMTP id i11-20020a056820012b00b00517518d79f6so270689ood.10 for ; Thu, 02 Feb 2023 10:57:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=p/FOgY/PIEehqvNKuHEcP3KvpaD9rir8xxAdIhpnVBA=; b=bD8dJD33CBxDfU3IOO6JxIRmX5B+fkXmGWtwK3Z1K17QDL47Mw0Nis1re7Zw9YIN3m +mG2lUQy4rncxzMi09bGfZV2+iAZvXDCFK4ABzMYNl8xB6m5DVZtKtvXDV3fjA5MDWpo 73Z9pktQ9c3IeBucQWGQNqQEZNrGLM3jXc8Go3k7UTiiA+U1wmjARvLLY/zTGCxneBIu TKLZKiKBnVV5xK4tbxsY8flQZcL8Y1XN5o72f1DzgoYEFcX9BC6RxAlSVlQVC3z16v9i TnTLmHYz+Y/NCIgXqXc357qW7JiiHP8GXo3DsKrvyka6tkUH6VjNwSi/jE9gCvX/kM/m pY2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=p/FOgY/PIEehqvNKuHEcP3KvpaD9rir8xxAdIhpnVBA=; b=GVHTbXUXvIelAB/r6wKsd/EP9kH5TPhPqLzYGVfaRJ/N+tSICr1DB1gIcjvh7h8OyA icWpO5zxyPt9kFeoKgFJAPSxkTSxbLYhxLc+k6v0Nq9y6mysqn7r5aOuIagPIwERoRhX PCvX3fyICD+SN5Te2cMZpkmuMsNUypG/ENuPWDQ5M2UeLUgYGwMQ5SEv6rjpW8DmJrhn laqrZhhUZUHRmJtp/shh7UBy3tCumiU8sDiiSZOb2Ob2kP801fD6t+uJQ4Mkv9xh+dWy BhFax1xgSTg9NvymhSYE5DNjdVfZhm9gsY1NPqWT79xzwDtSAVwkdCpm9/u7sixY6UXZ DZmg== X-Gm-Message-State: AO0yUKUYjAhxT7Jn3d73IsSe/BF+S9/pqVbtwLCu/6SQRNLitK6z76FA gcRTRBppCrBTzMJ5ehuCFjLSyXmHdOU= X-Google-Smtp-Source: AK7set9mklQ7uMH/cAHM8Edelq6UFtC39sEJ05gzTLxXwiddl68zBVnKK/UuHAbnbdgdH5euDvT00w== X-Received: by 2002:a05:6820:1053:b0:517:4ac0:e2e7 with SMTP id x19-20020a056820105300b005174ac0e2e7mr3250781oot.5.1675364225457; Thu, 02 Feb 2023 10:57:05 -0800 (PST) Received: from ?IPV6:2600:1700:bd2c:b810::43? ([2600:1700:bd2c:b810::43]) by smtp.googlemail.com with ESMTPSA id x48-20020a4a97f3000000b004f28d09a880sm144553ooi.13.2023.02.02.10.57.05 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Feb 2023 10:57:05 -0800 (PST) Content-Type: multipart/alternative; boundary="------------BiKYLLcGJKM5TsW7NUIsfodp" Message-ID: Date: Thu, 2 Feb 2023 12:57:04 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: too high planning time Content-Language: en-US To: pgsql-admin@lists.postgresql.org References: <1536295.1675351465@sss.pgh.pa.us> From: Ron In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------BiKYLLcGJKM5TsW7NUIsfodp Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit pg_stat_activity is the place to look. This tells you all of the blocked pids: SELECT pid      , username      , pg_blocking_pids(pid) AS blocked_by      , left(query, 100) as blocked_query FROM pg_stat_activity WHERE CARDINALITY(blocking_pids(pid)) > 0; From there, you can see what the blocking pid is doing. On 2/2/23 12:21, Kenny Bachman wrote: > In fact, when I run the query for the second time, it returns very fast > results. But after a while the problem reoccurs. > Not just for these queries, but almost all queries in the database are > slowed down this way. > Is it normal to have a lock on the catalog or system tables? What should I > do when this happens on pg_statistic or other catalogs? > > thanks a lot for your comments and help > Kenny > > Tom Lane , 2 Şub 2023 Per, 18:24 tarihinde şunu yazdı: > > Kenny Bachman writes: > > EXPLAIN ANALYZE select > >     i."DefinitionId", > > from > >     "T_WF_INSTANCE" i > > where > >     i."InstanceId" = 10045683193; > > >        QUERY PLAN > > > > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > >  Index Scan using > > "T_WF_INSTANCE_InstanceId_ApplicationCd_EntityStatusCd_idx" on > > "T_WF_INSTANCE" i  (cost=0.57..2.79 rows=1 width=34) (actual > > time=2.522..2.522 rows=1 loops=1) > >    Index Cond: ("InstanceId" = '10045683193'::bigint) > > > * Planning Time: 8460.446 ms Execution Time: 2.616 ms* > > (4 rows) > > It's hard to believe that such a simple query could take that > long to plan.  What I'm wondering is if the planner got blocked > on some other session's exclusive lock.  Not a lock on > "T_WF_INSTANCE" itself, because we'd have got that lock during > parsing before the "Planning Time" measurement starts.  But > there's going to be a physical access to the table's index > to determine its tree height, so an ex-lock on the index could > explain this.  Or an ex-lock on catalogs, particularly pg_statistic. > What else is going on in your database when this happens? > >                         regards, tom lane > -- Born in Arizona, moved to Babylonia. --------------BiKYLLcGJKM5TsW7NUIsfodp Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit pg_stat_activity is the place to look.

This tells you all of the blocked pids:
SELECT pid
     , username
     , pg_blocking_pids(pid) AS blocked_by
     , left(query, 100) as blocked_query
FROM pg_stat_activity
WHERE CARDINALITY(blocking_pids(pid)) > 0;

From there, you can see what the blocking pid is doing.

On 2/2/23 12:21, Kenny Bachman wrote:
In fact, when I run the query for the second time, it returns very fast results. But after a while the problem reoccurs.
Not just for these queries, but almost all queries in the database are slowed down this way. 
Is it normal to have a lock on the catalog or system tables? What should I do when this happens on pg_statistic or other catalogs?

thanks a lot for your comments and help
Kenny

Tom Lane <tgl@sss.pgh.pa.us>, 2 Şub 2023 Per, 18:24 tarihinde şunu yazdı:
Kenny Bachman <kenny.bachman17@gmail.com> writes:
> EXPLAIN ANALYZE select
>     i."DefinitionId",
> from
>     "T_WF_INSTANCE" i
> where
>     i."InstanceId" = 10045683193;

>        QUERY PLAN

> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>  Index Scan using
> "T_WF_INSTANCE_InstanceId_ApplicationCd_EntityStatusCd_idx" on
> "T_WF_INSTANCE" i  (cost=0.57..2.79 rows=1 width=34) (actual
> time=2.522..2.522 rows=1 loops=1)
>    Index Cond: ("InstanceId" = '10045683193'::bigint)

> * Planning Time: 8460.446 ms Execution Time: 2.616 ms*
> (4 rows)

It's hard to believe that such a simple query could take that
long to plan.  What I'm wondering is if the planner got blocked
on some other session's exclusive lock.  Not a lock on
"T_WF_INSTANCE" itself, because we'd have got that lock during
parsing before the "Planning Time" measurement starts.  But
there's going to be a physical access to the table's index
to determine its tree height, so an ex-lock on the index could
explain this.  Or an ex-lock on catalogs, particularly pg_statistic.
What else is going on in your database when this happens?

                        regards, tom lane

--
Born in Arizona, moved to Babylonia.
--------------BiKYLLcGJKM5TsW7NUIsfodp--