Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Arithmetic overflow in numeric precision causes clients to hang #3137

Open
1 task done
staticlibs opened this issue Nov 20, 2024 · 1 comment
Open
1 task done
Labels
bug Something isn't working

Comments

@staticlibs
Copy link
Contributor

What happened?

With a table like this:

create table tab1(col1 numeric(4, 2))
insert into tab1 values('12.345')

The following query causes an error in TdsSendTypeNumeric:

select case when col1 < 0 then cast(1 as numeric(3, 2)) else col1 end from tab1

This causes TDS clients to hang like this:

With ODBC sqlcmd:

> sqlcmd -S 192.168.178.58,1433 -U jdbc_user -P 12345678 -Q "select case when col1 < 0 then cast(1 as decimal(3, 2)) else col1 end from tab1"
col1
-----
Sqlcmd: Error: Internal error at ReadAndHandleColumnData (Reason: Error reading column data).

With Go sqlcmd:

> sqlcmd -S 127.0.0.1,1433 -U jdbc_user -P 12345678 -Q "select case when col1 < 0 then cast(1 as decimal(3, 2)) else col1 end from tab1"
runtime error: slice bounds out of range [:170] with capacity 17

AFAIU there is no actual arithmetic overflow here, it is the current type resolution logic for CASE expressions is not compatible with the precision handling logic in TDS response processing.

Version

BABEL_4_X_DEV (Default)

Extension

babelfishpg_tds

Which flavor of Linux are you using when you see the bug?

Fedora

Relevant log output

Backend log:

2024-11-20 10:44:47.283 GMT [44711] LOG:  Unmapped error found. Code: 50331778, Message: Arithmetic overflow error for data type numeric., File: tdstypeio.c, Line: 3233, Context: babelfishpg_tsql
2024-11-20 10:44:47.283 GMT [44711] CONTEXT:  PL/tsql function inline_code_block line 2 at SQL statement
	TDS Protocol: Message Type: SQL BATCH, Phase: TDS_REQUEST_PHASE_PROCESS. Writing the Tds response to the socket
2024-11-20 10:44:47.283 GMT [44711] LOG:  Unmapped error found. Code: 50331778, Message: Arithmetic overflow error for data type numeric., File: tdstypeio.c, Line: 3233, Context: babelfishpg_tsql
2024-11-20 10:44:47.283 GMT [44711] CONTEXT:  TDS Protocol: Message Type: SQL BATCH, Phase: TDS_REQUEST_PHASE_PROCESS. Writing the Tds response to the socket
2024-11-20 10:44:47.283 GMT [44711] ERROR:  Arithmetic overflow error for data type numeric.
2024-11-20 10:44:47.283 GMT [44711] CONTEXT:  PL/tsql function inline_code_block line 2 at SQL statement
	TDS Protocol: Message Type: SQL BATCH, Phase: TDS_REQUEST_PHASE_PROCESS. Writing the Tds response to the socket
2024-11-20 10:44:47.283 GMT [44711] LOG:  Unmapped error found. Code: 50331778, Message: Arithmetic overflow error for data type numeric., File: tdstypeio.c, Line: 3233, Context: TDS
2024-11-20 10:44:47.283 GMT [44711] CONTEXT:  TDS Protocol: Message Type: SQL BATCH, Phase: TDS_REQUEST_PHASE_PROCESS. Writing the Tds response to the socket


Backtrace:

babelfishpg_tds.so!TdsSendTypeNumeric(FmgrInfo * finfo, Datum value, void * vMetaData) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c:3233)
babelfishpg_tds.so!TdsPrintTup(TupleTableSlot * slot, DestReceiver * self) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c:3050)
ExecutePlan(EState * estate, PlanState * planstate, _Bool use_parallel_mode, CmdType operation, _Bool sendTuples, uint64 numberTuples, ScanDirection direction, DestReceiver * dest, _Bool execute_once) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/executor/execMain.c:1705)
standard_ExecutorRun(QueryDesc * queryDesc, ScanDirection direction, uint64 count, _Bool execute_once) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/executor/execMain.c:368)
pg_stat_statements.so!pgss_ExecutorRun(QueryDesc * queryDesc, ScanDirection direction, uint64 count, _Bool execute_once) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/contrib/pg_stat_statements/pg_stat_statements.c:1010)
babelfishpg_tsql.so!pltsql_ExecutorRun(QueryDesc * queryDesc, ScanDirection direction, uint64 count, _Bool execute_once) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/hooks.c:1021)
ExecutorRun(QueryDesc * queryDesc, ScanDirection direction, uint64 count, _Bool execute_once) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/executor/execMain.c:310)
PortalRunSelect(Portal portal, _Bool forward, long count, DestReceiver * dest) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/tcop/pquery.c:924)
PortalRun(Portal portal, long count, _Bool isTopLevel, _Bool run_once, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/tcop/pquery.c:768)
babelfishpg_tsql.so!execute_plan_and_push_result(PLtsql_execstate * estate, PLtsql_expr * expr, ParamListInfo paramLI) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/pl_exec-2.c:3882)
babelfishpg_tsql.so!exec_stmt_execsql(PLtsql_execstate * estate, PLtsql_stmt_execsql * stmt) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/pl_exec.c:4827)
babelfishpg_tsql.so!dispatch_stmt(PLtsql_execstate * estate, PLtsql_stmt * stmt) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/iterative_exec.c:666)
babelfishpg_tsql.so!dispatch_stmt_handle_error(PLtsql_execstate * estate, PLtsql_stmt * stmt, _Bool * terminate_batch, int active_non_tsql_procs, int active_sys_functions) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/iterative_exec.c:1301)
babelfishpg_tsql.so!exec_stmt_iterative(PLtsql_execstate * estate, ExecCodes * exec_codes, ExecConfig_t * config) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/iterative_exec.c:1625)
babelfishpg_tsql.so!pltsql_exec_function(PLtsql_function * func, FunctionCallInfo fcinfo, EState * simple_eval_estate, _Bool atomic) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/pl_exec.c:688)
babelfishpg_tsql.so!pltsql_inline_handler(FunctionCallInfo fcinfo) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tsql/src/pl_handler.c:5488)
babelfishpg_tds.so!ExecuteSQLBatch(char * query) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tdssqlbatch.c:94)
babelfishpg_tds.so!ProcessSQLBatchRequest(TDSRequest request) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tdssqlbatch.c:142)
babelfishpg_tds.so!ProcessTDSRequest(TDSRequest request) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tdsprotocol.c:422)
babelfishpg_tds.so!TdsSocketBackend() (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tdsprotocol.c:666)
babelfishpg_tds.so!pe_process_command() (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tds_srv.c:453)
PostgresMain(const char * dbname, const char * username) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/tcop/postgres.c:4729)
babelfishpg_tds.so!pe_mainfunc(Port * port) (/home/alex/projects/postgres/dev/babelfish_extensions/contrib/babelfishpg_tds/src/backend/tds/tds_srv.c:387)
BackendRun(Port * port) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/postmaster/postmaster.c:4641)
BackendStartup(Port * port) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/postmaster/postmaster.c:4368)
ServerLoop() (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/postmaster/postmaster.c:1953)
PostmasterMain(int argc, char ** argv) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/postmaster/postmaster.c:1499)
main(int argc, char ** argv) (/home/alex/projects/postgres/dev/postgresql_modified_for_babelfish/src/backend/main/main.c:198)

Code of Conduct

  • I agree to follow this project's Code of Conduct.
@Deepesh125
Copy link
Contributor

Thanks @staticlibs for reporting this issue. This is known issue and we are internally tracking it for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants