Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of ProcessRepliesIfAny function in PostgreSQL

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly talks about "ProcessRepliesIfAny function Analysis in PostgreSQL". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "ProcessRepliesIfAny function Analysis in PostgreSQL".

The call stack is as follows:

(gdb) bt#0 0x00007fb6e6390903 in _ epoll_wait_nocancel from / lib64/libc.so.6#1 0x000000000088e668 in WaitEventSetWaitBlock (set=0x10ac808, cur_timeout=29999, occurred_events=0x7ffd634441b0, nevents=1) at latch.c:1048#2 0x000000000088e543 in WaitEventSetWait (set=0x10ac808, timeout=29999, occurred_events=0x7ffd634441b0, nevents=1, wait_event_info=83886092) at latch.c:1000#3 0x000000000088dcec in WaitLatchOrSocket (latch=0x7fb6dcbfc4d4, wakeEvents=27, sock=10, timeout=29999 Wait_event_info=83886092) at latch.c:385#4 0x000000000085405b in WalSndLoop (send_data=0x8547fe) at walsender.c:2229#5 0x0000000000851c93 in StartReplication (cmd=0x10ab750) at walsender.c:684#6 0x00000000008532f0 in exec_replication_command (cmd_string=0x101dd78 "START_REPLICATION 0/5D000000 TIMELINE 16") at walsender.c:1539#7 0x00000000008c0170 in PostgresMain (argc=1, argv=0x1049cb8, dbname=0x1049ba8 "" Username=0x1049b80 "replicator") at postgres.c:4178#8 0x000000000081e06c in BackendRun (port=0x103fb50) at postmaster.c:4361#9 0x000000000081d7df in BackendStartup (port=0x103fb50) at postmaster.c:4033#10 0x0000000000819bd9 in ServerLoop () at postmaster.c:1706#11 0x000000000081948f in PostmasterMain (argc=1, argv=0x1018a50) at postmaster.c:1379#12 0x0000000000742931 in main (argc=1, argv=0x1018a50) at main.c:228 1, data structure

N/A

Second, source code interpretation

ProcessRepliesIfAny

During streaming, it processes the received messages and checks whether the remote terminal closes the connection and performs related processing.

The code is not much or complex, so you can read it by yourself.

