* Write out a new shared or local map file with the given contents. * * The magic number and CRC are automatically updated in *newmap. On * success, we copy the data to the appropriate permanent static variable. * * If write_wal is true then an appropriate WAL message is emitted. * (It will be false for bootstrap and WAL replay cases.) * * If send_sinval is true then a SI invalidation mes
| 806 | * map update could be happening. |
| 807 | */ |
| 808 | static void |
| 809 | write_relmap_file(bool shared, RelMapFile *newmap, |
| 810 | bool write_wal, bool send_sinval, bool preserve_files, |
| 811 | Oid dbid, Oid tsid, const char *dbpath) |
| 812 | { |
| 813 | int fd; |
| 814 | RelMapFile *realmap; |
| 815 | char mapfilename[MAXPGPATH]; |
| 816 | |
| 817 | /* |
| 818 | * Fill in the overhead fields and update CRC. |
| 819 | */ |
| 820 | newmap->magic = RELMAPPER_FILEMAGIC; |
| 821 | if (newmap->num_mappings < 0 || newmap->num_mappings > MAX_MAPPINGS) |
| 822 | elog(ERROR, "attempt to write bogus relation mapping"); |
| 823 | |
| 824 | INIT_CRC32C(newmap->crc); |
| 825 | COMP_CRC32C(newmap->crc, (char *) newmap, offsetof(RelMapFile, crc)); |
| 826 | FIN_CRC32C(newmap->crc); |
| 827 | |
| 828 | /* |
| 829 | * Open the target file. We prefer to do this before entering the |
| 830 | * critical section, so that an open() failure need not force PANIC. |
| 831 | */ |
| 832 | if (shared) |
| 833 | { |
| 834 | snprintf(mapfilename, sizeof(mapfilename), "global/%s", |
| 835 | RELMAPPER_FILENAME); |
| 836 | realmap = &shared_map; |
| 837 | } |
| 838 | else |
| 839 | { |
| 840 | snprintf(mapfilename, sizeof(mapfilename), "%s/%s", |
| 841 | dbpath, RELMAPPER_FILENAME); |
| 842 | realmap = &local_map; |
| 843 | } |
| 844 | |
| 845 | fd = OpenTransientFile(mapfilename, O_WRONLY | O_CREAT | PG_BINARY); |
| 846 | if (fd < 0) |
| 847 | ereport(ERROR, |
| 848 | (errcode_for_file_access(), |
| 849 | errmsg("could not open file \"%s\": %m", |
| 850 | mapfilename))); |
| 851 | |
| 852 | if (write_wal) |
| 853 | { |
| 854 | xl_relmap_update xlrec; |
| 855 | XLogRecPtr lsn; |
| 856 | |
| 857 | /* now errors are fatal ... */ |
| 858 | START_CRIT_SECTION(); |
| 859 | |
| 860 | xlrec.dbid = dbid; |
| 861 | xlrec.tsid = tsid; |
| 862 | xlrec.nbytes = sizeof(RelMapFile); |
| 863 | |
| 864 | XLogBeginInsert(); |
| 865 | XLogRegisterData((char *) (&xlrec), MinSizeOfRelmapUpdate); |
no test coverage detected