ogr2ogr with missing geometries crashes postgresql with segmentation fault
Jorge Gustavo Rocha
jgr at di.uminho.pt
Fri Dec 12 17:19:29 PST 2025
Hi,
TL;DR
A GIST index on a EMPTY POLYGON (encoded by GDAL) crashes postgresql
with segmentation fault.
drop table if exists fault;
create table fault (fid integer, geom geometry(Polygon,5015));
INSERT INTO fault (fid, geom) VALUES (1,
'0103000020971300000100000000000000'::GEOMETRY);
CREATE INDEX fault_geom_index ON fault USING GIST (geom);
Postgresql log:
2025-12-13 00:53:11.879 UTC [12360] LOG: client backend (PID 18218) was
terminated by signal 11: Segmentation fault
2025-12-13 00:53:11.879 UTC [12360] DETAIL: Failed process was running:
CREATE INDEX fault_geom_index ON fault USING GIST (geom);
2025-12-13 00:53:11.879 UTC [12360] LOG: terminating any other active
server processes
2025-12-13 00:53:11.881 UTC [18263] postgres at idea FATAL: the database
system is in recovery mode
2025-12-13 00:53:11.883 UTC [12360] LOG: all server processes
terminated; reinitializing
2025-12-13 00:53:11.899 UTC [18267] LOG: database system was
interrupted; last known up at 2025-12-13 00:52:07 UTC
2025-12-13 00:53:11.909 UTC [18267] LOG: database system was not
properly shut down; automatic recovery in progress
That geometry was encoded by ogr2ogr:
idea=# select ST_AsEWKT('0103000020971300000100000000000000'::GEOMETRY);
st_asewkt
-------------------------
SRID=5015;POLYGON EMPTY
The same geometry encoded by Postgis does not crashes postgresql.
drop table if exists fault;
create table fault (fid integer, geom geometry(Polygon,5015));
INSERT INTO fault (fid, geom) VALUES (2,
ST_GeomFromEWKT('SRID=5015;POLYGON EMPTY'));
CREATE INDEX fault_geom_index ON fault USING GIST (geom);
No errors.
Different encodings. Both valid using st_isvalid():
ogr2ogr encode of 'SRID=5015;POLYGON EMPTY':
0103000020971300000100000000000000
postgis encode of 'SRID=5015;POLYGON EMPTY': 01030000209713000000000000
idea=# select
st_isvalid('0103000020971300000100000000000000'::GEOMETRY),
st_isvalid('01030000209713000000000000'::GEOMETRY);
st_isvalid | st_isvalid
------------+------------
t | t
/TL;DR
Long story
I have a postgresql database that is crashing a few times, with
Segmentation fault.
Users are uploading data (they called it data!) with ogr2ogr. The files
have issues, like missing geometries and so on. I'm attaching one of
such files.
Using QGIS
If I run the validity check on QGIS the file is ok, but there are
missing geometries. QGIS doesn't complain about it.
Uploading the file to postgis within QGIS fails, but fails with a nice
error:
2025-12-12 22:51:18.871 UTC [13255] idea at idea ERROR: Polygon must have
at least four points in each ring
2025-12-12 22:51:18.871 UTC [13255] idea at idea CONTEXT: SQL function
"st_geomfromwkb" statement 1
2025-12-12 22:51:18.871 UTC [13255] idea at idea STATEMENT: INSERT INTO
"upload"."geojson —
8a2bdfa0859a7663b1e3f3b947f933ed"("geom","fid","tema","igt","freguesia","concelho","shape_length")
VALUES (st_geomfromwkb($1::bytea,5015),$2,'Limite de Concelho','Plano
Regional de Ordenamento do Território',NULL,$3,$4)
That is what I was expecting. Errors reported, but no segmentation fault.
Using ogr2ogr
Using ogr2ogr to upload the same files crashes the postgresql server.
ogr2ogr "PG:host='x.x.x.x' port='xxxx' user='xxx' password='xxx'
dbname='idea' active_schema=upload" 8a2bdfa0859a7663b1e3f3b947f933ed.geojson
Postgresql log:
2025-12-12 23:06:08.215 UTC [12360] LOG: client backend (PID 13893) was
terminated by signal 11: Segmentation fault
2025-12-12 23:06:08.215 UTC [12360] DETAIL: Failed process was running:
COPY "8a2bdfa0859a7663b1e3f3b947f933ed" ("wkb_geometry", "tema", "igt",
"freguesia", "concelho", "shape_length") FROM STDIN;
2025-12-12 23:06:08.215 UTC [12360] LOG: terminating any other active
server processes
2025-12-12 23:06:08.219 UTC [12360] LOG: all server processes
terminated; reinitializing
2025-12-12 23:06:08.235 UTC [13898] LOG: database system was
interrupted; last known up at 2025-12-12 23:04:25 UTC
2025-12-12 23:06:08.247 UTC [13898] LOG: database system was not
properly shut down; automatic recovery in progress
Differences
QGIS upload is using INSERT and st_geomfromwkb.
ogr2ogr is using COPY, but encodes the geometry beforehand.
I've tried ogr2ogr with INSERT, but the crash happens again.
PG_USE_COPY=NO ogr2ogr -skipfailures "PG:host='x.x.x.x' port='xxxx'
user='xxx' password='xxx' dbname='idea' active_schema=upload"
/home/qgis/public_html/uploads/8a2bdfa0859a7663b1e3f3b947f933ed.geojson
2025-12-12 23:59:01.494 UTC [12360] LOG: client backend (PID 16042) was
terminated by signal 11: Segmentation fault
2025-12-12 23:59:01.494 UTC [12360] DETAIL: Failed process was running:
INSERT INTO "8a2bdfa0859a7663b1e3f3b947f933ed" ("wkb_geometry" , "tema",
"igt", "freguesia", "concelho", "shape_length") VALUES
('0103000020971300000100000000000000'::GEOMETRY, 'Limite de Concelho',
'Plano Regional de Ordenamento do Territ??rio', NULL, NULL,
0.106232057407228) RETURNING "ogc_fid"
2025-12-12 23:59:01.494 UTC [12360] LOG: terminating any other active
server processes
2025-12-12 23:59:01.498 UTC [12360] LOG: all server processes
terminated; reinitializing
2025-12-12 23:59:01.514 UTC [16047] LOG: database system was
interrupted; last known up at 2025-12-12 23:58:01 UTC
2025-12-12 23:59:01.526 UTC [16047] LOG: database system was not
properly shut down; automatic recovery in progress
The ogr2ogr encoded geometry that caused the segmentation fault is valid.
select st_isvalid('0103000020971300000100000000000000'::GEOMETRY);
Isolating the problem
The following query raised the segmentation fault:
INSERT INTO upload."8a2bdfa0859a7663b1e3f3b947f933ed" ("wkb_geometry" ,
"tema", "igt", "freguesia", "concelho", "shape_length") VALUES
('0103000020971300000100000000000000'::GEOMETRY, 'Limite de Concelho',
'Plano Regional de Ordenamento', NULL, NULL, 0.106232057407228);
That geometry is:
idea=# select ST_AsEWKT('0103000020971300000100000000000000'::GEOMETRY);
st_asewkt
-------------------------
SRID=5015;POLYGON EMPTY
This alternative works:
INSERT INTO upload."8a2bdfa0859a7663b1e3f3b947f933ed" ("wkb_geometry" ,
"tema", "igt", "freguesia", "concelho", "shape_length") VALUES
(ST_GeomFromEWKT('SRID=5015;POLYGON EMPTY'), 'Limite de Concelho',
'Plano Regional de Ordenamento', NULL, NULL, 0.106232057407228);
After further investigation, I found out that the problem is the GIST
index. If the index exists, when we do the INSERTS with ogr2ogr
encoded empty polygons, the server crashes. If there is no index, there
is no problem.
Further comments are welcome. I don't know exactly if it is a Postgis or
a GDAL problem, but this should not crash Postgresql.
Environment
This Segmentation fault happens with Postgresql 17 or 18 + Postgis 3.6.1
+ Ubuntu 24.04.3:
POSTGIS="3.6.1 f533623" [EXTENSION] PGSQL="180"
GEOS="3.12.1-CAPI-1.18.1" PROJ="9.4.0 NETWORK_ENABLED=OFF
URL_ENDPOINT=https://cdn.proj.org USER_WRITABLE_DIRECTORY=/tmp/proj
DATABASE_PATH=/usr/share/proj/proj.db" (
compiled against PROJ 9.4.0) GDAL="GDAL 3.8.4, released 2024/02/08"
LIBXML="2.9.14" LIBJSON="0.17" LIBPROTOBUF="1.4.1" WAGYU="0.5.0
(Internal)" RASTER
qgis at s0618idea02:~$ dpkg -l | grep gdal
ii gdal-bin 3.8.4+dfsg-3ubuntu3
amd64 Geospatial Data Abstraction Library - Utility
programs
ii gdal-data 3.8.4+dfsg-3ubuntu3
all Geospatial Data Abstraction Library - Data files
ii gdal-plugins:amd64 3.8.4+dfsg-3ubuntu3
amd64 Geospatial Data Abstraction Library - Plugins
ii libgdal34t64:amd64 3.8.4+dfsg-3ubuntu3
amd64 Geospatial Data Abstraction Library
ii python3-gdal 3.8.4+dfsg-3ubuntu3
amd64 Python 3 bindings to the Geospatial Data
Abstraction Library
Regards,
Jorge
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 8a2bdfa0859a7663b1e3f3b947f933ed.geojson
Type: application/geo+json
Size: 106583 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20251213/5cbc00df/attachment.bin>
More information about the postgis-users
mailing list