Bird
Raised Fist0
PostgreSQLquery~10 mins

Foreign data wrappers concept in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Foreign data wrappers concept
Start Query on Local DB
Check if Table is Foreign
Use FDW to
Connect to
Remote Source
Return Local Data
Fetch Data
Return Data to Local DB
Query Result Returned
When a query accesses a foreign table, PostgreSQL uses the foreign data wrapper (FDW) to connect to the remote source, fetch data, and return it as if it were local.
Execution Sample
PostgreSQL
CREATE FOREIGN TABLE foreign_customers (
  id INT,
  name TEXT
) SERVER foreign_server;

SELECT * FROM foreign_customers;
This creates a foreign table linked to a remote server and queries all rows from it.
Execution Table
StepActionEvaluationResult
1Query starts on local DBSELECT * FROM foreign_customersQuery recognized as foreign table access
2Check if table is foreignforeign_customers is foreignYes, use FDW
3FDW connects to remote serverConnection establishedReady to fetch data
4FDW sends query to remote sourceSELECT * FROM foreign_customersRemote source executes query
5Remote source returns dataRows receivedData ready to send back
6FDW returns data to local DBData receivedData available for local query
7Local DB returns query resultRows from foreign tableUser sees remote data as local
8Query endsNo more dataExecution complete
💡 Query ends after all remote data is fetched and returned to local database
Variable Tracker
VariableStartAfter Step 3After Step 5Final
connection_statusdisconnectedconnectedconnectedclosed after query
data_bufferemptyemptyrows receivedrows returned
query_statenot startedrunning remote querydata fetchedcompleted
Key Moments - 3 Insights
Why does the local database need to check if the table is foreign before querying?
Because foreign tables require using the FDW to fetch data from a remote source, unlike local tables which are accessed directly. This is shown in execution_table step 2.
What happens if the remote server is unreachable during the query?
The FDW cannot fetch data, so the query will fail or timeout. This would stop the process at execution_table step 3 where connection is established.
Does the local database store the remote data permanently?
No, the data is fetched on demand and returned for the query. It is not stored locally unless explicitly copied. This is shown in execution_table steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the FDW send the query to the remote source?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Check the 'Action' column for when the FDW sends the query to the remote source.
According to variable_tracker, what is the state of 'data_buffer' after step 5?
Arows returned
Bempty
Crows received
Ddisconnected
💡 Hint
Look at the 'data_buffer' row under 'After Step 5' column.
If the table was not foreign, which step in execution_table would be skipped?
AStep 4
BStep 6
CStep 2
DStep 7
💡 Hint
Step 4 involves sending query to remote source, which only happens for foreign tables.
Concept Snapshot
Foreign Data Wrappers (FDW) let PostgreSQL query external data sources as if they were local tables.
Create foreign tables linked to remote servers.
When queried, FDW connects remotely, fetches data, and returns it.
Local DB treats foreign data like local data during query.
Useful for integrating different databases or external data seamlessly.
Full Transcript
Foreign data wrappers in PostgreSQL allow the database to access tables that are not stored locally but exist on remote servers or other data sources. When a query runs on a foreign table, PostgreSQL checks if the table is foreign. If yes, it uses the FDW to connect to the remote server, send the query, fetch the data, and return it as if it were local. This process involves establishing a connection, sending the query remotely, receiving rows, and returning them to the local database. The data is fetched on demand and not stored permanently unless copied. If the remote server is unreachable, the query fails. This mechanism enables seamless integration of external data sources into PostgreSQL queries.

Practice

(1/5)
1. What is the main purpose of a Foreign Data Wrapper (FDW) in PostgreSQL?
easy
A. To speed up local query execution by caching results
B. To create backup copies of local tables automatically
C. To encrypt data stored in PostgreSQL tables
D. To access external data sources as if they were local tables

Solution

  1. Step 1: Understand FDW functionality

    FDWs allow PostgreSQL to connect to external data sources and treat them like local tables.
  2. Step 2: Compare options

    Options A, B, and D describe unrelated features: caching, backups, and encryption, not FDWs.
  3. Final Answer:

    To access external data sources as if they were local tables -> Option D
  4. Quick Check:

    FDW = Access external data like local tables [OK]
Hint: FDWs connect external data as local tables [OK]
Common Mistakes:
  • Confusing FDW with backup or caching features
  • Thinking FDW encrypts data
  • Assuming FDW only works with local data
2. Which of the following is the correct SQL command to create a foreign server named myserver using the postgres_fdw wrapper?
easy
A. CREATE FOREIGN SERVER myserver FOREIGN DATA WRAPPER postgres_fdw;
B. CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw;
C. CREATE SERVER myserver USING FOREIGN DATA WRAPPER postgres_fdw;
D. CREATE FOREIGN SERVER myserver USING postgres_fdw;

Solution

  1. Step 1: Recall correct syntax for creating foreign server

    The correct syntax is: CREATE FOREIGN SERVER server_name FOREIGN DATA WRAPPER wrapper_name;
  2. Step 2: Match options to syntax

    CREATE FOREIGN SERVER myserver FOREIGN DATA WRAPPER postgres_fdw; matches exactly. Options A and C miss the FOREIGN keyword before SERVER. CREATE FOREIGN SERVER myserver USING postgres_fdw; uses USING incorrectly.
  3. Final Answer:

    CREATE FOREIGN SERVER myserver FOREIGN DATA WRAPPER postgres_fdw; -> Option A
  4. Quick Check:

    CREATE FOREIGN SERVER ... FOREIGN DATA WRAPPER ... [OK]
Hint: Use CREATE FOREIGN SERVER ... FOREIGN DATA WRAPPER ... [OK]
Common Mistakes:
  • Omitting FOREIGN keyword before SERVER
  • Using USING instead of FOREIGN DATA WRAPPER
  • Mixing order of keywords
3. Given the following commands in PostgreSQL:
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER foreign_srv FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '192.168.1.10', dbname 'remotedb');
CREATE USER MAPPING FOR current_user SERVER foreign_srv OPTIONS (user 'remoteuser', password 'remotepass');
CREATE FOREIGN TABLE foreign_table (id INT, name TEXT) SERVER foreign_srv OPTIONS (schema_name 'public', table_name 'users');
SELECT * FROM foreign_table WHERE id = 1;

What will the SELECT query do?
medium
A. Retrieve rows from the remote table users in remotedb where id = 1
B. Retrieve rows from the local table named foreign_table
C. Cause a syntax error because foreign_table is not a local table
D. Return an empty result because no data is copied locally

Solution

  1. Step 1: Understand foreign table mapping

    The foreign table foreign_table maps to the remote table users in the remotedb database on host 192.168.1.10.
  2. Step 2: Analyze the SELECT query

    The SELECT queries the foreign_table, which fetches data from the remote users table where id = 1.
  3. Final Answer:

    Retrieve rows from the remote table users in remotedb where id = 1 -> Option A
  4. Quick Check:

    Foreign table SELECT fetches remote data [OK]
Hint: Foreign tables query remote data transparently [OK]
Common Mistakes:
  • Thinking foreign_table is local data
  • Expecting syntax error on foreign table
  • Assuming data is copied locally automatically
4. You run this command:
CREATE FOREIGN TABLE foreign_table (id INT, name TEXT) SERVER foreign_srv OPTIONS (schema_name 'public', table_name 'users');

But get an error: ERROR: foreign server "foreign_srv" does not exist
What is the most likely cause?
medium
A. The user mapping for foreign_srv is missing
B. The foreign table syntax is incorrect and missing parentheses
C. The foreign server foreign_srv was not created before creating the foreign table
D. The remote table users does not exist in the remote database

Solution

  1. Step 1: Interpret the error message

    The error says foreign server "foreign_srv" does not exist, meaning PostgreSQL cannot find that server definition.
  2. Step 2: Check prerequisites for foreign table creation

    Before creating a foreign table, the foreign server must be created. If missing, this error occurs.
  3. Final Answer:

    The foreign server foreign_srv was not created before creating the foreign table -> Option C
  4. Quick Check:

    Foreign server must exist before foreign table [OK]
Hint: Create foreign server before foreign table [OK]
Common Mistakes:
  • Ignoring the need to create foreign server first
  • Assuming user mapping causes this error
  • Thinking syntax error causes this message
5. You want to combine data from a local table orders and a remote table customers accessed via FDW named cust_fdw. Which approach correctly joins these tables in PostgreSQL?
hard
A. Create a view combining orders and customers on the remote server
B. Create foreign table for customers via cust_fdw, then run: SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id;
C. Use dblink to fetch customers data inside a subquery, then join with orders
D. Copy remote customers data into a local table, then join locally

Solution

  1. Step 1: Understand FDW usage for joining local and remote data

    FDW allows creating a foreign table for remote customers, so you can join it directly with local orders.
  2. Step 2: Evaluate options for combining data

    Create foreign table for customers via cust_fdw, then run: SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id; uses FDW foreign table and a direct join, which is correct and efficient. Copy remote customers data into a local table, then join locally copies data manually, less dynamic. Use dblink to fetch customers data inside a subquery, then join with orders uses dblink, more complex. Create a view combining orders and customers on the remote server creates a view on remote server, not accessible locally.
  3. Final Answer:

    Create foreign table for customers via cust_fdw, then run: SELECT * FROM orders JOIN customers ON orders.customer_id = customers.id; -> Option B
  4. Quick Check:

    FDW foreign table join local table directly [OK]
Hint: Create foreign table, then join like local tables [OK]
Common Mistakes:
  • Trying to join without creating foreign table
  • Copying remote data manually instead of FDW
  • Using dblink unnecessarily for simple joins