Skip to content

ocgeo_fdw

Foreign data wrapper for the OpenCageData API

ocgeo_fdw · Foreign data wrapper for the OpenCageData API

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100370ocgeo_fdwocgeo_fdw1.0FDWPostgreSQLC

PostgreSQL · 10, 11, 12, 13, 14

Usage

Sources:

ocgeo_fdw version 1.0 maps OpenCage’s JSON geocoding API to a read-only foreign table. An equality condition on the q input column performs forward or reverse geocoding; a minimum confidence condition can be sent as an API parameter. Returned fields can include the raw JSON, PostgreSQL point and box values, formatted address, confidence, and address components.

Server, mapping, and query

CREATE EXTENSION ocgeo_fdw;

CREATE SERVER ocdata_server
FOREIGN DATA WRAPPER ocgeo_fdw
OPTIONS (uri 'https://api.opencagedata.com/geocode/v1/json');

CREATE USER MAPPING FOR current_user
SERVER ocdata_server
OPTIONS (api_key '<api-key>', max_reqs_sec '1');

CREATE FOREIGN TABLE geocode_api (
    json_response jsonb,
    location point,
    confidence integer,
    formatted text,
    q text
) SERVER ocdata_server;

SELECT confidence, location, formatted
FROM geocode_api
WHERE q = 'Eiffel Tower, France' AND confidence >= 5;

Without a usable q equality restriction, a simple scan makes no API request. A nested-loop join can make one request per outer row.

Network, quota, and observability limits

Every API request is synchronous inside the PostgreSQL backend and can hold a transaction, connection, and locks while DNS, TLS, rate-limit sleep, and remote execution complete. The built-in limiter controls requests per second only within one backend. It does not enforce daily quota globally across sessions. Joins and rescans can multiply calls, so materialize or cache stable results.

The API key is stored in a user mapping. Debug output can include the full request URL; the README explicitly shows enabling client debug to view it, so do not enable that logging where the key could enter client or server logs. Restrict server usage, apply outbound allowlists, use a low-privilege key, set statement timeouts, and monitor the provider’s authoritative quota counters.

The ocgeo_stats function reports process-local in-memory counters, can include entries for roles used by that backend, and is incorrectly declared IMMUTABLE even though the values change after requests. Do not use it as an audit or billing source. The reviewed source documents only PostgreSQL 9.6 through 14 and has not changed since 2021; libcurl, planner APIs, JSON parsing, TLS policy, provider response fields, and current server majors all require fresh integration tests.

Table reader