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 1vdVIE-001RUr-04 for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 15:17:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdVIC-00EeZN-0o for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jan 2026 15:17:49 +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.96) (envelope-from ) id 1vdVIB-00EeZF-2u for pgsql-hackers@lists.postgresql.org; Wed, 07 Jan 2026 15:17:48 +0000 Received: from mail-vs1-xe29.google.com ([2607:f8b0:4864:20::e29]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdVIA-0056Dw-0G for pgsql-hackers@postgresql.org; Wed, 07 Jan 2026 15:17:48 +0000 Received: by mail-vs1-xe29.google.com with SMTP id ada2fe7eead31-5ec84929ddcso297435137.3 for ; Wed, 07 Jan 2026 07:17:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767799064; x=1768403864; darn=postgresql.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=R7yhNwqID0zf2LZdARQyFS/jQWCixMVCBnHlPXggmA0=; b=AAheovZSFJ6NpRy8Q/Auc3FmA6uexJdj5JOcddYaV+AQSs7aUgcAS611x76h/CIPDT op0y+9KD/M0eauSDRC5U8YIgCgTlzdek44nyHvVgK3+N9gUMzx4DR9VdfJkOYxC5P8vo kSkdIBZe8dbuxBSMhJvQ7PlH4HAp9DgxEpRccyL0cXl4UjaxPn/O/TTboVOL9B9GmANW 6IgB/BeN6W5bEHEoAjvK26vw0pvzA6mW2AkpC2XdIFpjnFh/wB96kEzvWv7MMt1i+Kah dK4pEq3ZJZUfZPqJZknFyUZnhvlFlPLgTxkdv556kZM8CvSlAZqty/BajfuaIktd7HfP 6/uQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767799064; x=1768403864; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=R7yhNwqID0zf2LZdARQyFS/jQWCixMVCBnHlPXggmA0=; b=FxSDOIuvQq8wzFg5h0LHU2SURmOiQTnoOLFYH2i3hXfSu8PAfkaHN+eP915aC2p30P 4wb+QPkHcBBsGk1kWnJUaz2ShGY/JPjkj98Ws+/xdCnbqRptKW+pZt8ueVRU3ngGngmJ ZXB7upMYDaxoLLk+U7YttLi4uzJ1ylWnk0kNZWoHl1dhFxpzHXzZgJNkDxqVvu6RoUQh 5uauJ+RdOMRM8Uec1Cm0F1+AgQ844XAytpy3QNE4qb/qEmkcX3M5nDNwLul1tOj0Evgt AWb+2IRjcVtE8C8bSSB+A0dvpNk0oSbxWnNDR8sFC9Xer7o4U486klQWOU9qzsHVpDIR dlAw== X-Forwarded-Encrypted: i=1; AJvYcCUoXXvtV3mOPuiUGH472wCdl8s1WDn144nHP9+FnC7iWTk0sk+ECF/ZhLvZ/1MKb76Sp+BPFJKYXO02bo32@postgresql.org X-Gm-Message-State: AOJu0Ywqrxlhv3VgutWjh8sRPVdRMr8RucPAkcZFFFVFGKkx9nhG7Gog rhi9fs9ffjGbpRhtV16ogtISvlzee8RA01PWRV+tAn3RNF1l2Fu6a3LQ X-Gm-Gg: AY/fxX4tHmKVIBjEbNqOYha2zjx5EIQ2VgG+qi6VyOQoaq74UwtHQD1xL4E7lXsSZn3 hnzPH0s2Ta322bVkACEgyRNzC7Hh2zNCBPqOCf6FSNDb7OJc8P7y7p3dIrGf2qICmKdo8hFDuRK 1siSpaq9DRS4TNA7Qj88YwWkkG0q13fEDDq6q3KM1+U5PwY3Mt8Y+LLng5bPxwZLiXxx1f2Kdqb yknVIfdn4r8A4vk4l5KFQjG3hdOloXTUp7XiiuNM89IiVPlcn2kbjn/RUKaklzI43KAMl6J9DBZ 801fVQ+4GuBDvUh6qheS7ZiDjgWkIDJBOL12aE058Cnk6J7W3Iy/c6euHVZDhK15nswb0bKb3Fz lr5mnUwwiFLKMmw7ID8YLb9xTwOWP+njsEpfAvEqPU0lgIWK5Yjj+TlKkuEnJHHhlxCl5wpitY9 go+NLHPz+wLFEFi/3pS1AVeohDGAPogw6/42gkTo4pap1f X-Google-Smtp-Source: AGHT+IE219nN6TcbW0g1Bt1i3fV0ha5BLOfyDganzkg5S5ntGffM1Q6Y+ByCGWPeeazgd3aSThkisg== X-Received: by 2002:a05:6102:688a:b0:5df:3b19:4125 with SMTP id ada2fe7eead31-5ecb692cf34mr907939137.26.1767799063969; Wed, 07 Jan 2026 07:17:43 -0800 (PST) Received: from localhost ([2804:14d:328a:a59c:4934:3e02:ac25:9aec]) by smtp.gmail.com with ESMTPSA id ada2fe7eead31-5ec77064e86sm3238049137.7.2026.01.07.07.17.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 07 Jan 2026 07:17:43 -0800 (PST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 07 Jan 2026 12:17:38 -0300 Message-Id: Cc: "Michael Paquier" , "Ashutosh Bapat" , , , Subject: Re: Import Statistics in postgres_fdw before resorting to sampling. From: "Matheus Alcantara" To: "Corey Huinker" , "Etsuro Fujita" X-Mailer: aerc 0.21.0 References: In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed Jan 7, 2026 at 3:04 AM -03, Corey Huinker wrote: >> >> > I agree, if there is no fallback, then the default should be false. Wh= en >> I was initially brainstorming this patch, Nathan Bossart had suggested >> making it the default because 1) that would be an automatic benefit to >> users and 2) the cost for attempting to import stats was small in >> comparison to a table stample, so it was worth the attempt. I still want >> users to get that automatic benefit, but if there is no fallback to >> sampling then the default only makes sense as false. >> >> I think that the FDW API that I proposed could actually allow us to >> fall back to sampling, by modifying StatisticsAreImportable so that it >> also checks if 1) there are statistics on the remote server and 2) the >> data is fresh enough, and if so, returns true; otherwise, returns >> false; in the latter case we could fall back to sampling. And if we >> modified it as such, I think we could change the default to true. >> (Checking #2 is necessary to avoid importing stale data, which would >> degrade plan quality.) > > > I've given this some more thought. > > First, we'd have to add the va_cols param to StatisticsAreImportable, whi= ch > isn't itself terrible. > > Then, we'd have to determine that there are stats available for every > mapped column (filtered by va_cols, if any). But the only way to do that = is > to query the pg_stats view on the remote end, and if we have done that, > then we've already fetched the stats. Yes, we could avoid selecting the > actual statistical values, and that would save some network bandwidth at > the cost of having to do the query again with stats. So I don't really se= e > the savings. > > Also, the pg_stats view is our security-barrier black box into statistics= , > and it gives no insight into how recently those stats were acquired. We > could poke pg_stat_all_tables, assuming we can even query it, and then ma= ke > a value judgement on the value of (CURRENT_TIMESTAMP - > GREATEST(last_analyze, last_autoanalyze), but that value is highly > subjective. > > I suppose we could move all of the statistics fetching > into StatisticsAreImportable, And carry those values forward if they are > satisfactory. That would leave ImportStatistics() with little to do other > than form up the calls to pg_restore_*_stats(), but those could still fai= l, > and at that point we'd have no way to fall back to sampling and analysis. > > I really want to make sampling fallback possible. > > Anyway, here's v8, incorporating the documentation feedback and Matheus's > notes. Thanks for the new version. +static bool +postgresStatisticsAreImportable(Relation relation) +... + + /* + * Server-level options can be overridden by table-level options, so chec= k + * server-level first. + */ + foreach(lc, server->options) + { + DefElem *def =3D (DefElem *) lfirst(lc); + + if (strcmp(def->defname, "fetch_stats") =3D=3D 0) + { + fetch_stats =3D defGetBoolean(def); + break; + } + } + + foreach(lc, table->options) + { + DefElem *def =3D (DefElem *) lfirst(lc); + + if (strcmp(def->defname, "fetch_stats") =3D=3D 0) + { + fetch_stats =3D defGetBoolean(def); + break; + } + } + I don't think that it's good to make StatisticsAreImportable() routine check if fetch_stats is enabled on foreign server/table options because if so, every fdw implementation would need this same block of code and also fdw implementations may forget or bypass these options which I don't think that it would be a desired behavior. What about move this check to analyze_rel()? Perhaps create a function that just check if the fetch_stats is enabled. If the above statement make sense, it seems to me that StatisticsAreImportable() may not be needed at all.=20 I think that we could make analyze_rel() check if fetch_stats is enable on the foreign server/table and then call ImportStatistics() which could return true or false. If it returns true it means that the statistics was imported successfully, otherwise if it returns false we could fallback to table sampling as we already do today. ImportStatistics could return false if the foreign server don't have statistics for the requested table, even after running ANALYZE if remote_analyze is true. Is that make sense? Any thoughts? -- Matheus Alcantara EDB: https://www.enterprisedb.com