pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
 Re[2]: [BUGS] Segfault in MemoryContextAlloc
8+ messages / 4 participants
[nested] [flat]

*  Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-01-27 10:47  Dmitriy Sarafannikov <dimon99901@mail.ru>
  0 siblings, 2 replies; 8+ messages in thread

From: Dmitriy Sarafannikov @ 2016-01-27 10:47 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs


>
>That's, uh, weird. TopTransactionContext should never be NULL here. This
>is with a stock 9.4.5, without any further extensions configured?

It is a stock 9.4.5 with pg_buffercache (1.0) and plv8 (1.4.0) installed extensions.

-- 
Best regards,
Dmitriy Sarafannikov


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

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-01-27 11:23  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Dmitriy Sarafannikov <dimon99901@mail.ru>
  1 sibling, 1 reply; 8+ messages in thread

From: Pavel Stehule @ 2016-01-27 11:23 UTC (permalink / raw)
  To: Dmitriy Sarafannikov <dimon99901@mail.ru>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

Hi

2016-01-27 11:47 GMT+01:00 Dmitriy Sarafannikov <dimon99901@mail.ru>:

>
> That's, uh, weird. TopTransactionContext should never be NULL here. This
> is with a stock 9.4.5, without any further extensions configured?
>
>
> It is a stock 9.4.5 with pg_buffercache (1.0) and plv8 (1.4.0) installed
> extensions.
>

I had to fix our log processing extension for 9.5 due NULL
TopTransactionContext. So I can confirm this issue.

When I start database vacuuming by analyze_new_cluster.sh, then Postgres
randomly fails in my extension.

Regards

Pavel





>
> --
> Best regards,
> Dmitriy Sarafannikov
>

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

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-01-27 12:26  Andres Freund <andres@anarazel.de>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 2 replies; 8+ messages in thread

From: Andres Freund @ 2016-01-27 12:26 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; Dmitriy Sarafannikov <dimon99901@mail.ru>; +Cc: pgsql-bugs

On January 27, 2016 12:23:07 PM GMT+01:00, Pavel Stehule <pavel.stehule@gmail.com> wrote:
>Hi
>
>2016-01-27 11:47 GMT+01:00 Dmitriy Sarafannikov <dimon99901@mail.ru>:
>
>>
>> That's, uh, weird. TopTransactionContext should never be NULL here.
>This
>> is with a stock 9.4.5, without any further extensions configured?
>>
>>
>> It is a stock 9.4.5 with pg_buffercache (1.0) and plv8 (1.4.0)
>installed
>> extensions.
>>
>
>I had to fix our log processing extension for 9.5 due NULL
>TopTransactionContext. So I can confirm this issue.

But this report isn't about 9.5 afaics.

>When I start database vacuuming by analyze_new_cluster.sh, then
>Postgres
>randomly fails in my extension.

What exactly does your extension do? And where did you add that null check?

Andres

--- 
Please excuse brevity and formatting - I am writing this on my mobile phone.


-- 
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] 8+ messages in thread

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-01-27 12:36  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 1 reply; 8+ messages in thread

From: Pavel Stehule @ 2016-01-27 12:36 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Dmitriy Sarafannikov <dimon99901@mail.ru>; pgsql-bugs

2016-01-27 13:26 GMT+01:00 Andres Freund <andres@anarazel.de>:

> On January 27, 2016 12:23:07 PM GMT+01:00, Pavel Stehule <
> pavel.stehule@gmail.com> wrote:
> >Hi
> >
> >2016-01-27 11:47 GMT+01:00 Dmitriy Sarafannikov <dimon99901@mail.ru>:
> >
> >>
> >> That's, uh, weird. TopTransactionContext should never be NULL here.
> >This
> >> is with a stock 9.4.5, without any further extensions configured?
> >>
> >>
> >> It is a stock 9.4.5 with pg_buffercache (1.0) and plv8 (1.4.0)
> >installed
> >> extensions.
> >>
> >
> >I had to fix our log processing extension for 9.5 due NULL
> >TopTransactionContext. So I can confirm this issue.
>
> But this report isn't about 9.5 afaics.
>
> >When I start database vacuuming by analyze_new_cluster.sh, then
> >Postgres
> >randomly fails in my extension.
>
> What exactly does your extension do? And where did you add that null check?
>

