From 315f05f1f6986e48a72c971a26544d4fbacc78f2 Mon Sep 17 00:00:00 2001
From: AyoubKAZ <kazarayoub2004@gmail.com>
Date: Thu, 5 Nov 2026 16:56:52 +0100
Subject: [PATCH v2] file_fdw: Document why ANALYZE does not enforce
 REJECT_LIMIT

When a file_fdw foreign table is configured with on_error 'ignore' and
reject_limit, foreign table scans abort with an error once the number
of skipped rows exceeds reject_limit. However, ANALYZE intentionally
ignores reject_limit while sampling rows so that statistics can still be
gathered from valid rows, rather than failing and leaving table
statistics outdated.

Document this exception in the file_fdw documentation and add an
explanatory comment in file_acquire_sample_rows().
---
 contrib/file_fdw/file_fdw.c | 6 ++++++
 doc/src/sgml/file-fdw.sgml  | 2 ++
 2 files changed, 8 insertions(+)

diff --git a/contrib/file_fdw/file_fdw.c b/contrib/file_fdw/file_fdw.c
index 7d9dc3baf74..91863dcc243 100644
--- a/contrib/file_fdw/file_fdw.c
+++ b/contrib/file_fdw/file_fdw.c
@@ -1261,6 +1261,12 @@ file_acquire_sample_rows(Relation onerel, int elevel,
 			 * ErrorSaveContext ready for the next NextCopyFrom. Since we
 			 * don't set details_wanted and error_data is not to be filled,
 			 * just resetting error_occurred is enough.
+			 *
+			 * Note that unlike fileIterateForeignScan(), we intentionally do
+			 * not enforce reject_limit here. Statistics collected from valid
+			 * rows remain useful even when the number of errors exceeds
+			 * reject_limit, whereas aborting ANALYZE would leave the table
+			 * with outdated statistics.
 			 */
 			cstate->escontext->error_occurred = false;
 
diff --git a/doc/src/sgml/file-fdw.sgml b/doc/src/sgml/file-fdw.sgml
index 3638689436f..012985994bd 100644
--- a/doc/src/sgml/file-fdw.sgml
+++ b/doc/src/sgml/file-fdw.sgml
@@ -157,6 +157,8 @@
      Specifies the maximum number of errors tolerated while converting a column's
      input value to its data type, the same as <command>COPY</command>'s
     <literal>REJECT_LIMIT</literal> option.
+     This option is not enforced when <command>ANALYZE</command> samples rows
+     from the foreign table.
     </para>
    </listitem>
   </varlistentry>
-- 
2.34.1

