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

Free buffer early when converting numerics #2456

Merged

Conversation

staticlibs
Copy link
Contributor

Description

TdsTypeNumericToDatum allocates a buffer for parsing incoming numeric data, but does not release this buffer. So it remains in MessageContext until the end of BCP import call.

Proposed patch frees this buffer before exiting TdsTypeNumericToDatum.

Backend memory usage when importing 1 million decimals (see details in linked issue) without the patch:

Figure_1

With patch applied:

Figure_2

Issues Resolved

#2455

Test Scenarios Covered

There are no functional changes, so no new tests.

Check List

  • Commits are signed per the DCO using --signoff

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.

Signed-off-by: Alex Kasko alex@staticlibs.net

Signed-off-by: Alex Kasko <alex@staticlibs.net>
@coveralls
Copy link
Collaborator

coveralls commented Apr 1, 2024

Pull Request Test Coverage Report for Build 8554170845

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

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 72.548%

Totals Coverage Status
Change from base Build 8479429136: 0.002%
Covered Lines: 41121
Relevant Lines: 56681

💛 - Coveralls

@@ -1189,6 +1192,7 @@ TdsTypeNumericToDatum(StringInfo buf, int scale)
decString++;

res = TdsSetVarFromStrWrapper(decString);
pfree(decStringOrig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I couldn't add comment above, but we should free memory allocated to zeros on line no. 1153
  2. Just to safer side and to avoid double free, use if (decStringOrig) then free

Copy link
Contributor

@KushaalShroff KushaalShroff Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no chance that decStringOrig is going to be NULL here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added memory free call to zeros buffer. Agree about the NULL checks before calling pfree, just in this case decStringOrig should not be NULL on any code path.

Signed-off-by: Alex Kasko <alex@staticlibs.net>
@Deepesh125
Copy link
Contributor

We should fix this in other dev branches also, BABEL_2_X_DEV and BABEL_3_X_DEV

@staticlibs
Copy link
Contributor Author

Thanks for the review! I've filed PRs for 3x and 2x.

@Anikait143 Anikait143 assigned Anikait143 and unassigned Anikait143 Apr 8, 2024
@Anikait143
Copy link
Contributor

Anikait143 commented Apr 8, 2024

Related Jira: BABEL-4882

@shardgupta shardgupta merged commit 3d2354e into babelfish-for-postgresql:BABEL_4_X_DEV Apr 8, 2024
40 checks passed
@staticlibs staticlibs deleted the bcp_leak_decimal branch April 8, 2024 12:23
staticlibs added a commit to wiltondb/babelfish_extensions that referenced this pull request Apr 14, 2024
…2456)

`TdsTypeNumericToDatum` [allocates a buffer](https://github.com/babelfish-for postgresql/babelfish_extensions/blob/6606b1118978477186869c3dddd2c6d85fcc6387/contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c#L1133) for parsing incoming numeric data, but does not release this buffer. So it remains in `MessageContext` until the end of BCP import call.

Proposed patch frees this buffer before exiting `TdsTypeNumericToDatum`.

### Issues Resolved

babelfish-for-postgresql#2455, BABEL-4882

Signed-off-by: Alex Kasko <alex@staticlibs.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants