babelfish-for-postgresql / babelfish_extensions

Babelfish for PostgreSQL provides the capability for PostgreSQL to work with applications written for Microsoft SQL Server. Babelfish understands the SQL Server wire-protocol and T-SQL, the Microsoft SQL Server query and procedural language, so you don’t have to switch database drivers or rewrite all of your application queries.
https://babelfishpg.org/
Apache License 2.0
274 stars 93 forks source link

Fix crash in some cases of recursive procedure calls #2648

Closed tanscorpio7 closed 2 months ago

tanscorpio7 commented 3 months ago

Description

Fix crash when we recursively execute a function or procedure and the call was made from the same line of the procedure. For ex:

CREATE PROCEDURE babel_4782_p(@in INT) AS
IF (@in = 1)
BEGIN
        SELECT 1;
END
ELSE
BEGIN
        SET @in = @in - 1
        /*
         * Below line will call the procedure babel_4782_p recursively. The plan for this call stmt is
         * cached in PLtsql_stmt_exec node. Now when we recursively execute the same line again, 
         * we will use the cached PLtsql_stmt_exec node which will also have the plan cached in it.
         */
        EXEC babel_4782_p @in
END
GO

In such recursive calls the plan in the outer call gets used by the inner call & at the end of execution we would free the plan even when the outer call is still active and using it. The cached plan is only available at statement level, hence the statement making the call should be same. As a fix we only free the plan in an exec stmt if we generated it in the first place. This is just a quick fix for now but a proper solution would be to adopt postgres method of caching the plan for exec calls.

Issues Resolved

[BABEL-4782]

Check List

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

coveralls commented 3 months ago

Pull Request Test Coverage Report for Build 9446296015

Details


Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_tds/src/backend/tds/tdscomm.c 3 73.33%
<!-- Total: 3 -->
Totals Coverage Status
Change from base Build 9416708039: -0.004%
Covered Lines: 42283
Relevant Lines: 57876

💛 - Coveralls
coveralls commented 3 months ago

Pull Request Test Coverage Report for Build 9452589260

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details


Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_common/src/datetime.c 52 92.57%
<!-- Total: 52 -->
Totals Coverage Status
Change from base Build 9416708039: 0.0%
Covered Lines: 42284
Relevant Lines: 57874

💛 - Coveralls