当前位置:网站首页>PostgreSQL database replication - background first-class citizen process walreceiver ready_ to_ display

PostgreSQL database replication - background first-class citizen process walreceiver ready_ to_ display

2022-06-09 07:18:00 Tertium ferrugosum

 Insert picture description here
After processing walRcvState After the state , That is to do the normal process of the state machine (WALRCV_STARTING–>WALRCV_STREAMING), Will be set WalRcvData Of pid and walRcvState. Set up ready_to_display by false, Because we haven't learned from WalRcvData Remove connection information ( When the standby machine starts, it will call RequestXLogStreaming function , This function will GUC In the parameter primary_conninfo The information is kept in WalRcvData in (WalRcvData By WalRcvShmemInit Function initialized in shared memory ), It will also be saved WAL Log replication start LSN And timeline information ). The following is the code for extracting information :

	strlcpy(conninfo, (char *) walrcv->conninfo, MAXCONNINFO); //  take WalRcvData Copy the connection string of to the local conninfo in 
	strlcpy(slotname, (char *) walrcv->slotname, NAMEDATALEN); //  take WalRcvData Copy the slot name string of to the local slotname in 
	is_temp_slot = walrcv->is_temp_slot;                       //  Whether to use temporary copy slots 
	startpoint = walrcv->receiveStart;                         // Standby Want to ask for XLOG Logical address location 
	startpointTLI = walrcv->receiveStartTLI;                   // Standby Want to request a timeline 

receiveStart and receiveStartTLI Indicates the position and timeline of the first byte to be received . When the startup process starts walreceiver when , It sets these to the point where it wants the streaming to start .

/* Shared memory area for management of walreceiver process */
typedef struct {
    
	/* receiveStart and receiveStartTLI indicate the first byte position and timeline that will be received. When startup process starts the walreceiver, it sets these to the point where it wants the streaming to begin. */
	XLogRecPtr	receiveStart;
	TimeLineID	receiveStartTLI;
	...
} WalRcvData;	

lastMsgSendTime and lastMsgReceiptTime Represents the timestamp of message receiving and sending ,latestWalEndTime Recently received WAL Time for .
writtenUpto And flushedUpto similar , But advanced operations are performed after writing and before refreshing , No need to acquire a spin lock . up to now , Other processes can read data , It should not be used for data integrity purposes .writtenUpto yes Standby WalReceiver On wal Write the position ,flushedUpto yes Standby WalReceiver On wal The position of the brush plate .flushedUpto-1 Is the last byte position that has been received ,receivedTLI It's the timeline it comes from . In the first boot walreceiver when , These settings are receiveStart and receiveStartTLI. after , Whenever you will receive WAL Refresh to disk ,walreceiver Will update these contents .

	/* Initialise to a sanish value */
	walrcv->lastMsgSendTime = walrcv->lastMsgReceiptTime = walrcv->latestWalEndTime = now;	
	walrcv->latch = &MyProc->procLatch; /* Report the latch to use to awaken this process */
	SpinLockRelease(&walrcv->mutex);
	pg_atomic_write_u64(&WalRcv->writtenUpto, 0);

typedef struct {
    
	/* Time of send and receive of any message received. */
	TimestampTz lastMsgSendTime;
	TimestampTz lastMsgReceiptTime;
	/* Time Latest reported end of WAL on the sender */
	TimestampTz latestWalEndTime;
	/* Like flushedUpto, but advanced after writing and before flushing, without the need to acquire the spin lock. Data can be read by another process up to this point, but shouldn't be used for data integrity purposes. */
	pg_atomic_uint64 writtenUpto;
} WalRcvData;	

The signal processing logic and the shared memory exit callback function register and obtain libpqwalreceiver Some functions defined .

	/* Arrange to clean up at walreceiver exit */
	on_shmem_exit(WalRcvDie, 0);
	/* Properly accept or ignore signals the postmaster might send us */
	pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config file */
	pqsignal(SIGINT, SIG_IGN);
	pqsignal(SIGTERM, SignalHandlerForShutdownRequest); /* request shutdown */
	/* SIGQUIT handler was already set up by InitPostmasterChild */
	pqsignal(SIGALRM, SIG_IGN);
	pqsignal(SIGPIPE, SIG_IGN);
	pqsignal(SIGUSR1, procsignal_sigusr1_handler);
	pqsignal(SIGUSR2, SIG_IGN);
	/* Reset some signals that are accepted by postmaster but not here */
	pqsignal(SIGCHLD, SIG_DFL);
	/* Load the libpq-specific functions */
	load_file("libpqwalreceiver", false);
	if (WalReceiverFunctions == NULL) elog(ERROR, "libpqwalreceiver didn't initialize correctly");
	/* Unblock signals (they were blocked when the postmaster forked us) */
	PG_SETMASK(&UnBlockSig);	

PostgreSQL Database replication —— Backstage first-class citizen process WalReceiver Learn about connections
Save the connection string visible to the user . For safety's sake , This will destroy the original conninfo. Also save this walreceiver The host and port of the sending server connected to . Set the obtained connection string to walrcv->conninfo in ; take sender_host and sender_port Also set to walrcv in .

	tmp_conninfo = walrcv_get_conninfo(wrconn);  //  Save connection string 
	walrcv_get_senderinfo(wrconn, &sender_host, &sender_port);  //  Get the host name and port of the sender 
	memset(walrcv->conninfo, 0, MAXCONNINFO);
	if (tmp_conninfo) strlcpy((char *) walrcv->conninfo, tmp_conninfo, MAXCONNINFO);	
	memset(walrcv->sender_host, 0, NI_MAXHOST);
	if (sender_host) strlcpy((char *) walrcv->sender_host, sender_host, NI_MAXHOST);
	walrcv->sender_port = sender_port;
	walrcv->ready_to_display = true;	
原网站

版权声明
本文为[Tertium ferrugosum]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090705552504.html