ogr_fdw

GIS 数据外部数据源包装器

概览

扩展包名版本分类许可证语言
ogr_fdw1.1.9GISMITC
ID扩展名BinLibLoadCreateTrustReloc模式
1550ogr_fdw-
相关扩展postgis file_fdw postgres_fdw postgis_topology postgis_raster postgis_sfcgal postgis_tiger_geocoder address_standardizer

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.1.91817161514ogr_fdw-
RPMPGDG1.1.91817161514ogr_fdw_$v-
DEBPGDG1.1.91817161514postgresql-$v-ogr-fdw-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64
u26.x86_64
u26.aarch64

安装

您可以直接安装 ogr_fdw 扩展包的预置二进制包,首先确保 PGDG 仓库已经添加并启用:

pig repo add pgdg -u          # 添加 PGDG 仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install ogr_fdw;          # 当前活跃 PG 版本安装
pig ext install -y ogr_fdw -v 18  # PG 18
pig ext install -y ogr_fdw -v 17  # PG 17
pig ext install -y ogr_fdw -v 16  # PG 16
pig ext install -y ogr_fdw -v 15  # PG 15
pig ext install -y ogr_fdw -v 14  # PG 14
dnf install -y ogr_fdw_18       # PG 18
dnf install -y ogr_fdw_17       # PG 17
dnf install -y ogr_fdw_16       # PG 16
dnf install -y ogr_fdw_15       # PG 15
dnf install -y ogr_fdw_14       # PG 14
apt install -y postgresql-18-ogr-fdw   # PG 18
apt install -y postgresql-17-ogr-fdw   # PG 17
apt install -y postgresql-16-ogr-fdw   # PG 16
apt install -y postgresql-15-ogr-fdw   # PG 15
apt install -y postgresql-14-ogr-fdw   # PG 14

创建扩展

CREATE EXTENSION ogr_fdw;

用法

来源:

ogr_fdw 将 GDAL/OGR 支持的矢量数据公开为 PostgreSQL 外部表。它可以通过 OGR 驱动读取文件和远程数据源;当所选驱动及数据源支持更新时,也可以写入。应先安装 PostGIS,再安装 ogr_fdw,以获得原生 geometry 列;否则 geometry 会以 WKB bytea 形式公开。

发现并导入图层

使用随扩展安装的辅助工具检查数据源并生成匹配的 SQL:

ogr_fdw_info -s /srv/gis/cities.gpkg
ogr_fdw_info -s /srv/gis/cities.gpkg -l cities

等价的最小定义如下:

CREATE EXTENSION postgis;
CREATE EXTENSION ogr_fdw;

CREATE SERVER city_source
  FOREIGN DATA WRAPPER ogr_fdw
  OPTIONS (
    datasource '/srv/gis/cities.gpkg',
    format 'GPKG'
  );

CREATE FOREIGN TABLE city (
  fid bigint,
  geom geometry,
  name text
) SERVER city_source
  OPTIONS (layer 'cities');

SELECT fid, name FROM city WHERE geom && ST_MakeEnvelope(-10, 35, 30, 60, 4326);

对于文件型数据源,PostgreSQL 服务器账号需要相应的文件系统权限;对于远程驱动,还需要网络和凭据访问权限。

导入与映射

CREATE SCHEMA gis_import;

IMPORT FOREIGN SCHEMA ogr_all
  LIMIT TO (cities)
  FROM SERVER city_source
  INTO gis_import;

ogr_all 表示全部 OGR 图层。导入通常会规范化表名和列名;需要保留远端原名时,使用 launder_table_nameslaunder_column_names 选项。外部列可以通过 OPTIONS (column_name 'RemoteName') 映射到不同的源字段名。

重要选项与对象

  • 服务器选项:必需的 datasource,以及可选的 formatupdateableconfig_optionsopen_optionscharacter_encoding
  • 表选项:layer 指定 OGR 图层,updateable 可用于禁用写入。
  • fid 标识要素;可写外部表必须提供该字段。
  • ogr_fdw_info 列出驱动与图层,并输出服务器/外部表定义。
  • ogr_fdw_version() 返回扩展与 GDAL 版本。
  • ogr_fdw_drivers() 列出编译进来的 OGR 驱动。

性能与写入边界

简单比较和边界框 && 谓词可以下推,但复杂过滤条件可能在本地执行。该 FDW 会取回所有选中的源字段,并为每条查询打开两个 OGR 连接,而不会进行连接池化。请使用 EXPLAIN,只投影所需字段,并针对实际驱动和数据源做基准测试。

写入能力取决于驱动,并要求数据源级写权限以及 fid。必须保持只读的数据源应设置 updateable = false。1.1.9 相比 1.1.8 简化了版本字符串,没有记录 SQL 工作流变化;控制文件中的 SQL 扩展版本仍为 1.1。