Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kz2Ba-00079G-Ib for pgsql-general@arkaria.postgresql.org; Mon, 11 Jan 2021 18:45:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kz2BY-0005I0-Bi for pgsql-general@arkaria.postgresql.org; Mon, 11 Jan 2021 18:45:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kz2BX-0005E3-W5 for pgsql-general@lists.postgresql.org; Mon, 11 Jan 2021 18:45:00 +0000 Received: from mail-il1-x132.google.com ([2607:f8b0:4864:20::132]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kz2BS-00087n-Jq for pgsql-general@postgresql.org; Mon, 11 Jan 2021 18:44:58 +0000 Received: by mail-il1-x132.google.com with SMTP id q1so125808ilt.6 for ; Mon, 11 Jan 2021 10:44:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=geophile-com.20150623.gappssmtp.com; s=20150623; h=content-transfer-encoding:from:mime-version:subject:message-id:date :cc:to; bh=DCgorApQubKYJNQIZSu0/PMTjBmiJS6ehNCz3C+qVUQ=; b=xs7hxXPGk9APZLDpiGfMfCPSVxniLqum8diDRaFSRWYuvZDrRNN9Q/9kfyHjynrYPW rHczJzmYTAtGzkOWauIvHbwXfHyIE7nEghbpLUGgzncuopG8rvkNt5aCmS1mEeIIa5G9 ohzaWawganS+fZhRQQlI/btq8HjbqptI8IJIrcAEnKiWxaJePaYh51mV6S76m0a3D69/ jiBVBRYDQANBcoUiIazqkL+Oq87N+sG9TjOgsG3cKeXEWdF5VGKwlz/RWh/mbkncDGuq Olpa5fXRBWVvwdJDuhKTmORvuGkSzCRgqTkZQbjHTbXmrZsWzYAwgwFufGcq0wDpos0j 2fRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:content-transfer-encoding:from:mime-version :subject:message-id:date:cc:to; bh=DCgorApQubKYJNQIZSu0/PMTjBmiJS6ehNCz3C+qVUQ=; b=ArtffGYlnJ1TXszCn9jVaYeFijOFbr3OBqF/MyDt60FobTteVlfEGTpKU0+smeuqhf vs7pXFx1z0Cx7VOyyyvq6qTP0FUI/2+9VTfztaTDIUad79Fe/o8ehX59kOayc0ZQ6/cs H6GeMpuM3eWupnExTyfYzMw5sUiTAlLDuHqTtrUjnsMJWEuhrEpPQeS/E60fWw5e/K7c P8EWzPG2bQW/cHKIvy1VLfJ3KmAqXwmq0fXJhLXGh25OKomAkADdcD333QeAwfbdEugI EfIZsJ7ivo2ChcSV5DfO24KAlK/qKqStHd0aK1hF+XijCiizUFkX2cbvQ9y2xBjJeDiY nXqw== X-Gm-Message-State: AOAM53384o6U+ze99P3+l59mxzRGrmhg9Nl/FK6N8pFuF1asdkTmVhbL vFHFPGHaPiQbUfgCwnGHWk8V3A== X-Google-Smtp-Source: ABdhPJwFL13sx5AddJGfYNdLN5XvAG13cmFjgeysE3Z/zFCdoFEmhWuxJuPohfD5S+T6BETqzwuamw== X-Received: by 2002:a92:28d:: with SMTP id 135mr488731ilc.35.1610390693318; Mon, 11 Jan 2021 10:44:53 -0800 (PST) Received: from [192.168.0.31] (d-65-175-247-246.nh.cpe.atlanticbb.net. [65.175.247.246]) by smtp.gmail.com with ESMTPSA id z4sm175475ioh.32.2021.01.11.10.44.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 Jan 2021 10:44:52 -0800 (PST) Content-Type: multipart/alternative; boundary=Apple-Mail-F3C92D73-BD41-4292-BFD4-049BDE016D2F Content-Transfer-Encoding: 7bit From: Jack Orenstein Mime-Version: 1.0 (1.0) Subject: Re: Static memory, shared memory Message-Id: Date: Mon, 11 Jan 2021 13:44:51 -0500 Cc: pgsql-general@postgresql.org To: Tom Lane X-Mailer: iPhone Mail (18B92) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-F3C92D73-BD41-4292-BFD4-049BDE016D2F Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable =EF=BB=BF On Sat, Jan 9, 2021 at 12:18 PM Tom Lane wrote: > Jack Orenstein writes: > > 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. >=20 > > 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 th= is > > cannot work in general (since Postgres is multi-process), but I thought i= t > > would be adequate for early development. However, I am seeing this stati= c > > memory get corrupted even when there is only a single process executing t= he > > extension code (verified by examining getpid()). >=20 > Define what you mean by "corrupted". It seems highly unlikely that any > code but your own is touching this memory. Some fields have expected values, others do not. I think I have just figured out this problem, and it was indeed my own gun s= hooting my own foot. =20 >=20 > Really the big-picture question here is what are you hoping to accomplish > and why do you think this memory might need to be shared? The type I am implementing depends on looking up data in an array whose size= is approximately 64k. This array needs to be computed once and for all earl= y on, and is then consulted as the type is used. For now, I have hardwired t= he parameters that determine the array's contents, and the array is construc= ted during _PG_init. I will eventually remove this scaffolding, and compute t= he array contents when required, which should be a rare event. Jack Orenstein --Apple-Mail-F3C92D73-BD41-4292-BFD4-049BDE016D2F Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
=EF=BB=BF
=


On Sat, Jan 9, 2021 at 12:18 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Jack Orenstein <jao@geophile.com> writes:
> 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 t= his
> cannot work in general (since Postgres is multi-process), but I thought= it
> would be adequate for early development. However, I am seeing this stat= ic
> memory get corrupted even when there is only a single process executing= the
> extension code (verified by examining getpid()).

Define what you mean by "corrupted".  It seems highly unlikely that any=
code but your own is touching this memory.

Some fields have expected values, others do not.

I think I have just figured out this problem, and it was indeed my own gun s= hooting my own foot.
 

Really the big-picture question here is what are you hoping to accomplish and why do you think this memory might need to be shared?

The type I am implementing depends on looking up data in a= n array whose size is approximately 64k. This array needs to be computed onc= e and for all early on, and is then consulted as the type is used. For now, I= have hardwired the parameters that determine the array's contents, and the a= rray is constructed during _PG_init. I will eventually remove this scaffoldi= ng, and compute the array contents when required, which should be a rare eve= nt.

Jack Orenstein
= --Apple-Mail-F3C92D73-BD41-4292-BFD4-049BDE016D2F--