Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aqmj7-0002HL-4B for pgsql-bugs@arkaria.postgresql.org; Thu, 14 Apr 2016 19:14:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1aqmj6-0004KC-ND for pgsql-bugs@arkaria.postgresql.org; Thu, 14 Apr 2016 19:14:52 +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.84_2) (envelope-from ) id 1aqmj6-0004JI-Cj for pgsql-bugs@postgresql.org; Thu, 14 Apr 2016 19:14:52 +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.84_2) (envelope-from ) id 1aqmj3-0001Uv-7v for pgsql-bugs@postgresql.org; Thu, 14 Apr 2016 19:14:51 +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 u3EJEbId015041; Thu, 14 Apr 2016 15:14:37 -0400 From: Tom Lane To: Christoph Berg cc: Andres Freund , PostgreSQL Bugs Subject: Re: Bus error in pg_logical_slot_get_changes (9.4.7, sparc) In-reply-to: <20160414183602.GA22486@msg.df7cb.de> References: <20160413211152.GA21711@msg.credativ.de> <20160413175827.dmlbtdf7c3mgmnex@alap3.anarazel.de> <20160414092919.GA11804@msg.df7cb.de> <20160414112518.GE11804@msg.df7cb.de> <423.1460653903@sss.pgh.pa.us> <20160414183602.GA22486@msg.df7cb.de> Comments: In-reply-to Christoph Berg message dated "Thu, 14 Apr 2016 20:36:02 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <14995.1460661194.0@sss.pgh.pa.us> Date: Thu, 14 Apr 2016 15:14:37 -0400 Message-ID: <15040.1460661277@sss.pgh.pa.us> X-Pg-Spam-Score: -2.9 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14995.1460661194.1@sss.pgh.pa.us> Christoph Berg writes: > Re: Tom Lane 2016-04-14 <423.1460653903@sss.pgh.pa.us> >> Proposed patch attached, but I'm still wondering why the alignment-picky >> buildfarm members aren't all failing on this. It seems to strongly >> suggest that the regression tests' coverage is lacking here. > This patch works both on sparc and amd64. Thanks! > (Tested on 9.4.7 only.) I've been looking around and testing some more. I noticed that the only caller of ReorderBufferRestoreChange always passes a maxaligned buffer, so most of the changes I suggested are unnecessary. AFAICT, it's only the second fetch of t_len that is at any risk. Even there, at least in HEAD, I cannot construct a test case in which the first tuple's t_len is not a multiple of 4. It's hard to tell what is causing that, because surely heap_form_tuple guarantees no such thing. I have a suspicion that something in the impenetrable thicket that passes for prefix/suffix optimization in log_heap_update is forcing the WAL-logged tuple length to be rounded off to sizeof(int) (*not* MAXALIGN). It might be that 9.4 acts differently here, but the lack of crashes in our buildfarm suggests otherwise. The best theory I can come up with is that somehow, your Sparc compiler is deciding that "data" is 8-aligned at this point and generating a code sequence that depends on that, even though it's only being asked to fetch a uint32. If the machine were a 64-bit machine, the compiler could legitimately make such a deduction because of the presence of a pointer field in HeapTupleData; but I dunno how it gets to that conclusion if the architecture is 32-bit. Anyway, there are few enough architectures where it could make sense to use an 8-aligned instruction to fetch a 4-byte value that it's not so surprising we've not seen it crash elsewhere. So I now think my previous patch is overkill, and we should instead use something like the attached. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="alignment-fix-2.patch"; charset="us-ascii" Content-ID: <14995.1460661194.2@sss.pgh.pa.us> Content-Description: alignment-fix-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/= replication/logical/reorderbuffer.c index 52c6986..4520708 100644 *** a/src/backend/replication/logical/reorderbuffer.c --- b/src/backend/replication/logical/reorderbuffer.c *************** ReorderBufferRestoreChanges(ReorderBuffe *** 2444,2449 **** --- 2444,2453 ---- /* * Convert change from its on-disk format to in-memory format and queue i= t onto * the TXN's ->changes list. + * + * Note: although "data" is declared char*, at entry it points to a + * maxalign'd buffer, making it safe in most of this function to assume + * that the pointed-to data is suitably aligned for direct access. */ static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn, *************** ReorderBufferRestoreChange(ReorderBuffer *** 2471,2477 **** case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT: if (change->data.tp.oldtuple) { ! Size tuplelen =3D ((HeapTuple) data)->t_len; =20=20 change->data.tp.oldtuple =3D ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); --- 2475,2481 ---- case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT: if (change->data.tp.oldtuple) { ! uint32 tuplelen =3D ((HeapTuple) data)->t_len; =20=20 change->data.tp.oldtuple =3D ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); *************** ReorderBufferRestoreChange(ReorderBuffer *** 2492,2498 **** =20=20 if (change->data.tp.newtuple) { ! Size tuplelen =3D ((HeapTuple) data)->t_len; =20=20 change->data.tp.newtuple =3D ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); --- 2496,2506 ---- =20=20 if (change->data.tp.newtuple) { ! /* here, data might not be suitably aligned! */ ! uint32 tuplelen; !=20 ! memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len), ! sizeof(uint32)); =20=20 change->data.tp.newtuple =3D ReorderBufferGetTupleBuf(rb, tuplelen - SizeofHeapTupleHeader); ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--