Skip to content

PlJava 1.5.5 Segmentation Fault #355

Closed

Description

@ricdhen

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

  1. jcflack commented on Jul 11, 2021

    @jcflack
    Contributor

    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 MyProcPort could 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).

  2. jcflack commented on Aug 22, 2021

    @jcflack
    Contributor

    Hi,

    Would you be interested in building from the branch bug/REL1_5_STABLE/issue355 to see if it resolves your issue?

    I have tested in PG 12 and 13, using these operations (and default settings for autovacuum thresholds):

    CREATE EXTENSION pljava;
    \c
    CREATE FUNCTION foo(bar text) RETURNS float8 LANGUAGE java IMMUTABLE STRICT
    AS 'java.lang.Double.valueOf';
    CREATE TABLE baz(bar text);
    CREATE INDEX ON baz USING btree (foo(bar));
    INSERT INTO baz(bar) SELECT a::text FROM generate_series(1,100) AS t(a);
    INSERT INTO baz(bar) SELECT a::text FROM generate_series(101,200) AS t(a);
    DELETE FROM baz USING generate_series(51,150) AS t(a) WHERE bar = a::text;
    SELECT pg_sleep_for(current_setting('autovacuum_naptime')::interval);
    SELECT last_autovacuum, autovacuum_count
      FROM pg_stat_user_tables WHERE relname = 'baz';

    A segfault is reproducible without the patch.

  3. ricdhen commented on Aug 23, 2021

    @ricdhen
    Author

    Sure, I'll give a try over the next few days and let you know how I get on. Thanks.

  4. ricdhen commented on Aug 24, 2021

    @ricdhen
    Author

    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?

  5. jcflack commented on Aug 24, 2021

    @jcflack
    Contributor

    Barring unforeseen complications, the next ones, 1.6.3 and 1.5.8.

  6. ricdhen commented on Aug 24, 2021

    @ricdhen
    Author

    Great, thanks again.

  7. jcflack commented on Oct 10, 2021

    @jcflack
    Contributor

    Believed resolved in 1.6.3 and 1.5.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions