|
41 | 41 | #include"miscadmin.h" |
42 | 42 | #include"pgstat.h" |
43 | 43 | #include"postmaster/bgwriter.h" |
| 44 | +#include"postmaster/startup.h" |
44 | 45 | #include"replication/walreceiver.h" |
45 | 46 | #include"replication/walsender.h" |
46 | 47 | #include"storage/bufmgr.h" |
@@ -584,19 +585,6 @@ typedef struct xl_restore_point |
584 | 585 | charrp_name[MAXFNAMELEN]; |
585 | 586 | }xl_restore_point; |
586 | 587 |
|
587 | | -/* |
588 | | - * Flags set by interrupt handlers for later service in the redo loop. |
589 | | - */ |
590 | | -staticvolatilesig_atomic_tgot_SIGHUP= false; |
591 | | -staticvolatilesig_atomic_tshutdown_requested= false; |
592 | | -staticvolatilesig_atomic_tpromote_triggered= false; |
593 | | - |
594 | | -/* |
595 | | - * Flag set when executing a restore command, to tell SIGTERM signal handler |
596 | | - * that it's safe to just proc_exit. |
597 | | - */ |
598 | | -staticvolatilesig_atomic_tin_restore_command= false; |
599 | | - |
600 | 588 |
|
601 | 589 | staticvoidXLogArchiveNotify(constchar*xlog); |
602 | 590 | staticvoidXLogArchiveNotifySeg(uint32log,uint32seg); |
@@ -3068,21 +3056,16 @@ RestoreArchivedFile(char *path, const char *xlogfname, |
3068 | 3056 | xlogRestoreCmd))); |
3069 | 3057 |
|
3070 | 3058 | /* |
3071 | | - * Set in_restore_command to tell the signal handler that we should exit |
3072 | | - * right away on SIGTERM. We know that we're at a safe point to do that. |
3073 | | - * Check if we had already received the signal, so that we don't miss a |
3074 | | - * shutdown request received just before this. |
| 3059 | + * Check signals before restore command and reset afterwards. |
3075 | 3060 | */ |
3076 | | -in_restore_command= true; |
3077 | | -if (shutdown_requested) |
3078 | | -proc_exit(1); |
| 3061 | +PreRestoreCommand(); |
3079 | 3062 |
|
3080 | 3063 | /* |
3081 | 3064 | * Copy xlog from archival storage to XLOGDIR |
3082 | 3065 | */ |
3083 | 3066 | rc=system(xlogRestoreCmd); |
3084 | 3067 |
|
3085 | | -in_restore_command= false; |
| 3068 | +PostRestoreCommand(); |
3086 | 3069 |
|
3087 | 3070 | if (rc==0) |
3088 | 3071 | { |
@@ -9946,177 +9929,6 @@ CancelBackup(void) |
9946 | 9929 | } |
9947 | 9930 | } |
9948 | 9931 |
|
9949 | | -/* ------------------------------------------------------ |
9950 | | - *Startup Process main entry point and signal handlers |
9951 | | - * ------------------------------------------------------ |
9952 | | - */ |
9953 | | - |
9954 | | -/* |
9955 | | - * startupproc_quickdie() occurs when signalled SIGQUIT by the postmaster. |
9956 | | - * |
9957 | | - * Some backend has bought the farm, |
9958 | | - * so we need to stop what we're doing and exit. |
9959 | | - */ |
9960 | | -staticvoid |
9961 | | -startupproc_quickdie(SIGNAL_ARGS) |
9962 | | -{ |
9963 | | -PG_SETMASK(&BlockSig); |
9964 | | - |
9965 | | -/* |
9966 | | - * We DO NOT want to run proc_exit() callbacks -- we're here because |
9967 | | - * shared memory may be corrupted, so we don't want to try to clean up our |
9968 | | - * transaction. Just nail the windows shut and get out of town. Now that |
9969 | | - * there's an atexit callback to prevent third-party code from breaking |
9970 | | - * things by calling exit() directly, we have to reset the callbacks |
9971 | | - * explicitly to make this work as intended. |
9972 | | - */ |
9973 | | -on_exit_reset(); |
9974 | | - |
9975 | | -/* |
9976 | | - * Note we do exit(2) not exit(0).This is to force the postmaster into a |
9977 | | - * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random |
9978 | | - * backend. This is necessary precisely because we don't clean up our |
9979 | | - * shared memory state. (The "dead man switch" mechanism in pmsignal.c |
9980 | | - * should ensure the postmaster sees this as a crash, too, but no harm in |
9981 | | - * being doubly sure.) |
9982 | | - */ |
9983 | | -exit(2); |
9984 | | -} |
9985 | | - |
9986 | | - |
9987 | | -/* SIGUSR1: let latch facility handle the signal */ |
9988 | | -staticvoid |
9989 | | -StartupProcSigUsr1Handler(SIGNAL_ARGS) |
9990 | | -{ |
9991 | | -intsave_errno=errno; |
9992 | | - |
9993 | | -latch_sigusr1_handler(); |
9994 | | - |
9995 | | -errno=save_errno; |
9996 | | -} |
9997 | | - |
9998 | | -/* SIGUSR2: set flag to finish recovery */ |
9999 | | -staticvoid |
10000 | | -StartupProcTriggerHandler(SIGNAL_ARGS) |
10001 | | -{ |
10002 | | -intsave_errno=errno; |
10003 | | - |
10004 | | -promote_triggered= true; |
10005 | | -WakeupRecovery(); |
10006 | | - |
10007 | | -errno=save_errno; |
10008 | | -} |
10009 | | - |
10010 | | -/* SIGHUP: set flag to re-read config file at next convenient time */ |
10011 | | -staticvoid |
10012 | | -StartupProcSigHupHandler(SIGNAL_ARGS) |
10013 | | -{ |
10014 | | -intsave_errno=errno; |
10015 | | - |
10016 | | -got_SIGHUP= true; |
10017 | | -WakeupRecovery(); |
10018 | | - |
10019 | | -errno=save_errno; |
10020 | | -} |
10021 | | - |
10022 | | -/* SIGTERM: set flag to abort redo and exit */ |
10023 | | -staticvoid |
10024 | | -StartupProcShutdownHandler(SIGNAL_ARGS) |
10025 | | -{ |
10026 | | -intsave_errno=errno; |
10027 | | - |
10028 | | -if (in_restore_command) |
10029 | | -proc_exit(1); |
10030 | | -else |
10031 | | -shutdown_requested= true; |
10032 | | -WakeupRecovery(); |
10033 | | - |
10034 | | -errno=save_errno; |
10035 | | -} |
10036 | | - |
10037 | | -/* Handle SIGHUP and SIGTERM signals of startup process */ |
10038 | | -void |
10039 | | -HandleStartupProcInterrupts(void) |
10040 | | -{ |
10041 | | -/* |
10042 | | - * Check if we were requested to re-read config file. |
10043 | | - */ |
10044 | | -if (got_SIGHUP) |
10045 | | -{ |
10046 | | -got_SIGHUP= false; |
10047 | | -ProcessConfigFile(PGC_SIGHUP); |
10048 | | -} |
10049 | | - |
10050 | | -/* |
10051 | | - * Check if we were requested to exit without finishing recovery. |
10052 | | - */ |
10053 | | -if (shutdown_requested) |
10054 | | -proc_exit(1); |
10055 | | - |
10056 | | -/* |
10057 | | - * Emergency bailout if postmaster has died. This is to avoid the |
10058 | | - * necessity for manual cleanup of all postmaster children. |
10059 | | - */ |
10060 | | -if (IsUnderPostmaster&& !PostmasterIsAlive()) |
10061 | | -exit(1); |
10062 | | -} |
10063 | | - |
10064 | | -/* Main entry point for startup process */ |
10065 | | -void |
10066 | | -StartupProcessMain(void) |
10067 | | -{ |
10068 | | -/* |
10069 | | - * If possible, make this process a group leader, so that the postmaster |
10070 | | - * can signal any child processes too. |
10071 | | - */ |
10072 | | -#ifdefHAVE_SETSID |
10073 | | -if (setsid()<0) |
10074 | | -elog(FATAL,"setsid() failed: %m"); |
10075 | | -#endif |
10076 | | - |
10077 | | -/* |
10078 | | - * Properly accept or ignore signals the postmaster might send us. |
10079 | | - * |
10080 | | - * Note: ideally we'd not enable handle_standby_sig_alarm unless actually |
10081 | | - * doing hot standby, but we don't know that yet. Rely on it to not do |
10082 | | - * anything if it shouldn't. |
10083 | | - */ |
10084 | | -pqsignal(SIGHUP,StartupProcSigHupHandler);/* reload config file */ |
10085 | | -pqsignal(SIGINT,SIG_IGN);/* ignore query cancel */ |
10086 | | -pqsignal(SIGTERM,StartupProcShutdownHandler);/* request shutdown */ |
10087 | | -pqsignal(SIGQUIT,startupproc_quickdie);/* hard crash time */ |
10088 | | -if (EnableHotStandby) |
10089 | | -pqsignal(SIGALRM,handle_standby_sig_alarm);/* ignored unless |
10090 | | - * InHotStandby */ |
10091 | | -else |
10092 | | -pqsignal(SIGALRM,SIG_IGN); |
10093 | | -pqsignal(SIGPIPE,SIG_IGN); |
10094 | | -pqsignal(SIGUSR1,StartupProcSigUsr1Handler); |
10095 | | -pqsignal(SIGUSR2,StartupProcTriggerHandler); |
10096 | | - |
10097 | | -/* |
10098 | | - * Reset some signals that are accepted by postmaster but not here |
10099 | | - */ |
10100 | | -pqsignal(SIGCHLD,SIG_DFL); |
10101 | | -pqsignal(SIGTTIN,SIG_DFL); |
10102 | | -pqsignal(SIGTTOU,SIG_DFL); |
10103 | | -pqsignal(SIGCONT,SIG_DFL); |
10104 | | -pqsignal(SIGWINCH,SIG_DFL); |
10105 | | - |
10106 | | -/* |
10107 | | - * Unblock signals (they were blocked when the postmaster forked us) |
10108 | | - */ |
10109 | | -PG_SETMASK(&UnBlockSig); |
10110 | | - |
10111 | | -StartupXLOG(); |
10112 | | - |
10113 | | -/* |
10114 | | - * Exit normally. Exit code 0 tells postmaster that we completed recovery |
10115 | | - * successfully. |
10116 | | - */ |
10117 | | -proc_exit(0); |
10118 | | -} |
10119 | | - |
10120 | 9932 | /* |
10121 | 9933 | * Read the XLOG page containing RecPtr into readBuf (if not read already). |
10122 | 9934 | * Returns true if the page is read successfully. |
@@ -10564,12 +10376,12 @@ CheckForStandbyTrigger(void) |
10564 | 10376 | if (triggered) |
10565 | 10377 | return true; |
10566 | 10378 |
|
10567 | | -if (promote_triggered) |
| 10379 | +if (IsPromoteTriggered()) |
10568 | 10380 | { |
10569 | 10381 | ereport(LOG, |
10570 | 10382 | (errmsg("received promote request"))); |
10571 | 10383 | ShutdownWalRcv(); |
10572 | | -promote_triggered= false; |
| 10384 | +ResetPromoteTriggered(); |
10573 | 10385 | triggered= true; |
10574 | 10386 | return true; |
10575 | 10387 | } |
|