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

Do not allocate shared memory for wal_redo process #165

Merged
merged 2 commits into from
Jun 6, 2022

Conversation

knizhnik
Copy link

No description provided.

@kelvich
Copy link

kelvich commented May 24, 2022

  • Would be good to have a not null check
  • Not completely sure, but seems that PGSharedMemoryDetach() will try to try to shmdt() returned address with the current approach.

I did a somewhat similar thing while playing with WASM, but one level up https://gist.github.com/kelvich/d0afaf4192574a3fc196263d441e6c04

I think a more clear approach would be to implement PGSharedMemoryCreate/PGSharedMemoryIsInUse/PGSharedMemoryDetach in a separate file like local_shmem.c and use it instead. However, that would be a build-time option so not completely sure.

@knizhnik
Copy link
Author

shmdt is any case not enabled in secomp mode:

		/* Enable those for a proper shutdown.
		PG_SCMP_ALLOW(munmap),
		PG_SCMP_ALLOW(shmctl),
		PG_SCMP_ALLOW(shmdt),
		PG_SCMP_ALLOW(unlink), // shm_unlink
		*/

IMHO providing special implementation of PG shared memory (local_shmem.c) just for wal redo is overkill. Also it may be not so convenient to build two different versions of postgres binaries.

It is also possible to add pecial type for dynamic_shared_memory_type (right now sysv and mmap) and look at it. But it is also not so correct, because it referes only dynamic shared memory segments, not static.

From my point of view adding two lines of Neon specific code is better than other alternatives requiring more invasive changes of postgres core.

@@ -155,6 +155,8 @@ InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
}
}
#endif
if (am_wal_redo_postgres)
return valloc(size);
Copy link
Contributor

Choose a reason for hiding this comment

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

Had to look up what valloc is, never heard of that before :-).

Why not malloc?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment

Copy link
Author

Choose a reason for hiding this comment

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

valloc returns address aligned on page boundary. It may affect alignment of some Postgres data structures. Not sure that it is critical for wal redo. But I thought that it will be better to preserve alignement.

@knizhnik knizhnik merged commit 1614d62 into main Jun 6, 2022
@knizhnik knizhnik deleted the no_shmat_for_wal_redo branch June 6, 2022 15:41
@hlinnaka
Copy link
Contributor

hlinnaka commented Jun 7, 2022

I agree with Stas' earlier comment that this needs a NULL check. If the valloc() call fails and returns NULL, I think this will retry the valloc() call in an infinite loop.

I also think we should replace the valloc() with plain malloc(). There is no reason for the allocation to be page-aligned.

BTW, with the default settings, this is only used to replace the tiny shmem shim allocation. Most of the shared memory is still allocated with the mmap() call. I think that's OK, as that codepath also does some tricks to make huge pages effective, which probably makes sense for the WAL redo process, too.

@knizhnik
Copy link
Author

knizhnik commented Jun 7, 2022

I agree with Stas' earlier comment that this needs a NULL check. If the valloc() call fails and returns NULL, I think this will retry the valloc() call in an infinite loop.

I also think we should replace the valloc() with plain malloc(). There is no reason for the allocation to be page-aligned.

BTW, with the default settings, this is only used to replace the tiny shmem shim allocation. Most of the shared memory is still allocated with the mmap() call. I think that's OK, as that codepath also does some tricks to make huge pages effective, which probably makes sense for the WAL redo process, too.

#173

I do not think that we should eliminate mmaps, because even malloc is using it internally.

MMeent pushed a commit that referenced this pull request Jul 7, 2022
* Do not allocate shared memory for wal_redo process

* Add comment
MMeent pushed a commit that referenced this pull request Aug 18, 2022
* Do not allocate shared memory for wal_redo process

* Add comment
lubennikovaav pushed a commit that referenced this pull request Nov 21, 2022
* Do not allocate shared memory for wal_redo process

* Add comment
MMeent pushed a commit that referenced this pull request Feb 10, 2023
* Do not allocate shared memory for wal_redo process

* Add comment
MMeent pushed a commit that referenced this pull request Feb 10, 2023
* Do not allocate shared memory for wal_redo process

* Add comment
MMeent pushed a commit that referenced this pull request May 11, 2023
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request Aug 10, 2023
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request Nov 8, 2023
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request Nov 8, 2023
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request Feb 5, 2024
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request Feb 5, 2024
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request Feb 6, 2024
* Do not allocate shared memory for wal_redo process

* Add comment
tristan957 pushed a commit that referenced this pull request May 10, 2024
* Do not allocate shared memory for wal_redo process

* Add comment
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.

3 participants