/ * Process any incoming messages while streaming. Also checks if the remote * end has closed the connection. * during streaming, the received messages are processed. * at the same time, check whether the remote terminal has closed the connection and perform relevant processing. * / static voidProcessRepliesIfAny (void) {unsigned char firstchar; int r; bool received = false; / / current time last_processing = GetCurrentTimestamp (); for (;;) {/-receive related messages pq_startmsgread (); r = pq_getbyte_if_available (& firstchar) If (r < 0) {/ * unexpected error or EOF * / / unknown exception or EOF ereport (COMMERROR, (errcode (ERRCODE_PROTOCOL_VIOLATION), errmsg ("unexpected EOF on standby connection")); / / process exits proc_exit (0) } if (r = = 0) {/ * no data available without blocking * / / No blocking message data, exit pq_endmsgread (); break;} / * Read the message contents * / / read message content resetStringInfo (& reply_message) If (pq_getmessage (& reply_message, 0)) {ereport (COMMERROR, (errcode (ERRCODE_PROTOCOL_VIOLATION), errmsg ("unexpected EOF on standby connection")); proc_exit (0) } / * If we already received a CopyDone from the frontend, the frontend * should not send us anything until we've closed our end of the COPY. * XXX: In theory, the frontend could already send the next command * before receiving the CopyDone, but libpq doesn't currently allow * that. * if a CopyDone message has been received at the front desk, the front desk should not send any more messages until COPY is closed. * XXX: in theory, the front desk may have sent the next command before receiving the CopyDone, but libpq does not allow this to happen * / if (streamingDoneReceiving & & firstchar! ='X') ereport (FATAL, (errcode (ERRCODE_PROTOCOL_VIOLATION), errmsg ("unexpected standby message type\"% c\ ", after receiving CopyDone") Firstchar) / * Handle the very limited subset of commands expected in this phase * / / handle a limited number of commands switch (firstchar) {/ *'d' means a standby reply wrapped in a CopyData packet. *'d' means that the reply of the standby node encapsulates the CopyData packet * / case'dreply: ProcessStandbyMessage (); received = true; break; / * * CopyDone means the standby requested to finish streaming. * Reply with CopyDone, if we had not sent that already. * CopyDone means that the standby node requests to end the streaming. * if it has not been sent, use CopyDone to reply. * / case'cages: if (! streamingDoneSending) {pq_putmessage_noblock ('caches, NULL, 0); streamingDoneSending = true;} streamingDoneReceiving = true; received = true; break / *'X' means that the standby is closing down the socket. *'X' means that the standby node is shutting down socket * / case 'Xnodes: proc_exit (0) Default: ereport (FATAL, (errcode (ERRCODE_PROTOCOL_VIOLATION), errmsg ("invalid standby message type\"% c\ ", firstchar);}} / * * Save the last reply timestamp if we've received at least one reply. * if at least one reply message is received, the last reply timestamp is saved. * / if (received) {last_reply_timestamp = last_processing; waiting_for_ping_response = false;}} II. Tracking analysis

Use gdb to track postmaster on the primary node, set a breakpoint on PostgresMain, start the standby node and enter the breakpoint

(gdb) set follow-fork-mode child (gdb) b ProcessRepliesIfAnyBreakpoint 2 at 0x85343b: file walsender.c, line 1597. (gdb) cContinuing.Breakpoint 2, ProcessRepliesIfAny () at walsender.c:15971597 bool received = false; (gdb)

View process information

[xdb@localhost ~] $ps-ef | grep postgresxdb 1376 1 0 14:16? 00:00:00 / appdb/xdb/pg11.2/bin/postgresxdb 1377 1376 0 14:16? 00:00:00 postgres: logger xdb 1550 1376 0 16:53? 00:00:00 postgres: checkpointer xdb 1551 1376 0 16:53? 00:00:00 postgres: background writer xdb 1552 1376 0 16:53? 00:00:00 postgres: walwriter xdb 1553 1376 0 16:53? 00:00:00 postgres: autovacuum launcher xdb 1554 1376 0 16:53? 00:00:00 postgres: archiver xdb 1555 1376 0 16:53? 00:00:00 postgres: stats collector xdb 1556 1376 0 16:53? 00:00:00 postgres: logical replication launcher xdb 1633 1376 0 17:26? 00:00:00 postgres: walsender replicator 192.168.26.26 (40528) idle

Receive related messages in a loop

(gdb) n1599 last_processing = GetCurrentTimestamp (); (gdb) 1603 pq_startmsgread (); (gdb) 1604 r = pq_getbyte_if_available (& firstchar); (gdb) 1605 if (r < 0) (gdb) pr $1 = 1 (gdb) p firstchar$2 = 100d' (gdb)

The command is' dumped'to perform the relevant processing

(gdb) n1613 if (r = = 0) (gdb) 1621 resetStringInfo (& reply_message); (gdb) 1622 if (& reply_message, 0) (gdb) 1637 if (streamingDoneReceiving & & firstchar! ='X') (gdb) 1644 switch (firstchar) (gdb) 1650 ProcessStandbyMessage (); (gdb) 1651 received = true (gdb) 1652 break; (gdb) 1681} (gdb)

Set breakpoint

(gdb) b walsender.c:1643Breakpoint 3 at 0x8535b6: file walsender.c, line 1643. (gdb) b walsender.c:1672Breakpoint 4 at 0x85361a: file walsender.c, line 1672. (gdb) cContinuing.Breakpoint 3, ProcessRepliesIfAny () at walsender.c:16441644 switch (firstchar) (gdb) Continuing....Breakpoint 4, ProcessRepliesIfAny () at walsender.c:16731673 proc_exit (0); (gdb)

The process is about to exit. View the process information

[xdb@localhost ~] $ps-ef | grep postgresxdb 1376 1 0 14:16? 00:00:00 / appdb/xdb/pg11.2/bin/postgresxdb 1377 1376 0 14:16? 00:00:00 postgres: logger xdb 1550 1376 0 16:53? 00:00:00 postgres: checkpointer xdb 1551 1376 0 16:53? 00:00:00 postgres: background writer xdb 1552 1376 0 16:53? 00:00:00 postgres: walwriter xdb 1553 1376 0 16:53? 00:00:00 postgres: autovacuum launcher xdb 1554 1376 0 16:53? 00:00:00 postgres: archiver xdb 1555 1376 0 16:53? 00:00:00 postgres: stats collector xdb 1556 1376 0 16:53? 00:00:00 postgres: logical replication launcher xdb 1633 1376 0 17:26? 00:00:00 postgres: walsender replicator 192.168.26.26 (40528) idlexdb 1637 1376 17:27? 00:00:00 postgres: walsender replicator 192.168.26.26 (40530) streaming 0/5D075248 [xdb@localhost] $

The process exits (PID=1633) and a new process is started (PID=1637)

(gdb) n [Inferior 2 (process 1633) exited normally] (gdb) so far, I believe you have a deeper understanding of "ProcessRepliesIfAny function analysis in PostgreSQL". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report