The Versatile file_fdw — Reading System Information from Your Database
Author: Vonng
PostgreSQL is the most advanced open-source database, and one of its killer features is FDW: Foreign Data Wrapper. Through FDW, users can access various external data sources from Postgres in a unified manner. file_fdw is one of the two FDWs that come bundled with the database. With the update to PostgreSQL 10, file_fdw has gained an awesome new capability: reading from program output.
This little powerhouse has endless possibilities. Through file_fdw, we can easily view operating system information, fetch network data, and feed various data sources into the database for unified viewing and management.
Installation and Configuration
file_fdw is a built-in PostgreSQL component that doesn’t require any special configuration. You can enable file_fdw in your database with a single command:
After enabling the FDW extension, you need to create an instance. This is also done with a single SQL statement. Let’s create an FDW Server instance named fs:
Creating Foreign Tables
For example, if I want to read information about running processes on the operating system from within the database, how would I do that?
The most typical and commonly used external data format is CSV. However, the output from system commands isn’t always well-formatted:
We can use awk to format the ps command output into CSV format with \x1F as the delimiter:
Now for the main event! Create a foreign table definition with the following DDL:
The key here is providing the appropriate parameters through OPTIONS in CREATE FOREIGN TABLE OPTIONS (xxxx). By specifying the command in the PROGRAM parameter, PostgreSQL will automatically execute this command when querying this table and read its output. The FORMAT parameter is set to CSV, the DELIMITER parameter is set to the previously used \x1F, and we ignore the first line of the CSV with HEADER 'TRUE'.
So what’s the result?

What’s It Good For?
In the simplest scenario, system metric monitoring that previously required writing various monitoring scripts deployed in random places, then regularly executing them to pull metrics and store them in a database — now through file_fdw, you can directly import the metrics of interest into database tables in one step. It’s easier to maintain, simpler to deploy, and more reliable. By adding views on top of foreign tables and regularly pulling aggregations, you can accomplish in the database what would normally require an entire monitoring system.
Since it can read output from programs, file_fdw can work with various powerful command-line tools in the Linux ecosystem, unleashing tremendous power.
More Examples
Along these lines, I later discovered that Facebook apparently has a similar product called OSQuery, which does pretty much the same thing — querying operating system metrics through SQL. But clearly the PostgreSQL approach is the most straightforward and efficient. Just define the table structure and command data source, and you can easily interface with metric data. You can build something with similar functionality in less than a day.
DDL for reading the system user list:
DDL for reading disk usage:
Of course, file_fdw is just a very basic FDW — for example, it’s read-only, you can’t modify data through it.
Writing your own FDW to implement CRUD logic is also quite simple. For example, Multicorn is a project for writing FDWs in Python.
SQL over everything — making the world simpler!
