1. 程式人生 > 實用技巧 >PG主程式模組(Main)——資料庫PostgreSQL入口

PG主程式模組(Main)——資料庫PostgreSQL入口

  PostgreSQL系統的主要功能都集中於Postgres程式,其入口是Main模組(src/backend/main/main.c)中的main函式,在初始化資料集簇、啟動資料庫伺服器時,都將從這裡開始執行。Main模組主要的工作是確定當前的作業系統平臺,並據此做一些平臺相關的環境變數設定和初始化,然後通過對命令列引數的判斷,將控制轉到相應的模組中去。PG使用一種專用伺服器程序體系結構,其中,最主要的兩個程序就是守護程序Postmaster和服務程序Postgres。從本質上來說,Postmaster和Postgres都是通過載入Postgres程式而形成的程序,只是在執行時所處的分支不同而已。

  Main模組為postgres的化身(postmaster、standalone backend、standalone bootstrap mode)做必要的啟動任務(確定當前的作業系統平臺,並據此做一些平臺相關的環境變數設定和初始化,然後通過對命令列引數的判斷),然後將控制權交給postgres。

 1 int main(int argc, char *argv[]){
 2     progname = get_progname(argv[0]);
 3         /* Platform-specific startup hacks */
 4         startup_hacks(progname);
5 /* Remember the physical location of the initially given argv[] array for possible use by ps display. */ 6 argv = save_ps_display_args(argc, argv); 7 /* Set up locale information from environment. */ 8 set_pglocale_pgservice(argv[0],PG_TEXTDOMAIN("postgres")); 9 #ifdef LC_MESSAGES
10 pg_perm_setlocale(LC_MESSAGES, ""); 11 #endif 12 pg_perm_setlocale(LC_MONETARY, "C"); 13 pg_perm_setlocale(LC_NUMERIC, "C"); 14 pg_perm_setlocale(LC_TIME, "C"); 15 /* remove any LC_ALL setting */ 16 unsetenv("LC_ALL"); 17 if (argc > 1){ 18 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0){ 19 help(progname); 20 exit(0); 21 } 22 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0){ 23 puts("postgres (PostgreSQL) " PG_VERSION); 24 exit(0); 25 } 26 } 27 /* Make sure we are not running as root. */ 28 check_root(progname); 29 /* Dispatch to one of various subprograms depending on first argument. */ 30 #ifdef EXEC_BACKEND 31 if (argc > 1 && strncmp(argv[1], "--fork", 6) == 0) 32 exit(SubPostmasterMain(argc, argv)); 33 #endif 34 if (argc > 1 && strcmp(argv[1], "--boot") == 0) 35 AuxiliaryProcessMain(argc, argv); /* does not return */ 36 if (argc > 1 && strcmp(argv[1], "--describe-config") == 0) 37 exit(GucInfoMain()); 38 if (argc > 1 && strcmp(argv[1], "--single") == 0) 39 exit(PostgresMain(argc, argv, get_current_username(progname))); 40 exit(PostmasterMain(argc, argv)); 41 }

指定EXEC_BACKEND,命令列指定--fork,執行SubPostmasterMain(argc. argv)

命令列指定--boot,執行AuxiliaryProcessMain(argc, argv),後臺Postgres程序建立資料庫集簇入口

命令列指定--describe-config,執行GucInfoMain()

命令列指定--single,執行PostgresMain(argc, argv, get_current_username(progname)),單使用者環境初始化

執行PostmasterMain(argc, argv),伺服器守護程序Postmaster