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.96) (envelope-from ) id 1wCWzb-0024du-1M for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Apr 2026 06:11:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCWzY-00A85s-1Z for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Apr 2026 06:11:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wCWzY-00A85j-0E for pgsql-hackers@lists.postgresql.org; Tue, 14 Apr 2026 06:11:20 +0000 Received: from mail-oo1-xc2f.google.com ([2607:f8b0:4864:20::c2f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCWzW-00000000vTZ-2hBJ for pgsql-hackers@lists.postgresql.org; Tue, 14 Apr 2026 06:11:19 +0000 Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-662f30d3f1fso2957216eaf.1 for ; Mon, 13 Apr 2026 23:11:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776147078; cv=none; d=google.com; s=arc-20240605; b=EDCFB8CLJgkHIZ8jQELfRsmdXcr1tRLhlq4zHTG1VxTTXBlpVc4zM01QX5vqjMysIz NkaSH7MINmqRTg/QSb++qgowDbTEojfrLjKSQQFxdvL386qD2ujX+lWVsg9siV5Xdj9U iVqbDKDSXxKqDeAWQReaJxhbkCiaaVhU5pE7sEaQZy2hVC6RYYiPJGFHukmaIbRcU8wD zeUaxLnZxV1Ux5lbVihRsHhmZcnbiJbG/S2VJQ/kc1KtGZXTYgE9d6o4e3KG4S9E+GAO EjpCC17cjy7VvNqQruH8lvmtG85kjL8KeQJwZlYHhpsGMy+9bKOEUgKUssoNwsCy8tE+ HsGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=eHvw+FqpH3LnxGETf3BRM2v2WffB/w6X+S7xMK/uFH0=; fh=EuTZuoz6LLhsbFycISt4S3a1HSXQFaBvgalbKOBmWUg=; b=WnwAsG+H+V9k8gkjKC3C0CsBEc1MvV/wv7VUNTEyVDblq/FpzHydjK2915HEKKeRuy zO4AShOzoJkS4yCoa/HXCeATkZpsogkLx4c78TJbmDT9jf79KmqDfNfQLy7hFnq/8JpP 7BeNoGfo23CIx8EdaaYVDDqepmD685jCjwTZU6xG3FmlxmF/sF+wYLmcd2JwKXzVfV49 VQJlMmYLwgQAPTbBS+EtQfa7PKWCX/7PxmqijNl3aR5BzEGeBUmptG9bWxsbfgKUzseq acJAVqe7p66aiGQ2A8Gq2yx/WOaQ4Rug1Br5vq+l4qlvxIywTl7mAs5jTlyTBOwx1IRn HG9w==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776147078; x=1776751878; 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=eHvw+FqpH3LnxGETf3BRM2v2WffB/w6X+S7xMK/uFH0=; b=WO7I2Jk4xYA4/rCKzf5DwwQvfKXiv7LMCXDEK3DBu+3be0O/r5bE30gAL0IME0bXQ9 X/m6PwBN/ODptS0Jex7kdwKQbalD6pz9lXRKuXKPCVjoR//expr8mCBfA9I+0PlPwY2N 6L0hqBTLwCyQPCrZyEH31IrtrYGnHlpyySlfsydOy9hpGA92amZ7e/OmHG33MPgPkXc0 Wrq183TrqBEJNby1UvoEJRaRMvMKWwWmua97rD10WEKa53Fr5hDCiT1gro0bWrGKHhOT NAHQo2ZGgftd0F7jPUgeQoTBr5WyKtxXNjCd+6fqhe6v7wkIUU3xDH+QhsE0l1wjI6V0 H0zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776147078; x=1776751878; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=eHvw+FqpH3LnxGETf3BRM2v2WffB/w6X+S7xMK/uFH0=; b=fWv92xHS0YOTBH+3ZHpU2cC1ZXHy8slq521yYBQ3vpIQecrSMkmtFsd6KZ9xG+TV9g keiEtXBEQaiD5zqXHv3R3lu34aWBpq8/kahdN8xzFR1jAoRibnc3skOkFIk8YCUeopKz q8q1BPG/oJ8b0QojT96jJxdESlKpL3EGolExuB+V/ugRk52YS9ON0IH8DqAFjfTCKlUe 2+GZO/KYfSYFiQqdpv6ST9DvOky8nO/Q3ag2KsGpAcrQeUPf/m62hew8cYarZLwA41MK ODpWWj2dwbhA1VI6bzI4+8mdfpfcNjs9rad82TwDe9I9f5MvVrT5FmLIJ0iSpt1FzRgM brMg== X-Gm-Message-State: AOJu0YylZNlLR57MDS/OQDmGc/3z1rIAkphaaJSVNK/etpcqctp8m8zc TvxUEK0A4hjFeIgfX64B0uBF0y9aQhyazOlqBDmYD6bKFDpKkZbiYbmZo4322vvVX0aQ7DEvT2l RxhVs8ILRdAj+IqQ7Qx1f8vkRB6EyOSI= X-Gm-Gg: AeBDieswaAjop1XY4NEtA/1CEIltVRl5l7raTQ8VOIe4hu0D1l/aubgAdn9NVm/zjuT R4RWIJN2T3pru1BGLZNpfW9wwav4NVYY2OA0sPu4vWeqnBZCfPC78hh+sMtOdDVY9HPYobUCtRZ Li5whBc/YkKeDjiPa09UlZOJrdf4xQeh9sKNiFccLXPJr9cph8/usHiBUdyJjP65DJGYPoNWi9n Ppuqu8TEi3ITLnhv4sS+bkvMgf1C+eOWR320fUsvSg3i9oMF60DYKTwrhQ/SZfJdskDXii5IZpT fO+Lwtjy2A== X-Received: by 2002:a05:6820:178c:b0:67e:33a0:3f2a with SMTP id 006d021491bc7-68bf7fc922fmr7158605eaf.20.1776147078128; Mon, 13 Apr 2026 23:11:18 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Guo Date: Tue, 14 Apr 2026 15:11:06 +0900 X-Gm-Features: AQROBzDcT9nmaUYK2_ladGlLVHToLMJ3wUA78qnZT-GqPz3zdvFcHS5f1vb3Et0 Message-ID: Subject: Re: Propagate stadistinct through GROUP BY/DISTINCT in subqueries and CTEs To: wenhui qiu Cc: Pg Hackers 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, Apr 13, 2026 at 12:27=E2=80=AFPM wenhui qiu = wrote: > Thanks so much for working on this! While looking at the negative stadist= inct conversion, I was wondering if we might run into a potential edge case= with multi-level nested subqueries. What do you think? > > /* Convert negative stadistinct to absolute count */ > > if (stats->stadistinct < 0) > { > - RelOptInfo *baserel =3D find_base_rel(subroot, var->varno); > + RelOptInfo *baserel =3D vardata->rel; > > - if (baserel->tuples > 0) > + if (baserel && baserel->tuples > 0) > { > stats->stadistinct =3D (float4) > clamp_row_est(-stats->stadistinct * baserel->tuples); > } > } I don't think your proposed change would work. vardata->rel is the CTE/subquery scan rel in the outer query, and its tuples count is the CTE's output row count, not the base table's. Using it would be equivalent to not converting at all, since get_variable_numdistinct() already computes -stadistinct * vardata->rel->tuples. What we need here is the base table's rel in the subroot, which gives us the correct rowcount for interpreting the negative fraction. - Richard