| 4742 | } |
| 4743 | |
| 4744 | static void |
| 4745 | WriteControlFile(void) |
| 4746 | { |
| 4747 | int fd; |
| 4748 | char buffer[PG_CONTROL_FILE_SIZE]; /* need not be aligned */ |
| 4749 | |
| 4750 | /* |
| 4751 | * Ensure that the size of the pg_control data structure is sane. See the |
| 4752 | * comments for these symbols in pg_control.h. |
| 4753 | */ |
| 4754 | StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_MAX_SAFE_SIZE, |
| 4755 | "pg_control is too large for atomic disk writes"); |
| 4756 | StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE, |
| 4757 | "sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE"); |
| 4758 | |
| 4759 | /* |
| 4760 | * Initialize version and compatibility-check fields |
| 4761 | */ |
| 4762 | ControlFile->pg_control_version = PG_CONTROL_VERSION; |
| 4763 | ControlFile->catalog_version_no = CATALOG_VERSION_NO; |
| 4764 | |
| 4765 | ControlFile->maxAlign = MAXIMUM_ALIGNOF; |
| 4766 | ControlFile->floatFormat = FLOATFORMAT_VALUE; |
| 4767 | |
| 4768 | ControlFile->blcksz = BLCKSZ; |
| 4769 | ControlFile->relseg_size = RELSEG_SIZE; |
| 4770 | ControlFile->xlog_blcksz = XLOG_BLCKSZ; |
| 4771 | ControlFile->xlog_seg_size = wal_segment_size; |
| 4772 | |
| 4773 | ControlFile->nameDataLen = NAMEDATALEN; |
| 4774 | ControlFile->indexMaxKeys = INDEX_MAX_KEYS; |
| 4775 | |
| 4776 | ControlFile->toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE; |
| 4777 | ControlFile->loblksize = LOBLKSIZE; |
| 4778 | |
| 4779 | ControlFile->float8ByVal = FLOAT8PASSBYVAL; |
| 4780 | |
| 4781 | /* Contents are protected with a CRC */ |
| 4782 | INIT_CRC32C(ControlFile->crc); |
| 4783 | COMP_CRC32C(ControlFile->crc, |
| 4784 | (char *) ControlFile, |
| 4785 | offsetof(ControlFileData, crc)); |
| 4786 | FIN_CRC32C(ControlFile->crc); |
| 4787 | |
| 4788 | /* |
| 4789 | * We write out PG_CONTROL_FILE_SIZE bytes into pg_control, zero-padding |
| 4790 | * the excess over sizeof(ControlFileData). This reduces the odds of |
| 4791 | * premature-EOF errors when reading pg_control. We'll still fail when we |
| 4792 | * check the contents of the file, but hopefully with a more specific |
| 4793 | * error than "couldn't read pg_control". |
| 4794 | */ |
| 4795 | memset(buffer, 0, PG_CONTROL_FILE_SIZE); |
| 4796 | memcpy(buffer, ControlFile, sizeof(ControlFileData)); |
| 4797 | |
| 4798 | fd = BasicOpenFile(XLOG_CONTROL_FILE, |
| 4799 | O_RDWR | O_CREAT | O_EXCL | PG_BINARY); |
| 4800 | if (fd < 0) |
| 4801 | ereport(PANIC, |
no test coverage detected