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

What did PostgreSQL do when responding to the backup command pg_basebackup from the client?

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

Share

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

This article mainly introduces "what PostgreSQL did when responding to the backup command pg_basebackup from the client". In the daily operation, it is believed that many people have doubts about what PostgreSQL did when responding to the backup command pg_basebackup from the client. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question "what did PostgreSQL do in response to the backup command pg_basebackup from the client?" Next, please follow the editor to study!

I. data structure

Basebackup_options

The option of pg_basebackup is parsed to the data structure on the database server.

Typedef struct {/ / backup label const char * label; / / does it show the progress bool progress; / / does it perform a fast fast checkpoint? Bool fastcheckpoint; / / nowait? Does bool nowait; / / include wal data bool includewal; / / uint32 maxrate; / / does it include tablespace mapping files? Bool sendtblspcmapfile;} basebackup_options; II. Source code interpretation

The database server receives the request, and postmaster starts a new postgres process to respond to the request, which is treated as walsender, marked with am_walsender set to T, and the following logic is executed in the PostgresMain function:

... For (;) / / main loop {... Switch (firstchar) {case 'Qcodes: / * simple query * / {if (am_walsender) {/ / if WAL sender, execute exec_replication_command if (! exec_replication_command (query_string)) exec_simple_query (query_string);.

Call the exec_replication_command function and execute the relevant commands. This function will call the SendBaseBackup function to execute the specific implementation logic, in which the key implementation function is sendFileWithContent/sendDir.

The 1.sendFileWithContent function is used to send files such as backup_label to the client

Pq_putmessage sends messages, and the message type of'd' indicates CopyData.

Static voidsendFileWithContent (const char * filename, const char * content) {struct stat statbuf; int pad, len; len = strlen (content); / * Construct a stat struct for the backup_label file we're injecting in * the tar. * / * Windows doesn't have the concept of uid and gid * / # ifdef WIN32 statbuf.st_uid = 0; statbuf.st_gid = 0; statbuf.st_gid = getegid (); # endif statbuf.st_mtime = time (NULL); statbuf.st_mode = pg_file_create_mode; statbuf.st_size = len; _ tarWriteHeader (filename, NULL, & statbuf, false) / * Send the contents as a CopyData message * / pq_putmessage ('dudes, content, len); / * Pad to 512 byte boundary, per tar format requirements * / pad = ((len + 511) & ~ 511)-len; if (pad > 0) {char buf [512]; MemSet (buf, 0, pad); pq_putmessage (' dudes, buf, pad);}}

2.sendDir traverses the file directory and calls sendFile to send it to the client

Recursively traverse the database directory and call sendFile to send files

... If (! sizeonly) sent = sendFile (pathbuf, pathbuf + basepathlen + 1, & statbuf, true, isDbDir? Pg_atoi (lastDir + 1, sizeof (Oid), 0): InvalidOid; if (sent | | sizeonly) {/ * Add size, rounded up to 512byte block * / size + = ((statbuf.st_size + 511) & ~ 511); size + = 512; / * Size of the header of the file * /}.

SendFile sends the corresponding file contents to the client.

... While ((cnt = fread (buf, 1, Min (sizeof (buf), statbuf- > st_size-len), fp) > 0) {. / * Send the chunk as a CopyData message * / if (pq_putmessage ('dice, buf, cnt)) ereport (ERROR, (errmsg ("base backup could not send data, aborting backup"));

Client starts pg_basebackup

[xdb@localhost] $pg_basebackup-h 192.168.26.25-U replicator-p 5432-D / data/backup-P-Xs-RPassword:

Trace postmaster, set tracking child process

(gdb) set follow-fork-mode child (gdb) b PostgresMain

After the client enters the password, it enters the breakpoint. Before executing the BASE_BACKUP command, it will first execute three SHOW data_directory_mode/SHOW wal_segment_size/IDENTIFY_SYSTEM commands, and then execute the BASE_BACKUP command.

(gdb) p input_message$2 = {data = 0x20a1d78 "SHOW data_directory_mode", len = 25, maxlen = 1024, cursor = 0}. (gdb) p input_message$4 = {data = 0x20a1d78 "SHOW wal_segment_size", len = 22, maxlen = 1024, cursor = 0}. (gdb) p input_message$5 = {data = 0x20a1d78 "IDENTIFY_SYSTEM", len = 16, maxlen = 1024 Cursor = 0}... (gdb) p input_message$7 = {data = 0x20a1d78 "BASE_BACKUP LABEL 'pg_basebackup basebackup' PROGRESS NOWAIT", len = 67, maxlen = 1024, cursor = 0}.

Tracking SendBaseBackup

(gdb) b SendBaseBackup...4178 if (! exec_replication_command (query_string)) (gdb) stepexec_replication_command (cmd_string=0x20a1d78 "BASE_BACKUP LABEL 'pg_basebackup basebackup' PROGRESS NOWAIT") at walsender.c:14381438 if (got_STOPPING) 1521 SendBaseBackup ((BaseBackupCmd *) cmd_node);

Enter SendBaseBackup

(gdb) cContinuing. [New process 1811] [Thread debugging using libthread_db enabled] Using host libthread_db library "/ lib64/libthread_db.so.1". [switching to Thread 0x7f46389b58c0 (LWP 1811)] Breakpoint 1, SendBaseBackup (cmd=0x2137da8) at basebackup.c:762762 parse_basebackup_options (cmd- > options, & opt); (gdb) n764 WalSndSetState (WALSNDSTATE_BACKUP); (gdb) p opt$1 = {label = 0x2137760 "pg_basebackup basebackup", progress = true, fastcheckpoint = false, nowait = true, includewal = false, maxrate = 0, sendtblspcmapfile = false}

It is the function perform_base_backup that actually executes the backup

... (gdb) 775 perform_base_backup (& opt); (gdb) stepperform_base_backup (opt=0x7ffc96573180) at basebackup.c:232

Execute sendXXX

320 if (ti- > path = = NULL) (gdb) 325 sendFileWithContent (BACKUP_LABEL_FILE, labelfile- > data) (gdb) p * labelfile$4 = {data = 0x2138a50 "START WAL LOCATION: 0tic66000028 (file 0000001",'0', "66)\ nCHECKPOINT LOCATION: 0Tabot 66000060\ nBACKUP METHOD: streamed\ nBACKUP FROM: master\ nSTART TIME: 2019-03-26 17:05:45 CST\ nLABEL: pg_basebackup base"..., len = 227,maxlen = 1024, cursor = 0} (gdb) n331 if (tblspc_map_file & opt- > sendtblspcmapfile) (gdb) 337 sendDir (".", 1, false. Tablespaces, true) (gdb) 340 if (lstat (XLOG_CONTROL_FILE, & statbuf)! = 0) (gdb) 345 sendFile (XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, & statbuf, false); (gdb) 356 if (opt- > includewal & & ti- > path = = NULL) (gdb) 361 pq_putemptymessage ('c') / * CopyDone * / (gdb) 309 foreach (lc, tablespaces) (gdb) 364 endptr = do_pg_stop_backup (labelfile- > data,! opt- > nowait, & endtli); (gdb) 366PG_END_ENSURE_ERROR_CLEANUP (base_backup_cleanup, (Datum) 0); (gdb) 369if (opt- > includewal) (gdb) 605SendXlogRecPtrResult (endptr, endtli) (gdb) 607 if (total_checksum_failures) (gdb) 623} (gdb) SendBaseBackup (cmd=0x2137da8) at basebackup.c:776776} (gdb) exec_replication_command (cmd_string=0x20a1d78 "BASE_BACKUP LABEL 'pg_basebackup basebackup' PROGRESS NOWAIT") at walsender.c:15221522 break (gdb) cContinuing. [Inferior 2 (process 1811) exited normally] (gdb) at this point, the study on "what PostgreSQL did when responding to the backup command pg_basebackup from the client" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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