[PATCH] Fix infinite recursion when foreign table references itself

From: John Mikk <jomikk2706(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] Fix infinite recursion when foreign table references itself
Date: 2026-05-12 14:06:08
Message-ID: CADY9qXeicGsG521Kz2hqoF4QJu4pOiK5ZSDySCnqyAEaUe0WAA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi, hackers.

If you create a foreign table referencing itself on a loopback server,
an unpleasant error occurs:

```sql
create extension if not exists postgres_fdw;

drop server if exists loopback cascade ;
create server loopback
foreign data wrapper postgres_fdw
options (dbname 'postgres', host 'localhost');

create user mapping for current_user server loopback;

create foreign table test_self (id int)
server loopback options (table_name 'test_self');
--> Ok

insert into test_self select 1;
--> Err
/*
[08001] ERROR: could not connect to server "loopback"
Detail: connection to server on socket "/tmp/.s.PGSQL.54321" failed:
FATAL: sorry, too many clients already
Where: remote SQL command: INSERT INTO public.test_self(id) VALUES ($1)
remote SQL command: INSERT INTO public.test_self(id) VALUES ($1)
remote SQL command: INSERT INTO public.test_self(id) VALUES ( ...
*/
```

The proposed patch fixes this error.

```sql
create foreign table test_self (id int)
server loopback options (table_name 'test_self');
--> Err
/*
[42P16] ERROR: foreign table "test_self" cannot reference itself
Hint: Foreign table pointing to the same table on the same database
creates circular reference
*/
```

John.

Attachment Content-Type Size
0001-Prevent-self-referencing-foreign-table-on-loopback.patch text/x-patch 1.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2026-05-12 14:13:57 Re: Proposal: Adding compression of temporary files
Previous Message Tomas Vondra 2026-05-12 13:42:32 Re: Add a greedy join search algorithm to handle large join problems