Skip to content

SQLDisconnect never called #96

Closed
@mbona92

Description

Hi,

I'm using odbc_fdw to connect to a Teradata database.
Each time I run a query, three connections to the Teradata database are opened and the connections remain open until the session ends.
So if I run 3 queries in the same session:

fdw=> select * from lvl_teradata.table1 limit 1;
 STAB | PRATICA | FIDO | SUB | PER_AMM | TIP_RAT | NUM_RAT 
------+---------+------+-----+---------+---------+---------
(0 rows)

fdw=> select * from lvl_teradata.table1 limit 1;
 STAB | PRATICA | FIDO | SUB | PER_AMM | TIP_RAT | NUM_RAT 
------+---------+------+-----+---------+---------+---------
(0 rows)

fdw=> select * from lvl_teradata.table1 limit 1;
 STAB | PRATICA | FIDO | SUB | PER_AMM | TIP_RAT | NUM_RAT 
------+---------+------+-----+---------+---------+---------
(0 rows)

9 connections to Teradata will be open:

tcp        0      0 10.129.218.138:45278    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:36370    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:39168    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:36876    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:36382    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:56412    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:36888    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:46476    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 
tcp        0      0 10.129.218.138:39156    10.242.31.130:1025      ESTABLISHED 44557/postgres: fdw 

Debugging odbc_fdw I see that odbc_connection, in my case, is called first by odbcGetForeignRelSize, then by odbcEstimateCosts and finally by odbcBeginForeignScan.

odbcGetForeignRelSize and odbcEstimateCosts call odbcGetTableSize in turn.
At the end of the odbcGetTableSize function there are the following lines of code:

odbc_fdw/odbc_fdw.c

Lines 878 to 895 in 49579ac

/* Free handles, and disconnect */
if (stmt)
{
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
stmt = NULL;
}
if (dbc)
{
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
dbc = NULL;
}
if (env)
{
SQLFreeHandle(SQL_HANDLE_ENV, env);
env = NULL;
}
if (dbc)
SQLDisconnect(dbc);

SQLDisconnect will never be called because the dbc pointer is freed by SQLFreeHandle (SQL_HANDLE_DBC, dbc) and dbc = NULL.
I think SQLDisconnect needs to be moved before SQLFreeHandle (SQL_HANDLE_DBC, dbc).

Furthermore, there is no closure after the end of the table scan (odbcEndForeignScan?).

Thanks,
Matteo

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions