Description
Hi, I am getting a segmentation fault in PLJava 1.5.5 with PostgreSQL version 12.5. The stack trace looks like this
#0 0x00007f1759389795 pljavaDbName (libpljava-so-1.5.5.so)
#1 0x00007f1759380de3 JVMOptList_addVisualVMName (libpljava-so-1.5.5.so)
#2 0x00007f175937faff initsequencer (libpljava-so-1.5.5.so)
#3 0x00007f17593802f1 _PG_init (libpljava-so-1.5.5.so)
#4 0x0000000000882d4a internal_load_library (postgres)
#5 0x00000000008835d3 load_external_function (postgres)
#6 0x0000000000883f81 fmgr_info_cxt_security (postgres)
#7 0x0000000000883dad fmgr_info_cxt_security (postgres)
#8 0x000000000060d091 ExecInitFunc (postgres)
#9 0x000000000060acdc ExecInitExprRec (postgres)
#10 0x000000000060ce2c ExecInitExpr (postgres)
#11 0x000000000060cf65 ExecPrepareExpr (postgres)
#12 0x000000000060cfb3 ExecPrepareExprList (postgres)
#13 0x000000000052a962 FormIndexDatum (postgres)
#14 0x0000000000599364 do_analyze_rel.isra.3 (postgres)
#15 0x0000000000599964 analyze_rel (postgres)
#16 0x000000000060719a vacuum (postgres)
#17 0x00000000006e1124 do_autovacuum (postgres)
#18 0x00000000006e15e8 AutoVacWorkerMain.isra.6 (postgres)
#19 0x00000000006e1e89 StartAutoVacWorker (postgres)
#20 0x00000000006f00e2 sigusr1_handler (postgres)
#21 0x00007f19781095e0 __restore_rt (libpthread.so.0)
#22 0x00007f1975eb3783 __select (libc.so.6)
#23 0x0000000000483810 ServerLoop (postgres)
#24 0x00000000006f10b3 PostmasterMain (postgres)
#25 0x0000000000485073 main (postgres)
#26 0x00007f1975de5c05 __libc_start_main (libc.so.6)
#27 0x00000000004850da _start (postgres)
I have a table with function-based index where the function is written in Java. The crash happens when the table is autovacuumed. The crashing function is pljavaDbName (in InstallHelper.c) and happens because MyProcPort is NULL.
char *pljavaDbName()
{
#ifdef BGW_HAS_NO_MYPROCPORT
char *shortlived;
static char *longlived;
if ( IsBackgroundWorker )
{
if ( NULL == longlived )
{
shortlived = get_database_name(MyDatabaseId);
if ( NULL != shortlived )
{
longlived = MemoryContextStrdup(TopMemoryContext, shortlived);
pfree(shortlived);
}
}
return longlived;
}
#endif
return MyProcPort->database_name;
}Looking at the PostgreSQL 12.5 source I can see that when an autovacuum worker is started MyProcPort is not set.
As a workaround I changed PLJava Backend.c. I commented out lines 662-666
greeting = InstallHelper_hello();
ereport(NULL != pljavaLoadPath ? NOTICE : DEBUG1, (
errmsg("PL/Java loaded"),
errdetail("versions:\n%s", greeting)));
pfree(greeting);
and at line 564 I set seenVisualVMName to true.
These two changes solved my problem.
Looking at the latest version of PLJava, 1.6.2, the function pljavaDbName has not changed since 1.5.5.
Is there anything you can do to handle this issue in the official version?
Regards and Thanks
Richard Hennessy
Activity
jcflack commentedon Jul 11, 2021
Thank you for the report. Perhaps you have the first case of a Java-based functional index and therefore the first case of PL/Java waking up in the autovacuum worker. Past changes in that code have involved identifying specific cases where
MyProcPortcould be null, but clearly the autovacuum worker is another such case that hasn't been anticipated, and I may have to reorganize that code. It should be fairly straightforward (I think).jcflack commentedon Aug 22, 2021
Hi,
Would you be interested in building from the branch
bug/REL1_5_STABLE/issue355to see if it resolves your issue?I have tested in PG 12 and 13, using these operations (and default settings for autovacuum thresholds):
A segfault is reproducible without the patch.
ricdhen commentedon Aug 23, 2021
Sure, I'll give a try over the next few days and let you know how I get on. Thanks.
ricdhen commentedon Aug 24, 2021
After I compiled the code from the branch I am no longer able to reproduce the segmentation violation.
So it looks like it's fixed. Thank you.
What happens now? What official version will this fix be available in?
jcflack commentedon Aug 24, 2021
Barring unforeseen complications, the next ones, 1.6.3 and 1.5.8.
ricdhen commentedon Aug 24, 2021
Great, thanks again.
Merge branch 'REL1_5_STABLE' into REL1_6_STABLE
jcflack commentedon Oct 10, 2021
Believed resolved in 1.6.3 and 1.5.8.