this extension does some log preprocessing for syslog

extension and fix attached

Regards

Pavel


> Andres
>
> ---
> Please excuse brevity and formatting - I am writing this on my mobile
> phone.
>

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

Attachments: download all (2 files, .tar.gz)

gdlogger.patch (text/x-patch, 6.0K) download *** ./contrib/gdlogger/gdlogger.c.orig 2015-10-22 12:03:36.910858470 +0200 --- ./contrib/gdlogger/gdlogger.c 2015-10-22 12:03:14.796733274 +0200 *************** *** 0 **** --- 1,225 ---- + /*------------------------------------------------------------------------- + * + * gdlogger.c + * + * + * Copyright (c) 2008-2012, PostgreSQL Global Development Group + * + * IDENTIFICATION + * contrib/gdlogger/gdlogger.c + * + *------------------------------------------------------------------------- + */ + #include "postgres.h" + + #include "fmgr.h" + #include "miscadmin.h" + + #include "catalog/pg_type.h" + #include "commands/dbcommands.h" + #include "storage/lwlock.h" + #include "storage/ipc.h" + #include "storage/shmem.h" + #include "storage/spin.h" + #include "utils/builtins.h" + #include "utils/elog.h" + #include "utils/guc.h" + #include "utils/lsyscache.h" + #include "utils/resowner.h" + + #include <syslog.h> + + PG_MODULE_MAGIC; + + typedef struct + { + slock_t mutex; /* protects output to syslog */ + } gdloggerData; + + gdloggerData *logger = NULL; + + emit_log_hook_type previous_log_hook = NULL; + static shmem_startup_hook_type prev_shmem_startup_hook = NULL; + + bool initialized = false; + + void _PG_init(void); + void _PG_fini(void); + + static void gdlogger(ErrorData *edata); + static void gdlogger_shmem_startup(void); + + static FmgrInfo inet_output; + Oid inet_output_func = InvalidOid; + + /* + * Module load callback + */ + void + _PG_init(void) + { + EmitWarningsOnPlaceholders("gdlogger"); + + /* + * Request additional shared resources. (These are no-ops if we're not in + * the postmaster process.) We'll allocate or attach to the shared + * resources in pgss_shmem_startup(). + */ + RequestAddinShmemSpace(sizeof(gdloggerData)); + RequestAddinLWLocks(1); + + /* + * Install hooks. + */ + prev_shmem_startup_hook = shmem_startup_hook; + shmem_startup_hook = gdlogger_shmem_startup; + previous_log_hook = emit_log_hook; + emit_log_hook = gdlogger; + + initialized = true; + } + + static void + gdlogger_shmem_startup(void) + { + bool found; + + if (prev_shmem_startup_hook) + prev_shmem_startup_hook(); + + /* + * Create or attach to the shared memory state, including hash table + */ + LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE); + + logger = ShmemInitStruct("gdlogger", + sizeof(gdloggerData), + &found); + + if (!found) + SpinLockInit(&logger->mutex); + + LWLockRelease(AddinShmemInitLock); + } + + /* + * Module unload callback + */ + void + _PG_fini(void) + { + shmem_startup_hook = prev_shmem_startup_hook; + emit_log_hook = previous_log_hook; + previous_log_hook = NULL; + } + + /* + * puts lines to syslog + * + */ + static void + syslog_fputs(int facility, char *lines) + { + char *str = pstrdup(lines); + char *line = str; + + while (1) + { + char *ptr = line; + + while (*ptr != '\0' && *ptr != '\n') + ptr++; + + if (*ptr == '\n') + { + *ptr = '\0'; + syslog(facility, "\t %s", line); + line = ptr + 1; + } + else + { + syslog(facility, "\t %s", line); + break; + } + } + + pfree(str); + } + + /* + * change facility for slow query + * 1 -- slow query statement + * 2 -- slow query plan (result of autoexplain) + * + */ + static void + gdlogger(ErrorData *edata) + { + if (previous_log_hook) + previous_log_hook(edata); + + if (edata->message != NULL && strncmp(edata->message, "duration: ", 9) == 0) + { + ResourceOwner local_res_owner = NULL; + Datum client_addr; + char *client_addr_str = NULL; + FunctionCallInfoData fcinfo; + char *database_name; + + if (CurrentResourceOwner == NULL) + { + local_res_owner = ResourceOwnerCreate(NULL, "fakeres"); + CurrentResourceOwner = local_res_owner; + } + + if (!OidIsValid(inet_output_func)) + { + bool is_varlena; + + getTypeOutputInfo(INETOID, &inet_output_func, &is_varlena); + fmgr_info(inet_output_func, &inet_output); + } + + InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); + client_addr = (*inet_client_addr) (&fcinfo); + if (!fcinfo.isnull) + client_addr_str = OutputFunctionCall(&inet_output, client_addr); + + database_name = get_database_name(MyDatabaseId); + + SpinLockAcquire(&logger->mutex); + + if (strcmp(edata->filename, "auto_explain.c") == 0) + { + syslog(LOG_LOCAL2, "auto_explain database=%s client=%s appname=%s", + database_name ? database_name : "NULL", + client_addr_str ? client_addr_str : "local", + application_name); + syslog_fputs(LOG_LOCAL2, edata->message); + } + else + { + syslog(LOG_LOCAL1, "slow_query database=%s client=%s appname=%s", + database_name ? database_name : "NULL", + client_addr_str ? client_addr_str : "local", + application_name); + syslog_fputs(LOG_LOCAL1, edata->message); + } + + SpinLockRelease(&logger->mutex); + + if (local_res_owner != NULL) + { + CurrentResourceOwner = NULL; + ResourceOwnerDelete(local_res_owner); + } + + if (client_addr_str != NULL) + pfree(client_addr_str); + + if (database_name != NULL) + pfree(database_name); + + edata->output_to_server = false; + } + } *** ./contrib/gdlogger/Makefile.orig 2015-10-22 12:03:42.493890078 +0200 --- ./contrib/gdlogger/Makefile 2015-10-22 12:03:14.796733274 +0200 *************** *** 0 **** --- 1,15 ---- + # contrib/gdlogger/Makefile + + MODULE_big = gdlogger + OBJS = gdlogger.o + + ifdef USE_PGXS + PG_CONFIG = pg_config + PGXS := $(shell $(PG_CONFIG) --pgxs) + include $(PGXS) + else + subdir = contrib/gdlogger + top_builddir = ../.. + include $(top_builddir)/src/Makefile.global + include $(top_srcdir)/contrib/contrib-global.mk + endif *** ./contrib/Makefile.orig 2015-10-22 11:56:03.450690007 +0200 --- ./contrib/Makefile 2015-10-22 12:03:14.797733279 +0200 *************** *** 19,24 **** --- 19,25 ---- earthdistance \ file_fdw \ fuzzystrmatch \ + gdlogger \ hstore \ intagg \ intarray \
gdlogger-9.5-fix.patch (text/x-patch, 2.1K) download *** ./contrib/gdlogger/gdlogger.c.orig 2016-01-18 17:21:19.000000000 +0100 --- ./contrib/gdlogger/gdlogger.c 2016-01-19 15:32:48.569251792 +0100 *************** *** 25,30 **** --- 25,31 ---- #include "utils/elog.h" #include "utils/guc.h" #include "utils/lsyscache.h" + #include "utils/memutils.h" #include "utils/resowner.h" #include <syslog.h> *************** *** 161,167 **** if (edata->message != NULL && strncmp(edata->message, "duration: ", 9) == 0) { ResourceOwner local_res_owner = NULL; ! Datum client_addr; char *client_addr_str = NULL; FunctionCallInfoData fcinfo; char *database_name; --- 162,168 ---- if (edata->message != NULL && strncmp(edata->message, "duration: ", 9) == 0) { ResourceOwner local_res_owner = NULL; ! Datum client_addr = (Datum) 0; char *client_addr_str = NULL; FunctionCallInfoData fcinfo; char *database_name; *************** *** 172,190 **** CurrentResourceOwner = local_res_owner; } ! if (!OidIsValid(inet_output_func)) { ! bool is_varlena; ! ! getTypeOutputInfo(INETOID, &inet_output_func, &is_varlena); ! fmgr_info(inet_output_func, &inet_output); } - InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); - client_addr = (*inet_client_addr) (&fcinfo); - if (!fcinfo.isnull) - client_addr_str = OutputFunctionCall(&inet_output, client_addr); - database_name = get_database_name(MyDatabaseId); SpinLockAcquire(&logger->mutex); --- 173,194 ---- CurrentResourceOwner = local_res_owner; } ! if (TopTransactionContext != NULL) { ! if (!OidIsValid(inet_output_func)) ! { ! bool is_varlena; ! ! getTypeOutputInfo(INETOID, &inet_output_func, &is_varlena); ! fmgr_info(inet_output_func, &inet_output); ! } ! ! InitFunctionCallInfoData(fcinfo, NULL, 0, InvalidOid, NULL, NULL); ! client_addr = (*inet_client_addr) (&fcinfo); ! if (!fcinfo.isnull) ! client_addr_str = OutputFunctionCall(&inet_output, client_addr); } database_name = get_database_name(MyDatabaseId); SpinLockAcquire(&logger->mutex);
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-01-27 13:05  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 0 replies; 8+ messages in thread

