Skip to content

Instantly share code, notes, and snippets.

@pramsey
Created January 5, 2016 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pramsey/80ddf002b9ea38c79e90 to your computer and use it in GitHub Desktop.
Save pramsey/80ddf002b9ea38c79e90 to your computer and use it in GitHub Desktop.
Create a recursive connections in PostgreSQL FDW, exhausting your client connections
DO $d$
BEGIN
EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname '$$||current_database()||$$',
port '$$||current_setting('port')||$$'
)$$;
END;
$d$;
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
CREATE TABLE foo ( id integer );
CREATE FOREIGN TABLE foo_fdw ( id integer ) server loopback;
SELECT * FROM foo_fdw;
@pramsey
Copy link
Author

pramsey commented Jan 5, 2016

The default target table in the CREATE FOREIGN TABLE statement is just the name of the foreign table itself (the code assumes the table will be remote) so this sets up a self reference. Each attempt to examine foo_fdw leads the backend to open a new connection to figure out what the structure of foo_fdw is. Even though each call only makes one new connection, all connections are eventually exhausted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment