tds-fdw / tds_fdw

A PostgreSQL foreign data wrapper to connect to TDS databases (Sybase and Microsoft SQL Server)
Other
357 stars 99 forks source link

Segmentation fault selecting geometry columns via foreign table #330

Open kinghuang opened 1 year ago

kinghuang commented 1 year ago

Issue report

I've imported a foreign table that has geometry columns. If I execute a select with any geometry columns included, a segmentation fault occurs. Selecting without any geometry columns work as expected.

Operating system

Using Amazon Aurora PostgreSQL.

Version of tds_fdw

2.0.2

Version of PostgreSQL

PostgreSQL 14.4 on aarch64-unknown-linux-gnu, compiled by aarch64-unknown-linux-gnu-gcc (GCC) 7.4.0, 64-bit

Version of FreeTDS

Unknown.

Logs

2022-12-09 20:40:37 UTC::@:[491]:LOG:  server process (PID 559) was terminated by signal 11: Segmentation fault
2022-12-09 20:40:37 UTC::@:[491]:DETAIL:  Failed process was running: SELECT * FROM env."TableNameHere" WHERE "PKColumn" = '12345';
2022-12-09 20:40:37 UTC::@:[491]:LOG:  terminating any other active server processes
2022-12-09 20:40:37 UTC::@:[491]:FATAL:  Can't handle storage runtime process crash
2022-12-09 20:40:37 UTC::@:[491]:LOG:  database system is shut down

Sentences, data structures, data

The actual object names have been replaced.

Schema import:

IMPORT FOREIGN SCHEMA "ENV" FROM SERVER foreign_server_here INTO env;

Example query:

SELECT "PKColumn", "ColumnWithGeometry"
FROM env."TableNameHere"
WHERE "PKColumn" = '12345';

Structure in PostgreSQL:

SELECT table_name, column_name, data_type 
FROM information_schema.columns
WHERE table_name = 'TableNameHere';
table_name column_name data_type
TableNameHere PKColumn character varying
TableNameHere ColumnWithGeometry text

Structure in SQL Server:

CREATE TABLE [ENV].[TableNameHere] (
    [PKColumn] varchar,
    [ColumnWithGeometry] geometry
);
GeoffMontee commented 1 year ago

To make this work, I think we would need to do a couple things at minimum:

  1. For IMPORT FOREIGN SCHEMA, we need a mapping from SQL Server's geometry type to a specific PostgreSQL type in the tdsImportSqlServerSchema() function - https://github.com/tds-fdw/tds_fdw/blob/16f3fc5cf97bc661021b076f5df3be97c151a42b/src/tds_fdw.c#L3226
  2. For SELECT, we would also need a conversion from SQL Server's geometry type to a specific PostgreSQL type in the tdsIterateForeignScan() function - https://github.com/tds-fdw/tds_fdw/blob/16f3fc5cf97bc661021b076f5df3be97c151a42b/src/tds_fdw.c#L1793

Other changes might also be required.