pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matheus Alcantara <matheusssilv97@gmail.com>
To: Corey Huinker <corey.huinker@gmail.com>
To: Etsuro Fujita <etsuro.fujita@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Cc: pgsql-hackers@postgresql.org
Cc: jkatz@postgresql.org
Cc: nathandbossart@gmail.com
Subject: Re: Import Statistics in postgres_fdw before resorting to sampling.
Date: Tue, 06 Jan 2026 18:38:04 -0300
Message-ID: <DFHTXIT8UOZ4.146C1ZNQ4Q6FY@gmail.com> (raw)
In-Reply-To: <CADkLM=dSBfGT-J5xmiwkMg6FU9CByKWA1XVwC2P+-_-pZ_3OsA@mail.gmail.com>
References: <CADkLM=chrYAx=X2KUcDRST4RLaRLivYDohZrkW4LLBa0iBhb5w@mail.gmail.com>
	<CAExHW5seoMNjHiZge0O=p-oF6PGsY8om4VjpzTCBsaASXZC6Fw@mail.gmail.com>
	<CADkLM=feCJrEDWCOpoxxg58J_6zX6nv+BnXufUOEZuP7bYTCbg@mail.gmail.com>
	<CADkLM=d8+FwmbH0imhPOw=VGEASL4Q8tsCFjNcnfbU7sSWm0ig@mail.gmail.com>
	<CADkLM=cVc-KUAqcm1yoDkGwMrcgCvxY3WAMpaRZVLtO9sbc+kg@mail.gmail.com>
	<aPXaeowZ6RBPlsrD@paquier.xyz>
	<CADkLM=dSJk8h+14Wi7dGGcO1168R_nCGO2EHRTcC6b2Zu0rhpg@mail.gmail.com>
	<CAPmGK15Lk0ynP_b46sgfV1pmByzWUKVQoZBSV8fbN0rKb_6AnA@mail.gmail.com>
	<CADkLM=foU7yK1rfMzqTPiQtfEOGANAS9dHE5-Js6hEXmZiKfow@mail.gmail.com>
	<CADkLM=eDJiXe58+fXiFqp8Zyrh-E28mU11U53rVGjx0AQFzBKw@mail.gmail.com>
	<CAPmGK15jEDCeMXE=EEuA5-1tC2xg5WYSjWzPGpcz+udHBT4bUA@mail.gmail.com>
	<CADkLM=fRF2w-_fMyztS0R4e9YVH8tamHXMsBBVhFsSHoJKrKXg@mail.gmail.com>
	<CAPmGK17Dfjy_zLH1yjPqybpSueHWP7Gy_xBZXA2NpRso1qya7A@mail.gmail.com>
	<CADkLM=fb=FsBLqNc14-y4na57ypywZxYb4q-zrxcaY6qQW0B2Q@mail.gmail.com>
	<CADkLM=dfuT8YgXOyoXW7nuGdOFw-sYh2uR3hQS-N66Gj_k_ytw@mail.gmail.com>
	<CAPmGK17v-qJL5j2Z25jD33vKLyhAnNppYsy5UTbEAwAeBP=vGQ@mail.gmail.com>
	<CAPmGK15iR8M55_Fi6PZyvSH5-4B-R2n2YvvhRJB8Pm2cxxrF1w@mail.gmail.com>
	<CADkLM=cVZH+SDUuvsyMHzuWTK5XkK1i_L5dgTNrgRXWN1S-syQ@mail.gmail.com>
	<CAPmGK16AQzGsEe6pD7m42AO1KRCuXQu=U_abXaAf3juCS5U+mA@mail.gmail.com>
	<CADkLM=dSBfGT-J5xmiwkMg6FU9CByKWA1XVwC2P+-_-pZ_3OsA@mail.gmail.com>

Hi, thanks for working on this. Generally I think that this is a good
idea to avoid fetching rows from a foreign table to compute statistics
that may already be available on the foreign server.

I started reviewing the v7 patch and here are my initial comments. I
still want to do another round of review and run some more tests.

