elog.c query_id support vs shutdown - Mailing list pgsql-hackers

From Andres Freund
Subject elog.c query_id support vs shutdown
Date
Msg-id 20210807234407.icku2rnqyapsb3io@alap3.anarazel.de
Whole thread Raw
Responses Re: elog.c query_id support vs shutdown  (Julien Rouhaud <rjuju123@gmail.com>)
List pgsql-hackers
Hi,

In the course of [1] I again looked at all pgstat_* callsites and noticed the
new calls to pgstat_get_my_query_id() in elog.c.

As currently implemented those pgstat_get_my_query_id() calls are not
safe. It's fine during backend startup because MyBEEntry is not set, but
during shutdown that's not ok, because we never unset MyBEEntry.

andres@awork3:~/src/postgresql$ /home/andres/build/postgres/dev-assert/vpath/src/backend/postgres --single postgres -D
/srv/dev/pgdev-dev/-c 'log_line_prefix=%Q' -c log_min_messages=debug1
 
0NOTICE:  database system was shut down at 2021-08-07 16:42:26 PDT
0DEBUG:  checkpoint record is at 1/1C000508
0DEBUG:  redo record is at 1/1C000508; shutdown true
0DEBUG:  next transaction ID: 213067; next OID: 16434
0DEBUG:  next MultiXactId: 1; next MultiXactOffset: 0
0DEBUG:  oldest unfrozen transaction ID: 701, in database 1
0DEBUG:  oldest MultiXactId: 1, in database 1
0DEBUG:  commit timestamp Xid oldest/newest: 0/0
0DEBUG:  transaction ID wrap limit is 2147484348, limited by database with OID 1
0DEBUG:  MultiXactId wrap limit is 2147483648, limited by database with OID 1
0DEBUG:  starting up replication slots
0DEBUG:  MultiXactId wrap limit is 2147483648, limited by database with OID 1
0DEBUG:  MultiXact member stop limit is now 4294914944 based on MultiXact 1

PostgreSQL stand-alone backend 15devel
backend> 0NOTICE:  shutting down
0DEBUG:  performing replication slot checkpoint
Segmentation fault

#0  0x000055cb9b871632 in pgstat_get_my_query_id () at
/home/andres/src/postgresql/src/backend/utils/activity/backend_status.c:1042
#1  0x000055cb9ba00398 in log_line_prefix (buf=0x7ffc751092e0, edata=0x55cb9bd6a900 <errordata>)
    at /home/andres/src/postgresql/src/backend/utils/error/elog.c:2723
#2  0x000055cb9ba00ee6 in send_message_to_server_log (edata=0x55cb9bd6a900 <errordata>) at
/home/andres/src/postgresql/src/backend/utils/error/elog.c:3017
#3  0x000055cb9b9fe1b3 in EmitErrorReport () at /home/andres/src/postgresql/src/backend/utils/error/elog.c:1542
#4  0x000055cb9b9fb600 in errfinish (filename=0x55cb9bc1a0eb "miscinit.c", lineno=974, funcname=0x55cb9bc1aea0
<__func__.5>"UnlinkLockFiles")
 
    at /home/andres/src/postgresql/src/backend/utils/error/elog.c:597
#5  0x000055cb9ba0f2b4 in UnlinkLockFiles (status=0, arg=0) at
/home/andres/src/postgresql/src/backend/utils/init/miscinit.c:974
#6  0x000055cb9b8148da in proc_exit_prepare (code=0) at /home/andres/src/postgresql/src/backend/storage/ipc/ipc.c:209
#7  0x000055cb9b814792 in proc_exit (code=0) at /home/andres/src/postgresql/src/backend/storage/ipc/ipc.c:107
#8  0x000055cb9b851568 in PostgresMain (argc=9, argv=0x55cb9daddec0, dbname=0x55cb9dac4f70 "postgres",
username=0x55cb9dac3580"andres")
 
    at /home/andres/src/postgresql/src/backend/tcop/postgres.c:4702
#9  0x000055cb9b672c5b in main (argc=9, argv=0x55cb9daddec0) at
/home/andres/src/postgresql/src/backend/main/main.c:195


I suspect that to make the elog.c usage safe, we'll have to clear MyBEEntry in
pgstat_beshutdown_hook().

Greetings,

Andres Freund

[1] https://www.postgresql.org/message-id/20210807210349.bby5ta2xrbnte6ht%40alap3.anarazel.de



pgsql-hackers by date:

Previous
From: ilmari@ilmari.org (Dagfinn Ilmari Mannsåker)
Date:
Subject: Re: perlcritic: prohibit map and grep in void conext
Next
From: Tom Lane
Date:
Subject: Re: Assert triggered during RE_compile_and_cache