Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzMLe-0003ak-Bb for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Sep 2018 13:35:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fzMLb-0006Qp-TX for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Sep 2018 13:35:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzMLb-0006Qa-MT for pgsql-hackers@lists.postgresql.org; Mon, 10 Sep 2018 13:35:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzMLY-0007xO-D2 for pgsql-hackers@lists.postgresql.org; Mon, 10 Sep 2018 13:35:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8ADZ0b0008353; Mon, 10 Sep 2018 09:35:00 -0400 From: Tom Lane To: Amit Kapila cc: PostgreSQL Hackers , hlinnaka Subject: Re: SerializeParamList vs machines with strict alignment In-reply-to: References: <11629.1536550032@sss.pgh.pa.us> Comments: In-reply-to Amit Kapila message dated "Mon, 10 Sep 2018 12:22:34 +0530" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8351.1536586500.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 10 Sep 2018 09:35:00 -0400 Message-ID: <8352.1536586500@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Amit Kapila writes: > On Mon, Sep 10, 2018 at 8:58 AM Tom Lane wrote: >> In particular, SerializeParamList does this: >> = >> /* Write flags. */ >> memcpy(*start_address, &prm->pflags, sizeof(uint16)); >> *start_address +=3D sizeof(uint16); >> = >> immediately followed by this: >> = >> datumSerialize(prm->value, prm->isnull, typByVal, typLen, >> start_address); > datumSerialize does this: > memcpy(*start_address, &header, sizeof(int)); > *start_address +=3D sizeof(int); > before calling EOH_flatten_into, so it seems to me it should be 4-byte a= ligned. But that doesn't undo the fact that you're now on an odd halfword boundary. In the case I observed, EA_flatten_into was trying to store an int32 through a pointer whose hex value ended in E, which is explained by the "+=3D sizeof(uint16)". > Yeah, I think as suggested by you, start_address should be maxaligned. A localized fix would be for datumSerialize to temporarily palloc the space for EOH_flatten_into to write into, and then it could memcpy that to whatever random address it'd been passed. Seems kind of inefficient though, especially since you'd likely have to do the same thing on the receiving side. A larger issue is that even on machines where misalignment is permitted, memcpy'ing to/from misaligned addresses is rather inefficient. I think it'd be better to redesign the whole thing so that every field is maxaligned, and you can e.g. just store and retrieve integer fields as integers without a memcpy. The "wasted" padding space doesn't seem very significant given the short-lived nature of the serialized data. However, that's not exactly a trivial change. Probably the best way forward is to adopt the extra-palloc solution as a back-patchable fix, and then work on a more performant solution in HEAD only. regards, tom lane