Allocate thread-local snapshot array statically
authorTomas Vondra <tomas@2ndquadrant.com>
Sat, 14 Oct 2017 10:38:06 +0000 (12:38 +0200)
committerTomas Vondra <tomas@2ndquadrant.com>
Sat, 14 Oct 2017 10:52:04 +0000 (12:52 +0200)
commit1078b079d5476e3447bd5268b317eacb4c455f5d
tree9efbd40a8be0b13e769e2fe54885828bcaed3cd3
parent1c7637f35f5f8b4566c4483f02041e7fc89a83cc
Allocate thread-local snapshot array statically

Since commit fb56418d66 the snapshots are computed in thread-local
storage, but we haven't been freeing the memory (on thread exit).
As the memory is allocated in the global (TopMostMemoryContext),
this presented a memory leak of 64kB on each GTM connection.

One way to fix this would be to track when the thread-local storage
is used in GTM_GetTransactionSnapshot(), and allocate the memory
in TopMemoryContext (which is per-thread and released on exit).

But there's a simpler way - allocate the thread-specific storage as
part of GTM_ThreadInfo, and just redirect sn_xip from the snapshot.
This way we don't have to worry about palloc/pfree at all, and we
mostly assume that every connection will need to compute at least
one snapshot anyway.

Reported by Rob Canavan <rxcanavan@gmail.com>, investigation and fix
by me. For more discussion see
<CAFTg0q6VC_11+c=Q=gsAcDsBrDjvuGKjzNwH4Lr8vERRDn4Ycw@mail.gmail.com>

Backpatch to Postgres-XL 9.5.
src/gtm/main/gtm_snap.c
src/gtm/main/gtm_thread.c
src/include/gtm/gtm.h