From: Pavel Stehule @ 2016-01-27 13:05 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Dmitriy Sarafannikov <dimon99901@mail.ru>; pgsql-bugs

2016-01-27 13:26 GMT+01:00 Andres Freund <andres@anarazel.de>:

> On January 27, 2016 12:23:07 PM GMT+01:00, Pavel Stehule <
> pavel.stehule@gmail.com> wrote:
> >Hi
> >
> >2016-01-27 11:47 GMT+01:00 Dmitriy Sarafannikov <dimon99901@mail.ru>:
> >
> >>
> >> That's, uh, weird. TopTransactionContext should never be NULL here.
> >This
> >> is with a stock 9.4.5, without any further extensions configured?
> >>
> >>
> >> It is a stock 9.4.5 with pg_buffercache (1.0) and plv8 (1.4.0)
> >installed
> >> extensions.
> >>
> >
> >I had to fix our log processing extension for 9.5 due NULL
> >TopTransactionContext. So I can confirm this issue.
>
> But this report isn't about 9.5 afaics.
>

it should not be - we do migration from 9.2 to 9.5 - so this issue can be
related to 9.3, 9.4

Pavel


>
> >When I start database vacuuming by analyze_new_cluster.sh, then
> >Postgres
> >randomly fails in my extension.
>
> What exactly does your extension do? And where did you add that null check?
>
> Andres
>
> ---
> Please excuse brevity and formatting - I am writing this on my mobile
> phone.
>

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

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-02-06 23:30  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2016-02-06 23:30 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Andres Freund <andres@anarazel.de>; Dmitriy Sarafannikov <dimon99901@mail.ru>; pgsql-bugs

