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

Fix leaking of memory and memory contexts in Foreign Constraint Graphs #7236

Merged
merged 4 commits into from
Oct 9, 2023

Conversation

thanodnl
Copy link
Member

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign Constraint Graphs

Previously, every time we (re)created the Foreign Constraint Relationship Graph, we created a new Memory Context while loosing a reference to the previous context. This old context could still have left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily initialize the first time we create our foreign constraint relationship graph. On every subsequent creation, beside destroying our previous hashmap we also reset our memory context to remove any left over references.

@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

Merging #7236 (2c9588c) into main (b87fbcb) will decrease coverage by 0.01%.
Report is 2 commits behind head on main.
The diff coverage is 100.00%.

❗ Current head 2c9588c differs from pull request most recent head 7346f93. Consider uploading reports for the commit 7346f93 to get more accurate results

@@            Coverage Diff             @@
##             main    #7236      +/-   ##
==========================================
- Coverage   93.23%   93.23%   -0.01%     
==========================================
  Files         275      275              
  Lines       59484    59488       +4     
==========================================
+ Hits        55462    55464       +2     
- Misses       4022     4024       +2     

@onderkalaci
Copy link
Member

marking as relevant: https://github.com/citusdata/citus/pull/6234/files

Probably doesn't resolve the issue, but at least invalidation becomes less spread over the code

Comment on lines 357 to 359
fConstraintRelationshipGraph = (ForeignConstraintRelationshipGraph *) palloc(
sizeof(ForeignConstraintRelationshipGraph));
Copy link
Member

@onurctirtir onurctirtir Oct 5, 2023

Choose a reason for hiding this comment

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

Resetting ForeignConstraintRelationshipMemoryContext would invalidate fConstraintRelationshipGraph as well and then the next time IsForeignConstraintRelationshipGraphValid would be accessing a freed memory?

Copy link
Member Author

Choose a reason for hiding this comment

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

https://github.com/citusdata/citus/pull/7236/files#diff-e0be0392562ddabab1d64b436a7cd8225ca5678d64b80c6f01c2be615a3848a8R327 Should have set that to NULL already no? I could add an assert to verify and make that case clear I guess

Copy link
Member

Choose a reason for hiding this comment

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

Should have set that to NULL already no?

Right. And when looking into that function, I realized that hash_destroy() already deletes the memory context. I know it's more efficient to reset a memory context rather than deleting it but wondering how we were leaking the memory before then.

Copy link
Member Author

@thanodnl thanodnl Oct 6, 2023

Choose a reason for hiding this comment

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

When creating a hashmap with the HASH_CONTEXT flag, like we do, the hashmap has an internal context as a child of its current context:
https://github.com/postgres/postgres/blob/ffb69b23115a1ca1d81f7e273d50b75154ae7b0b/src/backend/utils/hash/dynahash.c#L380-L387

This context is then stored on the hashmap via
https://github.com/postgres/postgres/blob/ffb69b23115a1ca1d81f7e273d50b75154ae7b0b/src/backend/utils/hash/dynahash.c#L501-L505

Which is then destroyed when hash_destroy deletes its context. Hashmaps would be very hard to work with if they start destroying memory contexts that were being used when the map gets created ;)

The context we create is the one that is leaking.

@onurctirtir onurctirtir self-requested a review October 6, 2023 13:09
Copy link
Member

@onurctirtir onurctirtir left a comment

Choose a reason for hiding this comment

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

I feel like now we can replace the call made to ClearForeignConstraintRelationshipGraphContext() with fConstraintRelationshipGraph = NULL, and remove ClearForeignConstraintRelationshipGraphContext() function?

@thanodnl
Copy link
Member Author

thanodnl commented Oct 6, 2023

I feel like now we can replace the call made to ClearForeignConstraintRelationshipGraphContext() with fConstraintRelationshipGraph = NULL, and remove ClearForeignConstraintRelationshipGraphContext() function?

I think that makes sense, I will double check the behaviour of memcontext reset, but I assume it would delete all child contexts as well.

@thanodnl thanodnl force-pushed the fix/foreign-key-memory-context-leak branch from 7346f93 to dc7ddf8 Compare October 6, 2023 14:49
Previously, every time we (re)created the Foreign Constraint Relationship Graph, we created a new Memory Context while loosing a reference to the previous context. This old context could still have left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily initialize the first time we create our foreign constraint relationship graph. On every subsequent creation, beside destroying our previous hashmap we also reset our memory context to remove any left over references.
@thanodnl thanodnl force-pushed the fix/foreign-key-memory-context-leak branch from dc7ddf8 to d33ecd0 Compare October 9, 2023 10:54
@thanodnl thanodnl enabled auto-merge (squash) October 9, 2023 10:58
@thanodnl thanodnl merged commit 6d8725e into main Oct 9, 2023
103 checks passed
@thanodnl thanodnl deleted the fix/foreign-key-memory-context-leak branch October 9, 2023 11:05
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl added a commit that referenced this pull request Oct 9, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
francisjodi pushed a commit that referenced this pull request Nov 13, 2023
#7236)

DESCRIPTION: Fix leaking of memory and memory contexts in Foreign
Constraint Graphs

Previously, every time we (re)created the Foreign Constraint
Relationship Graph, we created a new Memory Context while loosing a
reference to the previous context. This old context could still have
left over memory in there causing a memory leak.

With this patch we statically have one memory context that we lazily
initialize the first time we create our foreign constraint relationship
graph. On every subsequent creation, beside destroying our previous
hashmap we also reset our memory context to remove any left over
references.
thanodnl referenced this pull request Nov 16, 2023
Co-authored-by: Onur Tirtir <onurcantirtir@gmail.com>
(cherry picked from commit 92228b2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants