0
0
PostgreSQLquery~10 mins

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

Choose your learning style9 modes available
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.