pramsey / pgsql-ogr-fdw

PostgreSQL foreign data wrapper for OGR
MIT License
240 stars 34 forks source link

Querying information_schema.tables.is_insertable_into with vsizip datasource errors #221

Open robe2 opened 3 years ago

robe2 commented 3 years ago
CREATE EXTENSION ogr_fdw;
CREATE SCHEMA staging;
DROP SERVER IF EXISTS fds_ne CASCADE;
CREATE SERVER fds_ne 
    FOREIGN DATA WRAPPER ogr_fdw
    OPTIONS(datasource '/vsizip//vsicurl/http://postgis.us/downloads/ne_10m_admin_1_states_provinces.zip', format 'ESRI Shapefile');

IMPORT FOREIGN SCHEMA ogr_all FROM SERVER fds_ne INTO staging; 

I also tried with: datasources which are equivalent: /fdw_data/argentina-latest-free.shp.zip and /vsizip//fdw_data/argentina-latest-free.shp.zip. Both work as far as linking and querying but the one with /vsizip chokes in the information_schema.tables query

CREATE SERVER fds_ne 
    FOREIGN DATA WRAPPER ogr_fdw
    OPTIONS(datasource '/vsizip//http://postgis.us/downloads/ne_10m_admin_1_states_provinces.zip', format 'ESRI Shapefile');

Works fine outputs:

NOTICE:  Number of tables to be created 1
IMPORT FOREIGN SCHEMA

This also works:

SELECT f_table_name, f_geometry_column, coord_dimension, srid, type 
FROM geometry_columns 
WHERE f_table_schema = 'staging';

This gives error, but leaving out the is_insertable_into column, it works okay

SELECT table_name, table_type, is_insertable_into 
FROM information_schema.tables 
WHERE table_schema = 'staging';

Error:

ERROR:  GDAL OpenFailed [4] Unable to open /vsizip//vsicurl/http://postgis.us/downloads/ne_10m_admin_1_states_provinces.zip/ne_10m_admin_1_states_provinces.shp or /vsizip//vsicurl/http://postgis.us/downloads/ne_10m_admin_1_states_provinces.zip/ne_10m_admin_1_states_provinces.SHP.
SQL state: XX000

Expected behavior is the query does not error and the is_insertable_into has a false in output because vszip are not editable.

My Version info

SELECT ogr_fdw_version();

outputs:

OGR_FDW="1.1" GDAL="3.2.0"
pramsey commented 3 years ago

Minimal reproduction:

select pg_relation_is_updatable('staging.ne_10m_admin_1_states_provinces'::regclass,false);
pramsey commented 3 years ago

OK, I'm pretty sure I see what's happening, but not sure how to fix it without an axe. The updateable test is trying a "soft updateable open" on the file to see if its openable, but the GDAL open process is tossing a CE_FATAL error when it cannot open one of the shp components in updateable mode. That in turn gets mapped in the OGR error handler to a PgSQL FATAL which stops everything dead, so the logic further on, that checks for a NULL layer and sets the updateable to False never gets to run.

It may be that the soft update check logic just never worked at all, and never had a chance to be tested. I should try opening a local shp file that has only read bits set.

Workaround would be to declare the layer not updateable in the layer options, since that check gets done before the opening test.