* load_relmap_file -- load data from the shared or local map file * * Because the map file is essential for access to core system catalogs, * failure to read it is a fatal error. * * Note that the local case requires DatabasePath to be set up. */
| 702 | * Note that the local case requires DatabasePath to be set up. |
| 703 | */ |
| 704 | static void |
| 705 | load_relmap_file(bool shared, bool lock_held) |
| 706 | { |
| 707 | RelMapFile *map; |
| 708 | char mapfilename[MAXPGPATH]; |
| 709 | pg_crc32c crc; |
| 710 | int fd; |
| 711 | int r; |
| 712 | |
| 713 | if (shared) |
| 714 | { |
| 715 | snprintf(mapfilename, sizeof(mapfilename), "global/%s", |
| 716 | RELMAPPER_FILENAME); |
| 717 | map = &shared_map; |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | snprintf(mapfilename, sizeof(mapfilename), "%s/%s", |
| 722 | DatabasePath, RELMAPPER_FILENAME); |
| 723 | map = &local_map; |
| 724 | } |
| 725 | |
| 726 | /* Read data ... */ |
| 727 | fd = OpenTransientFile(mapfilename, O_RDONLY | PG_BINARY); |
| 728 | if (fd < 0) |
| 729 | ereport(FATAL, |
| 730 | (errcode_for_file_access(), |
| 731 | errmsg("could not open file \"%s\": %m", |
| 732 | mapfilename))); |
| 733 | |
| 734 | /* |
| 735 | * Grab the lock to prevent the file from being updated while we read it, |
| 736 | * unless the caller is already holding the lock. If the file is updated |
| 737 | * shortly after we look, the sinval signaling mechanism will make us |
| 738 | * re-read it before we are able to access any relation that's affected by |
| 739 | * the change. |
| 740 | */ |
| 741 | if (!lock_held) |
| 742 | LWLockAcquire(RelationMappingLock, LW_SHARED); |
| 743 | |
| 744 | pgstat_report_wait_start(WAIT_EVENT_RELATION_MAP_READ); |
| 745 | r = read(fd, map, sizeof(RelMapFile)); |
| 746 | if (r != sizeof(RelMapFile)) |
| 747 | { |
| 748 | if (r < 0) |
| 749 | ereport(FATAL, |
| 750 | (errcode_for_file_access(), |
| 751 | errmsg("could not read file \"%s\": %m", mapfilename))); |
| 752 | else |
| 753 | ereport(FATAL, |
| 754 | (errcode(ERRCODE_DATA_CORRUPTED), |
| 755 | errmsg("could not read file \"%s\": read %d of %zu", |
| 756 | mapfilename, r, sizeof(RelMapFile)))); |
| 757 | } |
| 758 | pgstat_report_wait_end(); |
| 759 | |
| 760 | if (!lock_held) |
| 761 | LWLockRelease(RelationMappingLock); |
no test coverage detected