Pavel Stehule <pavel.stehule@gmail.com> writes:
> 2016-01-27 13:26 GMT+01:00 Andres Freund <andres@anarazel.de>:
>> What exactly does your extension do? And where did you add that null check?

> this extension does some log preprocessing for syslog
> extension and fix attached

That extension is broken on its face.  You cannot have an emit_log_hook
that does catalog access, because elog() can be called outside a
transaction or in an already-failed transaction.  Whatever band-aids
you put around that will just be band-aids that will fail regularly.
You need to move the catalog lookup someplace else.  (Actually, I wonder
why you're doing a catalog lookup at all ... wouldn't it be sufficient to
do a DirectFunctionCall to inet_out?)

I doubt this has much to do with the OP's issue.

			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] 8+ messages in thread

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-02-06 23:37  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Dmitriy Sarafannikov <dimon99901@mail.ru>
  1 sibling, 0 replies; 8+ messages in thread

From: Tom Lane @ 2016-02-06 23:37 UTC (permalink / raw)
  To: Dmitriy Sarafannikov <dimon99901@mail.ru>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

=?UTF-8?B?RG1pdHJpeSBTYXJhZmFubmlrb3Y=?= <dimon99901@mail.ru> writes:
>> That's, uh, weird. TopTransactionContext should never be NULL here. This
>> is with a stock 9.4.5, without any further extensions configured?

