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 1s9N3I-001n3c-LE for pgsql-general@arkaria.postgresql.org; Tue, 21 May 2024 10:49:06 +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 1s9N3I-009KJx-JX for pgsql-general@arkaria.postgresql.org; Tue, 21 May 2024 10:49:04 +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 1s9N3I-009KJp-8P for pgsql-general@lists.postgresql.org; Tue, 21 May 2024 10:49:04 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s9N3G-0001Lt-5K for pgsql-general@lists.postgresql.org; Tue, 21 May 2024 10:49:03 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-5231efd80f2so5710243e87.2 for ; Tue, 21 May 2024 03:49:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716288541; x=1716893341; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ChiIOnnPNDwNOL4r00H1KrG4QHHazT3VijZJX5jjUSE=; b=FNuTbZTBp26ulyAv9Mi+apccYhNUQ/FCxqNqtV8aKwFNEk7o30gkvTqcu21+zHQ0hL tiycpK48hgsiHRRwA4P69cDjSLnIVM1vgh8/Hk4KG8+FE/nVaUFTrTBq6WTZW/pgChr8 NlHf/xOpQIfgJuTIxlencOAL2NotyfQoEiwgMQR0OoKzJhSlEpdmSgPPTgCwCKO4vUJz Fo+LieqlivTAtIBQ+cL03oWuvUSNOznSHWjBcVqxlVyBmcbiZuqfSGcnpsdzqs/9c0q5 EPdw42yg2C5aMrv5YcrGpC7Z/G1iv8MPRO4v4KUrGcJ9pPYSQzIxLqOZG4uhKfGqsGVl Je+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716288541; x=1716893341; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ChiIOnnPNDwNOL4r00H1KrG4QHHazT3VijZJX5jjUSE=; b=K2cvFPb697rs/F9GYN39ZRO/9dHSDzxcUYgwGqqJ46dnOFaQ3nLZjye3zoWfvFvdoK oJySg7Zt1McrVFHR2YtreOePlVMg1J+RENxO5MXEWcdz/EZY3eCaETS+MIMbv8KO2cPy GUHR3WAlXjtzu5cmkYgSuOC2xDsZXmOTs5hrpGxbmEF8udeRTnYviL4nzGBk2M+wl/J8 5qxglbGIWJVsZZlZyoE50qYgrWaol3IIeOT8JBKxyJpXoF34HJpNhufJYGkQNDymt8ip bIuJaJ4rECscYQIwkQqvHc2TCQt0QbSEGxSNluBqZp8DKf9EaNCD/S0sj9rrtjQbsfKL 6tnA== X-Gm-Message-State: AOJu0YyGgXvCvdC4AN/l1ak5ezqxvObwHJ2rOAbo/QXqg2meookfe7Sp uwGEuQD41zyVjmrPRqQgzNWBqVFzuO4E5VGj9GoS8iBUSwVP5vETYmR7hxoeMCns8V1ZFhDvUZk xbQPio6EsL6wpqR+Jddw/69vUfFO3wJEK X-Google-Smtp-Source: AGHT+IHQoBSAHeaj42lnXy13wWMQ52LSb/M2iyWis0/fjGuzb/ChNsIC/RI9cnSW1EBGZZzMwe4A0W+JDioSmzcfnkM= X-Received: by 2002:a05:6512:3a8a:b0:51f:4c53:8e4c with SMTP id 2adb3069b0e04-5220fd785f3mr25843644e87.33.1716288540632; Tue, 21 May 2024 03:49:00 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 21 May 2024 22:48:48 +1200 Message-ID: Subject: Re: problem with query To: =?UTF-8?B?U2HFoW8gR2FudGFy?= Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 20 May 2024 at 23:09, Sa=C5=A1o Gantar wrote: > what helps is > SET enable_nestloop =3D off; > query takes less then 2seconds > but it's probably not a good idea to change this flag Looks like it's slow due to a bad selectivity estimate on the join between pgn and pgc. This results in: -> Nested Loop (cost=3D39.47..80.56 rows=3D1 width=3D133) (actual time=3D0.179..0.475 rows=3D57 loops=3D1) because the row estimate is 1, from there down to the root of the plan tree the planner thinks Nested Loop is a good join type for the remaining joins. Unfortunately, it's wrong. I don't really see a good way to convince the planner not to do this. The problem condition is: Recheck Cond: (relnamespace =3D pgn.oid) Filter: (relkind =3D ANY ('{r,v,f,m,p}'::"char"[])) if ANALYZE pg_class; does not help then you could maybe mess with the n_distinct estimate on pg_class.relnamespace, but you risk making other queries worse. Disabling enable_nestloop might be a good option, if you can just do it for this query. Unfortunately, parameterized nested loops are also tied into that GUC, so you'll stop those working for this plan. The Nested Loop between pgn and pgc looks like a good choice. The rest, not so much. I don't think (ndistinct) extended statistics on pg_class relnamespace, relkind will help since "relnamespace =3D pgn.oid" is a join condition. David