Skip site navigation (1) Skip section navigation (2)

POSIX shared memory support

From: Chris Marcellino <cmarcellino(at)apple(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: POSIX shared memory support
Date: 2007-02-27 05:00:09
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-patches
Recapitulating and addressing some of the issues with my previous  
attempt at this feature:

PostgreSQL currently uses the System V shared memory APIs to access  
shared memory. On Mac OS X and other BSDs,
the default System V shared memory limits are often very low and  
require adjustment for acceptable performance. Par-
ticularly, when Postgres is included as part of larger end-user  
friendly software products, these kernel settings are often
difficult to change programatically.

The System V shared memory facilities provide a method to determine  
who is attached to a shared memory segment.
This is used to prevent backends that were orphaned by crashed or  
killed database processes from corrupting the data-
base as it is restarted. The same effect can be achieved with using  
the POSIX APIs, but since the POSIX library does not
have a way to check who is attached to a segment, atomic segment  
creation must be used to ensure exclusive access to
the database.

In order for this to work, the key name used to open and create the  
shared memory segment must be unique for each
data directory. This is done by using a strong hash of the canonical  
form of the data directory’s pathname. This also re-
moves any risk of other applications, or other databases’ memory  
segments colliding with the current shared memory
segment, which conveniently simplifies the logic.

The algorithm changes are described by the before and after flowchart  
PDF included in the tarball below.

There is also a Windows version of this patch included, which can  
replace the current SysV-to-Win32 shared memory
layer as it currently does not check for orphaned backends in the  
database. If this is used,
src/backend/port/win32/shmem.c and its makefile reference can be  

Included is a patch for the file to opt-in all of the  
supported platforms to use this POSIX shared memory
layer except those that are explicitly excluded (of course, this  
could be done in reverse if desired). Notably, NetBSD and
OpenBSD do not support the POSIX shared memory calls and their  
template patches are included. Other platforms
should be tested and excluded as needed. In addition, the  
documentation section 16.4.1 ‘Shared Memory and Sema-
phores’ could be largely pruned and updated to reflect use of the  
POSIX calls.

The patches are available here (the postings were being silently  
dropped when I attached this large of a file):

Please let me know if there is a better way to post this.

Thanks for your feedback,
Chris Marcellino


pgsql-patches by date

Next:From: Jim C. NasbyDate: 2007-02-27 05:11:44
Subject: Re: Dead Space Map version 2
Previous:From: Tom LaneDate: 2007-02-27 04:07:10
Subject: Re: [HACKERS] COMMIT NOWAIT Performance Option (patch)

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group