pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Segfault on exclusion constraint violation
6+ messages / 4 participants
[nested] [flat]

* Segfault on exclusion constraint violation
@ 2015-02-02 13:50  Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Heikki Linnakangas @ 2015-02-02 13:50 UTC (permalink / raw)
  To: pgsql-bugs

9.4 and master segfaults, if an insertion would need to wait for another 
transaction to finish because of an exclusion constraint. To reproduce:

Run these in session A:

create extension btree_gist;
create table foo (i int4, constraint i_exclude exclude using gist (i 
with =));
begin; insert into foo values (1);

leave the transaction open, and session B:

insert into foo values (1);


LOG:  server process (PID 3690) was terminated by signal 11: 
Segmentation fault
DETAIL:  Failed process was running: insert into foo values (1);
LOG:  terminating any other active server processes

gdb backtrace:

#0  0x000000000078520d in XactLockTableWait (xid=705, rel=0x7f2f6e835728,
     ctid=0x7f7f7f7f7f7f7f8b, oper=XLTW_RecheckExclusionConstr) at 
lmgr.c:515
#1  0x000000000064bd86 in check_exclusion_constraint (heap=0x7f2f6e835728,
     index=0x7f2f6e837620, indexInfo=0x22187c0, tupleid=0x2219514,
     values=0x7fffae880a10, isnull=0x7fffae8809f0 "", estate=0x2218228,
     newIndex=0 '\000', errorOK=0 '\000') at execUtils.c:1310
#2  0x000000000064b9a9 in ExecInsertIndexTuples (slot=0x2218500,
     tupleid=0x2219514, estate=0x2218228) at execUtils.c:1126
#3  0x000000000065f8c4 in ExecInsert (slot=0x2218500, planSlot=0x2218500,
     estate=0x2218228, canSetTag=1 '\001') at nodeModifyTable.c:274


This only happens with assertions enabled. The culprit is commit 
f88d4cfc9d417dac2ee41a8f5e593898e56fd2bd, which added the 'ctid' 
argument to XactLockTableWait. check_exclusion_constraint calls 
index_endscan() just before XactLockTableWait, but that free's the 
memory the ctid points to.

The fix for this particular instance is trivial: copy the ctid to a 
local variable before calling index_endscan. However, looking at the 
other XactLockTableWait() and MultiXactIdWait() calls, there are more 
questionable pointers being passed. Most point to heap tuples on disk 
pages, after releasing the lock on the page, although not the pin. The 
one in EvalPlanQualFetch releases the pin too.

I'll write up a patch to change those call sites to use local variables. 
Hopefully it's trivial enough to still include in 9.4.1, although time 
is really running out..

- Heikki


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Segfault on exclusion constraint violation
@ 2015-02-02 14:35  Heikki Linnakangas <hlinnakangas@vmware.com>
  parent: Heikki Linnakangas <hlinnakangas@vmware.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Heikki Linnakangas @ 2015-02-02 14:35 UTC (permalink / raw)
  To: pgsql-bugs

On 02/02/2015 03:50 PM, Heikki Linnakangas wrote:
> This only happens with assertions enabled. The culprit is commit
> f88d4cfc9d417dac2ee41a8f5e593898e56fd2bd, which added the 'ctid'
> argument to XactLockTableWait. check_exclusion_constraint calls
> index_endscan() just before XactLockTableWait, but that free's the
> memory the ctid points to.
>
> The fix for this particular instance is trivial: copy the ctid to a
> local variable before calling index_endscan. However, looking at the
> other XactLockTableWait() and MultiXactIdWait() calls, there are more
> questionable pointers being passed. Most point to heap tuples on disk
> pages, after releasing the lock on the page, although not the pin. The
> one in EvalPlanQualFetch releases the pin too.
>
> I'll write up a patch to change those call sites to use local variables.
> Hopefully it's trivial enough to still include in 9.4.1, although time
> is really running out..

I'll commit the attached fix shortly, so please shout quickly if you see 
a problem with this.

Aside from the potential for segfaults with assertions, I think the 
calls passed incorrect ctid anyway. For example:

> --- a/src/backend/executor/execUtils.c
> +++ b/src/backend/executor/execUtils.c
> @@ -1307,7 +1307,7 @@ retry:
>                 if (TransactionIdIsValid(xwait))
>                 {
>                         index_endscan(index_scan);
> -                       XactLockTableWait(xwait, heap, &tup->t_data->t_ctid,
> +                       XactLockTableWait(xwait, heap, &tup->t_self,
>                                                           XLTW_RecheckExclusionConstr);
>                         goto retry;
>                 }

We don't really want to pass the heap tuple's ctid field. If the tuple 
was updated (again) in the same transaction, the one that's still 
in-progress, that points to the *next* tuple in the chain, but the error 
message says "while checking exclusion constraint on tuple (%u,%u) in 
relation \"%s\"". We should be passing the TID of the tuple itself, not 
the ctid value in the tuple's header. The attached patch fixes that too.

- Heikki

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

0001-Fix-reference-after-free-when-waiting-for-another-xa.patch (text/x-diff, 5.9K) download From 2b53793ca2c32ca8b3d26dc8f0ee72c3a623ed69 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas <heikki.linnakangas@iki.fi> Date: Mon, 2 Feb 2015 16:24:58 +0200 Subject: [PATCH 1/1] Fix reference-after-free when waiting for another xact due to constraint. If an insertion or update had to wait for another transaction to finish, because there was another insertion with conflicting key in progress, we would pass a just-free'd item pointer to XactLockTableWait(). All calls to XactLockTableWait() and MultiXactIdWait() had similar issues. Some passed a pointer to an buffer in the buffer cache, after already releasing the lock. The call in EvalPlanQualFetch had already released the pin too. All but the call in execUtils.c would merely lead to reporting a bogus ctid, however (or an assertion failure, if enabled). All the callers that passed HeapTuple->t_data->t_ctid were slightly bogus anyway: if the tuple was updated (again) in the same transaction, its ctid field would point to the next tuple in the chain, not the tuple itself. Backpatch to 9.4, where the 'ctid' argument to XactLockTableWait was added (in commit f88d4cfc) --- src/backend/access/heap/heapam.c | 16 ++++++++-------- src/backend/catalog/index.c | 4 ++-- src/backend/executor/execMain.c | 2 +- src/backend/executor/execUtils.c | 4 +++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 21e9d06..46060bc 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -2717,7 +2717,7 @@ l1: { /* wait for multixact */ MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate, infomask, - relation, &tp.t_data->t_ctid, XLTW_Delete, + relation, &(tp.t_self), XLTW_Delete, NULL); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -2744,7 +2744,7 @@ l1: else { /* wait for regular transaction to end */ - XactLockTableWait(xwait, relation, &tp.t_data->t_ctid, XLTW_Delete); + XactLockTableWait(xwait, relation, &(tp.t_self), XLTW_Delete); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); /* @@ -3261,7 +3261,7 @@ l2: /* wait for multixact */ MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask, - relation, &oldtup.t_data->t_ctid, XLTW_Update, + relation, &oldtup.t_self, XLTW_Update, &remain); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -3341,7 +3341,7 @@ l2: */ heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode, LockWaitBlock, &have_tuple_lock); - XactLockTableWait(xwait, relation, &oldtup.t_data->t_ctid, + XactLockTableWait(xwait, relation, &oldtup.t_self, XLTW_Update); LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE); @@ -4365,7 +4365,7 @@ l3: { case LockWaitBlock: MultiXactIdWait((MultiXactId) xwait, status, infomask, - relation, &tuple->t_data->t_ctid, XLTW_Lock, NULL); + relation, &tuple->t_self, XLTW_Lock, NULL); break; case LockWaitSkip: if (!ConditionalMultiXactIdWait((MultiXactId) xwait, @@ -4437,7 +4437,7 @@ l3: switch (wait_policy) { case LockWaitBlock: - XactLockTableWait(xwait, relation, &tuple->t_data->t_ctid, + XactLockTableWait(xwait, relation, &tuple->t_self, XLTW_Lock); break; case LockWaitSkip: @@ -5181,7 +5181,7 @@ l4: { LockBuffer(buf, BUFFER_LOCK_UNLOCK); XactLockTableWait(members[i].xid, rel, - &mytup.t_data->t_ctid, + &mytup.t_self, XLTW_LockUpdated); pfree(members); goto l4; @@ -5242,7 +5242,7 @@ l4: if (needwait) { LockBuffer(buf, BUFFER_LOCK_UNLOCK); - XactLockTableWait(rawxmax, rel, &mytup.t_data->t_ctid, + XactLockTableWait(rawxmax, rel, &mytup.t_self, XLTW_LockUpdated); goto l4; } diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 9bb9deb..f85ed93 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2328,7 +2328,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, */ LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); XactLockTableWait(xwait, heapRelation, - &heapTuple->t_data->t_ctid, + &heapTuple->t_self, XLTW_InsertIndexUnique); CHECK_FOR_INTERRUPTS(); goto recheck; @@ -2377,7 +2377,7 @@ IndexBuildHeapRangeScan(Relation heapRelation, */ LockBuffer(scan->rs_cbuf, BUFFER_LOCK_UNLOCK); XactLockTableWait(xwait, heapRelation, - &heapTuple->t_data->t_ctid, + &heapTuple->t_self, XLTW_InsertIndexUnique); CHECK_FOR_INTERRUPTS(); goto recheck; diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 20b3188..33b172b 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -2118,7 +2118,7 @@ EvalPlanQualFetch(EState *estate, Relation relation, int lockmode, { case LockWaitBlock: XactLockTableWait(SnapshotDirty.xmax, - relation, &tuple.t_data->t_ctid, + relation, &tuple.t_self, XLTW_FetchUpdated); break; case LockWaitSkip: diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c index 4b921fa..022041b 100644 --- a/src/backend/executor/execUtils.c +++ b/src/backend/executor/execUtils.c @@ -1245,6 +1245,7 @@ retry: ForwardScanDirection)) != NULL) { TransactionId xwait; + ItemPointerData ctid_wait; Datum existing_values[INDEX_MAX_KEYS]; bool existing_isnull[INDEX_MAX_KEYS]; char *error_new; @@ -1306,8 +1307,9 @@ retry: if (TransactionIdIsValid(xwait)) { + ctid_wait = tup->t_data->t_ctid; index_endscan(index_scan); - XactLockTableWait(xwait, heap, &tup->t_data->t_ctid, + XactLockTableWait(xwait, heap, &ctid_wait, XLTW_RecheckExclusionConstr); goto retry; } -- 2.1.4
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Segfault on exclusion constraint violation
@ 2015-02-02 14:38  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Heikki Linnakangas <hlinnakangas@vmware.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Tom Lane @ 2015-02-02 14:38 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; +Cc: pgsql-bugs

Heikki Linnakangas <hlinnakangas@vmware.com> writes:
> 9.4 and master segfaults, if an insertion would need to wait for another 
> transaction to finish because of an exclusion constraint. To reproduce:
> ...
> This only happens with assertions enabled. The culprit is commit 
> f88d4cfc9d417dac2ee41a8f5e593898e56fd2bd, which added the 'ctid' 
> argument to XactLockTableWait. check_exclusion_constraint calls 
> index_endscan() just before XactLockTableWait, but that free's the 
> memory the ctid points to.

> I'll write up a patch to change those call sites to use local variables. 
> Hopefully it's trivial enough to still include in 9.4.1, although time 
> is really running out..

If the only known bad consequence requires assertions enabled, I think
it would be more prudent to *not* try to fix this in haste.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Segfault on exclusion constraint violation
@ 2015-02-02 15:01  Heikki Linnakangas <hlinnakangas@vmware.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2015-02-02 15:01 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

On 02/02/2015 04:38 PM, Tom Lane wrote:
> Heikki Linnakangas <hlinnakangas@vmware.com> writes:
>> 9.4 and master segfaults, if an insertion would need to wait for another
>> transaction to finish because of an exclusion constraint. To reproduce:
>> ...
>> This only happens with assertions enabled. The culprit is commit
>> f88d4cfc9d417dac2ee41a8f5e593898e56fd2bd, which added the 'ctid'
>> argument to XactLockTableWait. check_exclusion_constraint calls
>> index_endscan() just before XactLockTableWait, but that free's the
>> memory the ctid points to.
>
>> I'll write up a patch to change those call sites to use local variables.
>> Hopefully it's trivial enough to still include in 9.4.1, although time
>> is really running out..
>
> If the only known bad consequence requires assertions enabled, I think
> it would be more prudent to *not* try to fix this in haste.

Ok, I'll wait until after the release.

- Heikki



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Segfault on exclusion constraint violation
@ 2015-03-17 15:15  Dennis Pozzi <dpozzi@adobe.com>
  parent: Heikki Linnakangas <hlinnakangas@vmware.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Dennis Pozzi @ 2015-03-17 15:15 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnakangas@vmware.com>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs; David Gould (daveg@sonic.net) <daveg@sonic.net>; Saravanakumar T R <satr@adobe.com>

We are seeing a similar segfault scenario in 9.4.1 without assertions enabled. We upgraded 4 of our production instances from 9.3.5 to 9.4.1 over the weekend.
We had staged on 9.4.0, and then 9.4.1 for over a month without seeing this error, but the staging work load is much lower than production.

postgres=# show debug_assertions;
 debug_assertions
------------------
 off
(1 row)

postgres=# set debug_assertions = 'on' ;
ERROR:  assertion checking is not supported by this build
postgres=#

We believe the error occurred when an insert query was running  inside a transaction, and another query attempted to insert into the same table.

Postgres Log says :
2015-03-16 02:43:25.762 PDT,,,5481,,5504121c.1569,3,,2015-03-14 03:49:00 PDT,,0,LOG,00000,"server process (PID 9471) was terminated by signal 11: Segmentation fault","Failed process was running: INSERT into ad_instances (adinstid, cid, kid, adrefid, termid, sync_match_type,
                                               status_code, start_date, sync_keyword)
                     SELECT nextval('public.ad_instances_adinstid_seq'), cid, kw.kid, adg.adrefid,
                            termid, ai_mtid, 'u', now(), sync_keyword
                     FROM   tmp_keywords_1100218810
                     JOIN   keywords kw using (keyword)
                     JOIN   adgroups adg using (adref,cid)
                     WHERE  keyword_type = 'n'
                        AND cid = 1100218810
                     GROUP BY cid, kw.kid, adg.adrefid, termid, ai_mtid, sync_keyword, tmp_keywords_1100218810.status_code",,,,,,,,""

Application log says:
2015-03-16 02:43:25.763 PDT,"release","c10036",4349,"op02.lon5.efrontier.com:39404",5506a408.10fd,27,"idle in transaction",2015-03-16 02:36:08 PDT,44/292306,3267900389,WARNING,57P02,"terminating connection because of crash of another server process","The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.","In a moment you should be able to reconnect to the database and repeat your command.",,,,,,,"ad_status"

/var/log/messages says:
Mar 16 02:40:35 user27 kernel: : postgres[9471]: segfault at 30 ip 000000000066148b sp 00007fffa9c5f5f0 error 4 in postgres[400000+54d000]

We have not been able to reproduce the error, but we are testing scenarios in our staging environment now.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Segfault on exclusion constraint violation
@ 2015-03-18 05:42  Michael Paquier <michael.paquier@gmail.com>
  parent: Dennis Pozzi <dpozzi@adobe.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Michael Paquier @ 2015-03-18 05:42 UTC (permalink / raw)
  To: Dennis Pozzi <dpozzi@adobe.com>; +Cc: Heikki Linnakangas <hlinnakangas@vmware.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs; David Gould (daveg@sonic.net) <daveg@sonic.net>; Saravanakumar T R <satr@adobe.com>

On Wed, Mar 18, 2015 at 12:15 AM, Dennis Pozzi <dpozzi@adobe.com> wrote:
> We have not been able to reproduce the error, but we are testing scenarios in our staging environment now.

If this is the same problem, this will be fixed in the next minor
release as the fix has been committed here after 9.4.1 was released:
commit: 57fe246890ad51e166fb6a8da937e41c35d7a279
author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
date: Wed, 4 Feb 2015 16:00:34 +0200
Fix reference-after-free when waiting for another xact due to constraint.

Now, if you are able to produce a self-contained test case that fails
even on HEAD, well that's another story...
Regards,
-- 
Michael


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2015-03-18 05:42 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-02-02 13:50 Segfault on exclusion constraint violation Heikki Linnakangas <hlinnakangas@vmware.com>
2015-02-02 14:35 ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-02-02 14:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-02-02 15:01   ` Heikki Linnakangas <hlinnakangas@vmware.com>
2015-03-17 15:15     ` Dennis Pozzi <dpozzi@adobe.com>
2015-03-18 05:42       ` Michael Paquier <michael.paquier@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox