diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c index fbc05f0dc13..c9a9b7ec66c 100644 --- a/src/backend/storage/ipc/shmem.c +++ b/src/backend/storage/ipc/shmem.c @@ -925,7 +925,14 @@ ShmemResizeStruct(const char *name, Size new_size) new_end = (char *) TYPEALIGN(page_size, (char *) result->location + new_size); if (new_size < result->size) { - char *max_end = (char *) TYPEALIGN_DOWN(page_size, (char *) result->location + result->maximum_size); + /* + * Only pages currently RW should be freed; anything past result->size + * is PROT_NONE reserved tail. Also stop before the last page shared + * with the next structure. + */ + char *current_end = (char *) TYPEALIGN(page_size, (char *) result->location + result->size); + char *reserved_end = (char *) TYPEALIGN_DOWN(page_size, (char *) result->location + result->maximum_size); + char *max_end = Min(current_end, reserved_end); if (max_end > new_end) {