Senior LevelPostgreSQL
How do you build a custom Foreign Data Wrapper in C using the FDW API?
❓ Interview Prompt: Explain FDW C API Development, `GetForeignRelSize`, `GetForeignPaths`, `GetForeignPlan`, `BeginForeignScan`, `IterateForeignScan`, and custom storage wrappers.
Building a custom Foreign Data Wrapper (FDW) in C involves implementing callback functions registered in an 'FdwRoutine' struct:
Key FDW Callback Routines:
1
'GetForeignRelSize': Estimates row count and tuple size of external data source.
2
'GetForeignPaths': Passes cost paths to the Query Planner.
3
'IterateForeignScan': Fetches and returns tuples line-by-line during query execution.
Code Implementation Example
c
// C Foreign Data Wrapper Callback Handler
Datum custom_fdw_handler(PG_FUNCTION_ARGS) {
FdwRoutine *fdwroutine = makeNode(FdwRoutine);
fdwroutine->GetForeignRelSize = customGetForeignRelSize;
fdwroutine->IterateForeignScan = customIterateForeignScan;
PG_RETURN_POINTER(fdwroutine);
}💡C Foreign Data Wrapper callback routine handler.
🎯 Interview Key Takeaway
Custom Foreign Data Wrappers allow PostgreSQL to query arbitrary custom APIs and binary data formats as standard SQL tables.
#FDW API#Custom FDW#C Extensions#Senior
Back to all PostgreSQL Questions→