PostgreSQL数据库集簇初始化——initdb初始化数据库(创建数据目录)
创建事务记录软链接symlink,如果xlog_dir不为空字符串,先清理xlog目录名,检查其绝对路径,检查指定xlog目录是否为空。check_data_dir(xlog_dir)如果返回0,说明xlog目录不存在,必须创建;如果返回1,说明xlog目录存在,但是为空,修改权限问题并使用;存在且不为空,调用exit_nicely函数退出。调用symlink函数创建软链接指向xlog_dir目录。
1 /* Create transaction log symlink, if required */ 2 if (strcmp(xlog_dir, "") != 0){ 3 char *linkloc; 4 /* clean up xlog directory name, check it's absolute */ 5 canonicalize_path(xlog_dir); 6 if (!is_absolute_path(xlog_dir)){ 7 fprintf(stderr, _("%s: transaction log directory location must be an absolute path\n"), progname); 8 exit_nicely(); 9 } 10 /* check if the specified xlog directory is empty */ 11 switch (check_data_dir(xlog_dir)){ 12 case 0: 13 /* xlog directory not there, must create it */ 14 printf(_("creating directory %s ... "), 15 xlog_dir); 16 fflush(stdout); 17 if (mkdir_p(xlog_dir, 0700) != 0){ 18 fprintf(stderr, _("%s: could not create directory \"%s\": %s\n"), progname, xlog_dir, strerror(errno)); 19 exit_nicely(); 20 }else 21 check_ok(); 22 made_new_xlogdir = true; 23 break; 24 case 1: 25 /* Present but empty, fix permissions and use it */ 26 printf(_("fixing permissions on existing directory %s ... "), xlog_dir); 27 fflush(stdout); 28 if (chmod(xlog_dir, 0700) != 0){ 29 fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),progname, xlog_dir, strerror(errno)); 30 exit_nicely(); 31 }else 32 check_ok(); 33 found_existing_xlogdir = true; 34 break; 35 case 2: 36 /* Present and not empty */ 37 fprintf(stderr,_("%s: directory \"%s\" exists but is not empty\n"),progname, xlog_dir); 38 fprintf(stderr, _("If you want to store the transaction log there, either\n""remove or empty the directory \"%s\".\n"),xlog_dir); 39 exit_nicely(); 40 default: 41 /* Trouble accessing directory */ 42 fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),progname, xlog_dir, strerror(errno)); 43 exit_nicely(); 44 } 45 /* form name of the place where the symlink must go */ 46 linkloc = (char *) pg_malloc(strlen(pg_data) + 8 + 1); 47 sprintf(linkloc, "%s/pg_xlog", pg_data); 48 49 #ifdef HAVE_SYMLINK 50 if (symlink(xlog_dir, linkloc) != 0){ 51 fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"),progname, linkloc, strerror(errno)); 52 exit_nicely(); 53 } 54 #else 55 fprintf(stderr, _("%s: symlinks are not supported on this platform")); 56 exit_nicely(); 57 #endif 58 }
#define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
创建数据目录,子目录如下:global、pg_xlog、pg_xlog/archive_status、pg_clog、pg_subtrans、pg_twophase、pg_multixact/members、pg_multixact/offsets、base、base/1、pg_stat_tmp。
1 static const char *subdirs[] = { 2 "global", 3 "pg_xlog", 4 "pg_xlog/archive_status", 5 "pg_clog", 6 "pg_subtrans", 7 "pg_twophase", 8 "pg_multixact/members", 9 "pg_multixact/offsets", 10 "base", 11 "base/1", 12 "pg_tblspc", 13 "pg_stat_tmp" 14 }; 15 16 /* Create required subdirectories */ 17 printf(_("creating subdirectories ... ")); 18 fflush(stdout); 19 for (i = 0; i < (sizeof(subdirs) / sizeof(char *)); i++){ 20 if (!mkdatadir(subdirs[i])) 21 exit_nicely(); 22 }