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
Recall & Review
beginner
What is a Foreign Data Wrapper (FDW) in PostgreSQL?
A Foreign Data Wrapper is a PostgreSQL feature that allows you to access data stored in external sources as if it were a regular table inside your database.
Click to reveal answer
beginner
How does PostgreSQL use Foreign Data Wrappers?
PostgreSQL uses FDWs to connect to external data sources like other databases, files, or web services, enabling queries across different systems seamlessly.
Click to reveal answer
beginner
What is a foreign table in the context of FDWs?
A foreign table is a table in PostgreSQL that represents data stored outside the database, accessed through a Foreign Data Wrapper.
Click to reveal answer
intermediate
Name one common use case for Foreign Data Wrappers.
One common use case is to query data from another database system like MySQL or MongoDB directly from PostgreSQL without moving the data.
Click to reveal answer
intermediate
What SQL command is used to create a foreign table?
The SQL command is CREATE FOREIGN TABLE, which defines a table linked to an external data source via an FDW.
Click to reveal answer
What does a Foreign Data Wrapper allow you to do in PostgreSQL?
ABackup the database automatically
BCreate new databases inside PostgreSQL
CAccess external data sources as if they were local tables
DEncrypt data stored in PostgreSQL
✗ Incorrect
Foreign Data Wrappers let you query external data sources like local tables.
Which SQL command creates a foreign table?
ACREATE TABLE
BCREATE FOREIGN TABLE
CCREATE EXTERNAL TABLE
DCREATE REMOTE TABLE
✗ Incorrect
CREATE FOREIGN TABLE defines a table linked to an external data source.
Which of these is NOT a typical use of Foreign Data Wrappers?
ARunning local PostgreSQL backups
BQuerying data from another database system
CAccessing files as tables
DConnecting to web services for data
✗ Incorrect
FDWs do not handle backups; they connect to external data sources.
What is a foreign table?
AA table representing external data accessed via FDW
BA table that stores encrypted data
CA table stored inside PostgreSQL only
DA temporary table used for backups
✗ Incorrect
A foreign table represents external data accessed through a Foreign Data Wrapper.
Which PostgreSQL feature helps you query data from MongoDB?
AStored Procedures
BTriggers
CViews
DForeign Data Wrapper
✗ Incorrect
Foreign Data Wrappers enable querying external databases like MongoDB.
Explain what a Foreign Data Wrapper is and how it helps in PostgreSQL.
Think about how you can see data from outside PostgreSQL as if it was inside.
You got /4 concepts.
Describe the steps to use a Foreign Data Wrapper to query data from another database.
Consider what you need to connect and then access external data.
You got /5 concepts.
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
Step 1: Understand FDW functionality
FDWs allow PostgreSQL to connect to external data sources and treat them like local tables.
Step 2: Compare options
Options A, B, and D describe unrelated features: caching, backups, and encryption, not FDWs.
Final Answer:
To access external data sources as if they were local tables -> Option D
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
Step 1: Recall correct syntax for creating foreign server
The correct syntax is: CREATE FOREIGN SERVER server_name FOREIGN DATA WRAPPER wrapper_name;
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.
Final Answer:
CREATE FOREIGN SERVER myserver FOREIGN DATA WRAPPER postgres_fdw; -> Option A
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
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.
Step 2: Analyze the SELECT query
The SELECT queries the foreign_table, which fetches data from the remote users table where id = 1.
Final Answer:
Retrieve rows from the remote table users in remotedb where id = 1 -> Option A
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
Step 1: Interpret the error message
The error says foreign server "foreign_srv" does not exist, meaning PostgreSQL cannot find that server definition.
Step 2: Check prerequisites for foreign table creation
Before creating a foreign table, the foreign server must be created. If missing, this error occurs.
Final Answer:
The foreign server foreign_srv was not created before creating the foreign table -> Option C
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
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.
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.
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
Quick Check:
FDW foreign table join local table directly [OK]
Hint: Create foreign table, then join like local tables [OK]