+		if (fdwroutine->ImportStatistics != NULL &&
+			fdwroutine->StatisticsAreImportable != NULL &&
+			fdwroutine->StatisticsAreImportable(onerel))
+			import_stats = true;
+		else
+		{
+			if (fdwroutine->AnalyzeForeignTable != NULL)
+				ok = fdwroutine->AnalyzeForeignTable(onerel,
+													 &acquirefunc,
+													 &relpages);
+
+			if (!ok)
+			{
+				ereport(WARNING,
+						errmsg("skipping \"%s\" -- cannot analyze this foreign table.",
+							   RelationGetRelationName(onerel)));
+				relation_close(onerel, ShareUpdateExclusiveLock);
+				return;
+			}
+		}
+
 		if (fdwroutine->AnalyzeForeignTable != NULL)
 			ok = fdwroutine->AnalyzeForeignTable(onerel,
 												 &acquirefunc,
												 &relpages);

		if (!ok)
		{
			ereport(WARNING,
					(errmsg("skipping \"%s\" --- cannot analyze this foreign table",
							RelationGetRelationName(onerel))));
			relation_close(onerel, ShareUpdateExclusiveLock);
			return;
		}

It seems that we have the same code within the else branch after the if/else
check, is this correct?

---

+	 * Every row of the result should be an attribute that we specificially

s/specificially/specifically

---

+		if (TupleDescAttr(tupdesc, i)->attisdropped)
+			continue;
+
+		/* Ignore generated columns. */
+		if (TupleDescAttr(tupdesc, i)->attgenerated)
+			continue;
+
+		attname = NameStr(TupleDescAttr(tupdesc, i)->attname);

Wouldn't be better to call TupleDescAttr a single time and save the value?
		Form_pg_attribute attr = TupleDescAttr(tupdesc, i - 1);

		/* Ignore dropped columns. */
		if (attr->attisdropped)
			continue;
		...

--
Matheus Alcantara
EDB: https://www.enterprisedb.com






view thread (89+ messages)  latest in thread

Message-ID: <DFHTXIT8UOZ4.146C1ZNQ4Q6FY@gmail.com>
Permalink:  ../DFHTXIT8UOZ4.146C1ZNQ4Q6FY@gmail.com/
Also on:    postgresql.org/message-id/DFHTXIT8UOZ4.146C1ZNQ4Q6FY@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: matheusssilv97@gmail.com, corey.huinker@gmail.com, etsuro.fujita@gmail.com, michael@paquier.xyz, ashutosh.bapat.oss@gmail.com, jkatz@postgresql.org, nathandbossart@gmail.com
  Subject: Re: Import Statistics in postgres_fdw before resorting to sampling.
  In-Reply-To: <DFHTXIT8UOZ4.146C1ZNQ4Q6FY@gmail.com>
  References: <CADkLM=chrYAx=X2KUcDRST4RLaRLivYDohZrkW4LLBa0iBhb5w@mail.gmail.com> <CADkLM=foU7yK1rfMzqTPiQtfEOGANAS9dHE5-Js6hEXmZiKfow@mail.gmail.com> <CADkLM=eDJiXe58+fXiFqp8Zyrh-E28mU11U53rVGjx0AQFzBKw@mail.gmail.com> <CAPmGK15jEDCeMXE=EEuA5-1tC2xg5WYSjWzPGpcz+udHBT4bUA@mail.gmail.com> <CADkLM=fRF2w-_fMyztS0R4e9YVH8tamHXMsBBVhFsSHoJKrKXg@mail.gmail.com> <CAPmGK17Dfjy_zLH1yjPqybpSueHWP7Gy_xBZXA2NpRso1qya7A@mail.gmail.com> <CADkLM=fb=FsBLqNc14-y4na57ypywZxYb4q-zrxcaY6qQW0B2Q@mail.gmail.com> <CADkLM=dfuT8YgXOyoXW7nuGdOFw-sYh2uR3hQS-N66Gj_k_ytw@mail.gmail.com> <CAPmGK17v-qJL5j2Z25jD33vKLyhAnNppYsy5UTbEAwAeBP=vGQ@mail.gmail.com> <CAPmGK15iR8M55_Fi6PZyvSH5-4B-R2n2YvvhRJB8Pm2cxxrF1w@mail.gmail.com> <CADkLM=cVZH+SDUuvsyMHzuWTK5XkK1i_L5dgTNrgRXWN1S-syQ@mail.gmail.com> <CAPmGK16AQzGsEe6pD7m42AO1KRCuXQu=U_abXaAf3juCS5U+mA@mail.gmail.com> <CADkLM=dSBfGT-J5xmiwkMg6FU9CByKWA1XVwC2P+-_-pZ_3OsA@mail.gmail.com> <DFHTXIT8UOZ4.146C1ZNQ4Q6FY@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox