Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UmPxc-00071z-46 for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2013 14:54:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UmPxb-000175-H6 for pgsql-general@arkaria.postgresql.org; Tue, 11 Jun 2013 14:54:11 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UmPxY-00015J-Ue for pgsql-general@postgresql.org; Tue, 11 Jun 2013 14:54:09 +0000 Received: from sd.tpf.co.jp ([210.161.239.34]) by makus.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UmPxT-00022f-WF for pgsql-general@postgresql.org; Tue, 11 Jun 2013 14:54:08 +0000 Received: (qmail 6779 invoked by uid 512); 11 Jun 2013 14:54:00 -0000 X-Authentication: inoue was authenticated by sd.tpf.co.jp at 11 Jun 2013 14:54:00 -0000 Received: from unknown (HELO ?192.168.1.3?) (110.5.25.94) by sd.tpf.co.jp with SMTP; 11 Jun 2013 14:54:00 -0000 Message-ID: <51B73A00.1030206@tpf.co.jp> Date: Tue, 11 Jun 2013 23:53:52 +0900 From: Hiroshi Inoue User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: Joshua Berry , Andres Freund CC: Tom Lane , PostgreSQL - General Subject: Re: Segmentation fault with core dump References: <20130410214929.GA7547@awork2.anarazel.de> <20130410222233.GB7547@awork2.anarazel.de> <12385.1365633318@sss.pgh.pa.us> <13081.1365635172@sss.pgh.pa.us> <20130412111231.GC5766@alap2.anarazel.de> In-Reply-To: Content-Type: multipart/mixed; boundary="------------000205000107010208030607" X-Pg-Spam-Score: -2.1 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org This is a multi-part message in MIME format. --------------000205000107010208030607 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi, (2013/05/09 1:39), Joshua Berry wrote: > | I'm using PG 9.1.9 with a client application using various versions of > the > | pgsqlODBC driver on Windows. Cursors are used heavily, as well as some > pretty > | heavy trigger queries on db writes which update several materialized > views. > | > | The server has 48GB RAM installed, PG is configured for 12GB shared > buffers, > | 8MB max_stack_depth, 32MB temp_buffers, and 2MB work_mem. Most of the > other > | settings are defaults. > | > | The server will seg fault from every few days to up to two weeks. Each > time > | one of the postgres server processes seg faults, the server gets > terminated by > | signal 11, restarts in recovery for up to 30 seconds, after which time it > | accepts connections as if nothing ever happened. Unfortunately all the > open > | cursors and connections are lost, so the client apps are left in a bad > state. > | > | Seg faults have also occurred with PG 8.4. ... I migrated the database > to a > | server running PG9.1 with the hopes that the problem would disappear, > but it > | has not. So now I'm starting to debug. > | > | # uname -a > | Linux [hostname] 2.6.32-358.2.1.el6.x86_64 #1 SMP Tue Mar 12 14:18:09 > CDT 2013 > | x86_64 x86_64 x86_64 GNU/Linux > | # cat /etc/redhat-release > | Scientific Linux release 6.3 (Carbon) > | > | # psql -U jberry > | psql (9.1.9) > | Type "help" for help. > | > | jberry=# select version(); > | version > | > ------------------------------------------------------------------------------- > | PostgreSQL 9.1.9 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) > 4.4.7 > | 20120313 (Red Hat 4.4.7-3), 64-bit > | (1 row) > > I've had another postmaster segfault on my production server. It appears > to be the same failure as the last one nearly a month ago, but I wanted > to post the gdb bt details in case it helps shed light on the issue. > Please let me know if anyone would like to drill into the dumped core > with greater detail. Both the OS and PG versions remain unchanged. > > Kind Regards, > -Joshua > > > On Fri, Apr 12, 2013 at 6:12 AM, Andres Freund > wrote: > > On 2013-04-10 19:06:12 -0400, Tom Lane wrote: > > I wrote: > > > (Wanders away wondering just how much the regression tests exercise > > > holdable cursors.) > > > > And the answer is they're not testing this code path at all, > because if > > you do > > DECLARE c CURSOR WITH HOLD FOR ... > > FETCH ALL FROM c; > > then the second query executes with a portal (and resource owner) > > created to execute the FETCH command, not directly on the held > portal. > > > > After a little bit of thought I'm not sure it's even possible to > > reproduce this problem with libpq, because it doesn't expose any > way to > > issue a bare protocol Execute command against a pre-existing portal. > > (I had thought psqlOBC went through libpq, but maybe it's playing > some > > games here.) > > > > Anyway, I'm thinking the appropriate fix might be like this > > > > - CurrentResourceOwner = portal->resowner; > > + if (portal->resowner) > > + CurrentResourceOwner = portal->resowner; > > > > in several places in pquery.c; that is, keep using > > TopTransactionResourceOwner if the portal doesn't have its own. > > > > A more general but probably much more invasive solution would be > to fake > > up an intermediate portal when pulling data from a held portal, to > > more closely approximate the explicit-FETCH case. > > We could also allocate a new resowner for the duration of that > transaction. That would get reassigned to the transactions resowner in > PreCommit_Portals (after a slight change there). > That actually seems simple enough? I made some changes to multi thread handling of psqlodbc driver. It's also better to fix the crash at backend side. I made 2 patches. The 1st one temporarily changes CurrentResourceOwner to CurTransactionResourceOwner during catalog cache handling. The 2nd one allocates a new resource owner for held portals. Both fix the crash in my test case. regards, Hiroshi Inoue --------------000205000107010208030607 Content-Type: text/x-patch; name="holdable_cursor_printtup.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="holdable_cursor_printtup.patch" diff --git a/src/backend/access/common/printtup.c b/src/backend/access/common/printtup.c index e87e675..258a4c2 100644 --- a/src/backend/access/common/printtup.c +++ b/src/backend/access/common/printtup.c @@ -238,6 +238,7 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs) { int16 *formats = myState->portal->formats; int i; + bool resownerIsNull = false; /* get rid of any old data */ if (myState->myinfo) @@ -252,6 +253,15 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs) myState->myinfo = (PrinttupAttrInfo *) palloc0(numAttrs * sizeof(PrinttupAttrInfo)); + if (NULL == CurrentResourceOwner) + { + /* + * This occurs when processing execute messages + * for holdable cursors after commit. + */ + resownerIsNull = true; + CurrentResourceOwner = CurTransactionResourceOwner; + } for (i = 0; i < numAttrs; i++) { PrinttupAttrInfo *thisState = myState->myinfo + i; @@ -273,10 +283,16 @@ printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs) fmgr_info(thisState->typsend, &thisState->finfo); } else + { + if (resownerIsNull) + CurrentResourceOwner = NULL; ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("unsupported format code: %d", format))); + } } + if (resownerIsNull) + CurrentResourceOwner = NULL; } /* ---------------- --------------000205000107010208030607 Content-Type: text/x-patch; name="holdable_cursor_resowner.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="holdable_cursor_resowner.patch" diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c index f8989f7..ccc1c45 100644 --- a/src/backend/tcop/pquery.c +++ b/src/backend/tcop/pquery.c @@ -759,6 +759,11 @@ PortalRun(Portal portal, long count, bool isTopLevel, saveResourceOwner = CurrentResourceOwner; savePortalContext = PortalContext; saveMemoryContext = CurrentMemoryContext; + /* + * Allocate a new ResourceOwner for the portal if not set. + * It can occur for held portals(committed holdable cursors). + */ + PortalAllocResourceOwner(portal); PG_TRY(); { ActivePortal = portal; diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c index 54bf16e..5013300 100644 --- a/src/backend/utils/mmgr/portalmem.c +++ b/src/backend/utils/mmgr/portalmem.c @@ -183,6 +183,37 @@ PortalListGetPrimaryStmt(List *stmts) return NULL; } +static void DelinkResownerFromPortal(ResourceOwner owner, void *arg) +{ + Portal portal = (Portal) arg; + + if (!portal) return; + if (portal->resowner == NULL) + elog(DEBUG1, "resowner is already delinked from Portal(%p)", portal); + else if (owner != portal->resowner) + elog(DEBUG1, "the target ResourceOwner is different from the portal's resowner", portal); + else + { + elog(DEBUG1, "delinking resowner(%p) from Portal(%p)", portal->resowner, portal); + portal->resowner = NULL; + } + UnregisterResownerOnDeleteProc(owner, DelinkResownerFromPortal, arg); +} + +/* + * Allocate a new ResourceOwner for the portal if not set. + * Also register an on delete procedure for the ResourceOwner. + */ +void PortalAllocResourceOwner(Portal portal) +{ + if (portal->resowner) + return; + /* create a ResourceOwner for the portal */ + portal->resowner = ResourceOwnerCreate(CurTransactionResourceOwner, "Portal"); + /* register an on delete procedure for the ResourceOwner. */ + RegisterResownerOnDeleteProc(portal->resowner, DelinkResownerFromPortal, portal); +} + /* * CreatePortal * Returns a new portal given a name. @@ -224,9 +255,7 @@ CreatePortal(const char *name, bool allowDup, bool dupSilent) ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE); - /* create a resource owner for the portal */ - portal->resowner = ResourceOwnerCreate(CurTransactionResourceOwner, - "Portal"); + PortalAllocResourceOwner(portal); /* initialize portal fields that don't start off zero */ portal->status = PORTAL_NEW; @@ -506,6 +535,8 @@ PortalDrop(Portal portal, bool isTopCommit) /* drop cached plan reference, if any */ PortalReleaseCachedPlan(portal); + /* unregister the on delete procedure because resowner is set to NULL later in this routine */ + UnregisterResownerOnDeleteProc(portal->resowner, NULL, portal); /* * Release any resources still attached to the portal. There are several * cases being covered here: diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c index e7ec393..63930dd 100644 --- a/src/backend/utils/resowner/resowner.c +++ b/src/backend/utils/resowner/resowner.c @@ -98,6 +98,8 @@ typedef struct ResourceOwnerData int nfiles; /* number of owned temporary files */ File *files; /* dynamically allocated array */ int maxfiles; /* currently allocated array size */ + ResourceOwnerDeleteProc on_delete; + void *on_delete_arg; } ResourceOwnerData; @@ -439,6 +441,10 @@ ResourceOwnerDelete(ResourceOwner owner) if (owner->files) pfree(owner->files); + /* call on delete procedure if exists */ + if (owner->on_delete != NULL) + (*(owner->on_delete)) (owner, owner->on_delete_arg); + pfree(owner); } @@ -537,6 +543,33 @@ UnregisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg) } } +/* + * Register or deregister an on delete procedure + * + */ +void +RegisterResownerOnDeleteProc(ResourceOwner owner, ResourceOwnerDeleteProc proc, void *arg) +{ + if (!owner) + return; + owner->on_delete = proc; + owner->on_delete_arg = arg; +} + +void +UnregisterResownerOnDeleteProc(ResourceOwner owner, ResourceOwnerDeleteProc proc, void *arg) +{ + if (!owner) + return; + if (arg != owner->on_delete_arg) + return; + if (NULL != proc && + proc != owner->on_delete) + return; + owner->on_delete = NULL; + owner->on_delete_arg = NULL; +} + /* * Make sure there is room for at least one more entry in a ResourceOwner's diff --git a/src/include/utils/portal.h b/src/include/utils/portal.h index 4ae7e46..41492c6 100644 --- a/src/include/utils/portal.h +++ b/src/include/utils/portal.h @@ -205,6 +205,7 @@ extern void AtSubAbort_Portals(SubTransactionId mySubid, extern void AtSubCleanup_Portals(SubTransactionId mySubid); extern Portal CreatePortal(const char *name, bool allowDup, bool dupSilent); extern Portal CreateNewPortal(void); +extern void PortalAllocResourceOwner(Portal portal); extern void PinPortal(Portal portal); extern void UnpinPortal(Portal portal); extern void MarkPortalDone(Portal portal); diff --git a/src/include/utils/resowner.h b/src/include/utils/resowner.h index da76649..9a06d88 100644 --- a/src/include/utils/resowner.h +++ b/src/include/utils/resowner.h @@ -58,6 +58,11 @@ typedef void (*ResourceReleaseCallback) (ResourceReleasePhase phase, bool isTopLevel, void *arg); +/* + * ResourceOwner delete procedure + */ +typedef void (*ResourceOwnerDeleteProc) (ResourceOwner owner, void *arg); + /* * Functions in resowner.c @@ -78,5 +83,7 @@ extern void RegisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg); extern void UnregisterResourceReleaseCallback(ResourceReleaseCallback callback, void *arg); +extern void RegisterResownerOnDeleteProc(ResourceOwner owner, ResourceOwnerDeleteProc proc, void *arg); +extern void UnregisterResownerOnDeleteProc(ResourceOwner owner, ResourceOwnerDeleteProc proc, void *arg); #endif /* RESOWNER_H */ --------------000205000107010208030607 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general --------------000205000107010208030607--