Static memory, shared memory

From: Jack Orenstein <jao(at)geophile(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Static memory, shared memory
Date: 2021-01-09 15:50:24
Message-ID: CAGNxcatnDTS8gnuz5s++Qn8o3-No+BCMxBaZwOEija-t0O=4PQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am writing a Postgres extension, and thought that I had memory
corruption, (thanks for the --enable-cassert lead). I might, but It now
looks like I need to understand the use of shared memory and locking in
Postgres. So I have two questions.

1) I am now guessing that my original problem is caused by relying on
static memory in my extension (i.e., in the source declaring
PG_MODULE_MAGIC). This static memory is almost but not quite constant -- it
is initialized from _PG_init, and then never modified. I suspect that this
cannot work in general (since Postgres is multi-process), but I thought it
would be adequate for early development. However, I am seeing this static
memory get corrupted even when there is only a single process executing the
extension code (verified by examining getpid()). So the question is this:
Is the use of non-constant static memory ill-advised, even assuming there
is just one process relying on it? Or is it more likely that I still have
run-of-the-mill memory corruption. (--enable-cassert hasn't notified me of
any problems).

2) Assuming that I should be using shared memory instead of static, I am
reading https://www.postgresql.org/docs/12/xfunc-c.html#id-1.8.3.13.14, and
examining contrib/pg_prewarm. The xfunc-c documentation mentions
RequestNamedLWLockTranche to get an array of LWLocks. But the sample code
that follows calls GetNamedLWLockTranche. And the pg_prewarm code doesn't
rely on an array of locks, it initializes a single lock, (which I think
would be adequate for my needs). I understand the purpose of locks for
obtaining and manipulating shared memory but I am confused about the
correct way to proceed. I'm guessing it is safe to assume that pg_prewarm
works and is a good model to follow, and that the doc may be buggy. Can
someone clarify my confusion, and perhaps point me at a tutorial on correct
usage of the interfaces for LWLocks and shared memory, (I haven't been able
to find one).

Thank you.

Jack Orenstein

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Farber 2021-01-09 16:49:29 Re: Select a column and then apply JSONB_ARRAY_ELEMENTS to it
Previous Message Christophe Pettus 2021-01-09 15:09:26 Re: How to keep format of views source code as entered?