summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÁlvaro Herrera2025-05-11 13:22:12 +0000
committerÁlvaro Herrera2025-05-11 13:22:12 +0000
commitdc9a2d54fd2571c21c074103cc8ddd93e840b985 (patch)
tree65bf3a936f8469a0f3e7fb202ea59dc4284cc271
parent7b2ad4342603c9796bf28dbe84aca247b2bfa9f8 (diff)
relcache: Avoid memory leak on tables with no CHECK constraints
As complained about by Valgrind, in commit a379061a22a8 I failed to realize that I was causing rd_att->constr->check to become allocated when no CHECK constraints exist; previously it'd remain NULL. (This was my bug, not the mentioned commit author's). Fix by making the allocation conditional, and set ->check to NULL if unallocated. Reported-by: Yasir <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected]
-rw-r--r--src/backend/utils/cache/relcache.c11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 68ff67de549..559ba9cdb2c 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -4598,10 +4598,13 @@ CheckNNConstraintFetch(Relation relation)
HeapTuple htup;
int found = 0;
- /* Allocate array with room for as many entries as expected */
- check = (ConstrCheck *)
- MemoryContextAllocZero(CacheMemoryContext,
- ncheck * sizeof(ConstrCheck));
+ /* Allocate array with room for as many entries as expected, if needed */
+ if (ncheck > 0)
+ check = (ConstrCheck *)
+ MemoryContextAllocZero(CacheMemoryContext,
+ ncheck * sizeof(ConstrCheck));
+ else
+ check = NULL;
/* Search pg_constraint for relevant entries */
ScanKeyInit(&skey[0],