> It is a stock 9.4.5 with pg_buffercache (1.0) and plv8 (1.4.0) installed extensions.

Both this and the other behavior you reported sure seem like "can't
happen" situations.  I do have a theory though: I think you are doing
something that is spawning multiple threads inside the backend, and then
the backend logic goes nuts because it's expecting to be single-threaded.

pg_buffercache wouldn't do that, and a quick google search suggests that
V8 knows nothing of threads either.  Have you got anything else going
on in there?

			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] 8+ messages in thread

* Re: Re[2]: [BUGS] Segfault in MemoryContextAlloc
@ 2016-02-08 05:32  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 8+ messages in thread

From: Pavel Stehule @ 2016-02-08 05:32 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andres Freund <andres@anarazel.de>; Dmitriy Sarafannikov <dimon99901@mail.ru>; pgsql-bugs

2016-02-07 0:30 GMT+01:00 Tom Lane <tgl@sss.pgh.pa.us>:

> Pavel Stehule <pavel.stehule@gmail.com> writes:
> > 2016-01-27 13:26 GMT+01:00 Andres Freund <andres@anarazel.de>:
> >> What exactly does your extension do? And where did you add that null
> check?
>
> > this extension does some log preprocessing for syslog
> > extension and fix attached
>
> That extension is broken on its face.  You cannot have an emit_log_hook
> that does catalog access, because elog() can be called outside a
> transaction or in an already-failed transaction.  Whatever band-aids
> you put around that will just be band-aids that will fail regularly.
> You need to move the catalog lookup someplace else.  (Actually, I wonder
> why you're doing a catalog lookup at all ... wouldn't it be sufficient to
> do a DirectFunctionCall to inet_out?)
>

yes - the design was unlucky - and I fixed it without the necessity to
touch system catalogue there.

The interesting on this issue is fact, so this extension in form, that I
sent (badly designed) worked without any problems on 9.1, and 9.2 and
failing with low probability on 9.4, 9.5.

Regards

Pavel


>
> I doubt this has much to do with the OP's issue.
>
>                         regards, tom lane
>

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


end of thread, other threads:[~2016-02-08 05:32 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-27 10:47  Re[2]: [BUGS] Segfault in MemoryContextAlloc Dmitriy Sarafannikov <dimon99901@mail.ru>
2016-01-27 11:23 ` Pavel Stehule <pavel.stehule@gmail.com>
2016-01-27 12:26   ` Andres Freund <andres@anarazel.de>
2016-01-27 12:36     ` Pavel Stehule <pavel.stehule@gmail.com>
2016-02-06 23:30       ` Tom Lane <tgl@sss.pgh.pa.us>
2016-02-08 05:32         ` Pavel Stehule <pavel.stehule@gmail.com>
2016-01-27 13:05     ` Pavel Stehule <pavel.stehule@gmail.com>
2016-02-06 23:37 ` Tom Lane <tgl@sss.pgh.pa.us>

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