@@ -9768,8 +9768,8 @@ XLogFileNameP(TimeLineID tli, XLogSegNo segno)
97689768 */
97699769XLogRecPtr
97709770do_pg_start_backup (const char * backupidstr , bool fast , TimeLineID * starttli_p ,
9771- char * * labelfile , DIR * tblspcdir , List * * tablespaces ,
9772- char * * tblspcmapfile , bool infotbssize ,
9771+ StringInfo labelfile , DIR * tblspcdir , List * * tablespaces ,
9772+ StringInfo tblspcmapfile , bool infotbssize ,
97739773 bool needtblspcmapfile )
97749774{
97759775 bool exclusive = (labelfile == NULL );
@@ -9783,8 +9783,6 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
97839783 XLogSegNo _logSegNo ;
97849784 struct stat stat_buf ;
97859785 FILE * fp ;
9786- StringInfoData labelfbuf ;
9787- StringInfoData tblspc_mapfbuf ;
97889786
97899787 backup_started_in_recovery = RecoveryInProgress ();
97909788
@@ -9981,7 +9979,8 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
99819979 /*
99829980 * Construct tablespace_map file
99839981 */
9984- initStringInfo (& tblspc_mapfbuf );
9982+ if (exclusive )
9983+ tblspcmapfile = makeStringInfo ();
99859984
99869985 datadirpathlen = strlen (DataDir );
99879986
@@ -10054,7 +10053,7 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1005410053 if (tablespaces )
1005510054 * tablespaces = lappend (* tablespaces , ti );
1005610055
10057- appendStringInfo (& tblspc_mapfbuf , "%s %s\n" , ti -> oid , ti -> path );
10056+ appendStringInfo (tblspcmapfile , "%s %s\n" , ti -> oid , ti -> path );
1005810057
1005910058 pfree (buflinkpath .data );
1006010059#else
@@ -10073,23 +10072,24 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1007310072 /*
1007410073 * Construct backup label file
1007510074 */
10076- initStringInfo (& labelfbuf );
10075+ if (exclusive )
10076+ labelfile = makeStringInfo ();
1007710077
1007810078 /* Use the log timezone here, not the session timezone */
1007910079 stamp_time = (pg_time_t ) time (NULL );
1008010080 pg_strftime (strfbuf , sizeof (strfbuf ),
1008110081 "%Y-%m-%d %H:%M:%S %Z" ,
1008210082 pg_localtime (& stamp_time , log_timezone ));
10083- appendStringInfo (& labelfbuf , "START WAL LOCATION: %X/%X (file %s)\n" ,
10083+ appendStringInfo (labelfile , "START WAL LOCATION: %X/%X (file %s)\n" ,
1008410084 (uint32 ) (startpoint >> 32 ), (uint32 ) startpoint , xlogfilename );
10085- appendStringInfo (& labelfbuf , "CHECKPOINT LOCATION: %X/%X\n" ,
10085+ appendStringInfo (labelfile , "CHECKPOINT LOCATION: %X/%X\n" ,
1008610086 (uint32 ) (checkpointloc >> 32 ), (uint32 ) checkpointloc );
10087- appendStringInfo (& labelfbuf , "BACKUP METHOD: %s\n" ,
10087+ appendStringInfo (labelfile , "BACKUP METHOD: %s\n" ,
1008810088 exclusive ? "pg_start_backup" : "streamed" );
10089- appendStringInfo (& labelfbuf , "BACKUP FROM: %s\n" ,
10089+ appendStringInfo (labelfile , "BACKUP FROM: %s\n" ,
1009010090 backup_started_in_recovery ? "standby" : "master" );
10091- appendStringInfo (& labelfbuf , "START TIME: %s\n" , strfbuf );
10092- appendStringInfo (& labelfbuf , "LABEL: %s\n" , backupidstr );
10091+ appendStringInfo (labelfile , "START TIME: %s\n" , strfbuf );
10092+ appendStringInfo (labelfile , "LABEL: %s\n" , backupidstr );
1009310093
1009410094 /*
1009510095 * Okay, write the file, or return its contents to caller.
@@ -10123,7 +10123,7 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1012310123 (errcode_for_file_access (),
1012410124 errmsg ("could not create file \"%s\": %m" ,
1012510125 BACKUP_LABEL_FILE )));
10126- if (fwrite (labelfbuf . data , labelfbuf . len , 1 , fp ) != 1 ||
10126+ if (fwrite (labelfile -> data , labelfile -> len , 1 , fp ) != 1 ||
1012710127 fflush (fp ) != 0 ||
1012810128 pg_fsync (fileno (fp )) != 0 ||
1012910129 ferror (fp ) ||
@@ -10132,10 +10132,12 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1013210132 (errcode_for_file_access (),
1013310133 errmsg ("could not write file \"%s\": %m" ,
1013410134 BACKUP_LABEL_FILE )));
10135- pfree (labelfbuf .data );
10135+ /* Allocated locally for exclusive backups, so free separately */
10136+ pfree (labelfile -> data );
10137+ pfree (labelfile );
1013610138
1013710139 /* Write backup tablespace_map file. */
10138- if (tblspc_mapfbuf . len > 0 )
10140+ if (tblspcmapfile -> len > 0 )
1013910141 {
1014010142 if (stat (TABLESPACE_MAP , & stat_buf ) != 0 )
1014110143 {
@@ -10159,7 +10161,7 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1015910161 (errcode_for_file_access (),
1016010162 errmsg ("could not create file \"%s\": %m" ,
1016110163 TABLESPACE_MAP )));
10162- if (fwrite (tblspc_mapfbuf . data , tblspc_mapfbuf . len , 1 , fp ) != 1 ||
10164+ if (fwrite (tblspcmapfile -> data , tblspcmapfile -> len , 1 , fp ) != 1 ||
1016310165 fflush (fp ) != 0 ||
1016410166 pg_fsync (fileno (fp )) != 0 ||
1016510167 ferror (fp ) ||
@@ -10170,13 +10172,9 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
1017010172 TABLESPACE_MAP )));
1017110173 }
1017210174
10173- pfree (tblspc_mapfbuf .data );
10174- }
10175- else
10176- {
10177- * labelfile = labelfbuf .data ;
10178- if (tblspc_mapfbuf .len > 0 )
10179- * tblspcmapfile = tblspc_mapfbuf .data ;
10175+ /* Allocated locally for exclusive backups, so free separately */
10176+ pfree (tblspcmapfile -> data );
10177+ pfree (tblspcmapfile );
1018010178 }
1018110179 }
1018210180 PG_END_ENSURE_ERROR_CLEANUP (pg_start_backup_callback , (Datum ) BoolGetDatum (exclusive ));
@@ -10283,7 +10281,16 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
1028310281 */
1028410282 WALInsertLockAcquireExclusive ();
1028510283 if (exclusive )
10284+ {
10285+ if (!XLogCtl -> Insert .exclusiveBackup )
10286+ {
10287+ WALInsertLockRelease ();
10288+ ereport (ERROR ,
10289+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
10290+ errmsg ("exclusive backup not in progress" )));
10291+ }
1028610292 XLogCtl -> Insert .exclusiveBackup = false;
10293+ }
1028710294 else
1028810295 {
1028910296 /*
0 commit comments