PostgreSQL Source Code: src/backend/access/transam/xlogarchive.c Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

16

17#include <sys/stat.h>

19#include <signal.h>

21

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53bool

55 const char *recovername, off_t expectedSize,

56 bool cleanupEnabled)

57{

59 char *xlogRestoreCmd;

60 char lastRestartPointFname[MAXPGPATH];

61 int rc;

62 struct stat stat_buf;

66

67

68

69

70

72 goto not_available;

73

74

76 goto not_available;

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

102

103

104

105

106 if (stat(xlogpath, &stat_buf) != 0)

107 {

108 if (errno != ENOENT)

111 errmsg("could not stat file \"%s\": %m",

112 xlogpath)));

113 }

114 else

115 {

116 if (unlink(xlogpath) != 0)

119 errmsg("could not remove file \"%s\": %m",

120 xlogpath)));

121 }

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140 if (cleanupEnabled)

141 {

144 XLogFileName(lastRestartPointFname, restartTli, restartSegNo,

146

147 Assert(strcmp(lastRestartPointFname, xlogfname) <= 0);

148 }

149 else

151

152

154 xlogpath, xlogfname,

155 lastRestartPointFname);

156

159 xlogRestoreCmd)));

160

161 fflush(NULL);

163

164

165

166

167

168

169

170

171

173

174

175

176

177 rc = system(xlogRestoreCmd);

178

180

182 pfree(xlogRestoreCmd);

183

184 if (rc == 0)

185 {

186

187

188

189

190 if (stat(xlogpath, &stat_buf) == 0)

191 {

192 if (expectedSize > 0 && stat_buf.st_size != expectedSize)

193 {

194 int elevel;

195

196

197

198

199

200

201

202

203

204

205

206

207

210 else

213 (errmsg("archive file \"%s\" has wrong size: %lld instead of %lld",

214 xlogfname,

215 (long long int) stat_buf.st_size,

216 (long long int) expectedSize)));

217 return false;

218 }

219 else

220 {

222 (errmsg("restored log file \"%s\" from archive",

223 xlogfname)));

224 strcpy(path, xlogpath);

225 return true;

226 }

227 }

228 else

229 {

230

231 int elevel = (errno == ENOENT) ? LOG : FATAL;

232

235 errmsg("could not stat file \"%s\": %m", xlogpath),

236 errdetail("\"restore_command\" returned a zero exit status, but stat() failed.")));

237 }

238 }

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

266

268 (errmsg("could not restore file \"%s\" from archive: %s",

270

271not_available:

272

273

274

275

276

277

278

279

281 return false;

282}

283

284

285

286

287

288

289

290

291

292

293

294void

296 bool failOnSignal, uint32 wait_event_info)

297{

298 char *xlogRecoveryCmd;

299 char lastRestartPointFname[MAXPGPATH];

300 int rc;

304

305 Assert(command && commandName);

306

307

308

309

310

311

314 XLogFileName(lastRestartPointFname, restartTli, restartSegNo,

316

317

318

319

321

323 (errmsg_internal("executing %s \"%s\"", commandName, command)));

324

325

326

327

328 fflush(NULL);

330 rc = system(xlogRecoveryCmd);

332

333 pfree(xlogRecoveryCmd);

334

335 if (rc != 0)

336 {

337

338

339

340

342

343

344

345

346 (errmsg("%s \"%s\": %s", commandName,

348 }

349}

350

351

352

353

354

355

356

357void

359{

361 bool reload = false;

362 struct stat statbuf;

363

365

367 {

369

370#ifdef WIN32

371 static unsigned int deletedcounter = 1;

372

373

374

375

376

377

378

379

380

381

382

385 if (rename(xlogfpath, oldpath) != 0)

386 {

389 errmsg("could not rename file \"%s\" to \"%s\": %m",

391 }

392#else

393

395#endif

396 if (unlink(oldpath) != 0)

399 errmsg("could not remove file \"%s\": %m",

401 reload = true;

402 }

403

405

406

407

408

409

412 else

414

415

416

417

418

419

420

421

422 if (reload)

424

425

426

427

428

429

431}

432

433

434

435

436

437

438

439

440

441

442

443void

445{

446 char archiveStatusPath[MAXPGPATH];

447 FILE *fd;

448

449

452 if (fd == NULL)

453 {

456 errmsg("could not create archive status file \"%s\": %m",

457 archiveStatusPath)));

458 return;

459 }

461 {

464 errmsg("could not write archive status file \"%s\": %m",

465 archiveStatusPath)));

466 return;

467 }

468

469

470

471

472

473

474

475

476

477

478

479

482

483

486}

487

488

489

490

491void

493{

495

497

500}

501

502

503

504

505

506

507

508

509void

511{

514 struct stat stat_buf;

515 FILE *fd;

516

517

519 if (stat(archiveDone, &stat_buf) == 0)

520 return;

521

522

524 if (stat(archiveReady, &stat_buf) == 0)

525 {

527 return;

528 }

529

530

532 if (fd == NULL)

533 {

536 errmsg("could not create archive status file \"%s\": %m",

537 archiveDone)));

538 return;

539 }

541 {

544 errmsg("could not write archive status file \"%s\": %m",

545 archiveDone)));

546 return;

547 }

548}

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564bool

566{

567 char archiveStatusPath[MAXPGPATH];

568 struct stat stat_buf;

569

570

572 return true;

573

574

575

576

577

580 return true;

581

582

583

584

585

586

587

588

590 if (stat(archiveStatusPath, &stat_buf) == 0)

591 return true;

592

593

595 if (stat(archiveStatusPath, &stat_buf) == 0)

596 return false;

597

598

600 if (stat(archiveStatusPath, &stat_buf) == 0)

601 return true;

602

603

605 return false;

606}

607

608

609

610

611

612

613

614

615

616

617

618bool

620{

621 char archiveStatusPath[MAXPGPATH];

622 struct stat stat_buf;

623

624

626 if (stat(archiveStatusPath, &stat_buf) == 0)

627 return false;

628

629

631 if (stat(archiveStatusPath, &stat_buf) == 0)

632 return true;

633

634

636 if (stat(archiveStatusPath, &stat_buf) == 0)

637 return false;

638

639

640

641

642

643

645 if (stat(archiveStatusPath, &stat_buf) != 0 &&

646 errno == ENOENT)

647 return false;

648

649 return true;

650}

651

652

653

654

655

656

657

658

659

660

661

662

663bool

665{

666 char archiveStatusPath[MAXPGPATH];

667 struct stat stat_buf;

668

669

671 if (stat(archiveStatusPath, &stat_buf) == 0)

672 return true;

673

674

676 if (stat(archiveStatusPath, &stat_buf) == 0)

677 return true;

678

679

681 if (stat(archiveStatusPath, &stat_buf) == 0)

682 return true;

683

684 return false;

685}

686

687

688

689

690

691

692

693bool

695{

696 char archiveStatusPath[MAXPGPATH];

697 struct stat stat_buf;

698

700 if (stat(archiveStatusPath, &stat_buf) == 0)

701 return true;

702

703 return false;

704}

705

706

707

708

709

710

711void

713{

714 char archiveStatusPath[MAXPGPATH];

715

716

718 unlink(archiveStatusPath);

719

720

721

723 unlink(archiveStatusPath);

724

725}

void PreRestoreCommand(void)

void PostRestoreCommand(void)

char * BuildRestoreCommand(const char *restoreCommand, const char *xlogpath, const char *xlogfname, const char *lastRestartPointFname)

int errmsg_internal(const char *fmt,...)

int errcode_for_file_access(void)

int errdetail(const char *fmt,...)

int errmsg(const char *fmt,...)

#define ereport(elevel,...)

int durable_rename(const char *oldfile, const char *newfile, int elevel)

FILE * AllocateFile(const char *name, const char *mode)

Assert(PointerIsAligned(start, uint64))

void pfree(void *pointer)

static char xlogfpath[MAXPGPATH]

char * replace_percent_placeholders(const char *instr, const char *param_name, const char *letters,...)

void PgArchForceDirScan(void)

size_t strlcpy(char *dst, const char *src, size_t siz)

static int fd(const char *x, int i)

char * wait_result_to_str(int exitstatus)

bool wait_result_is_signal(int exit_status, int signum)

bool wait_result_is_any_signal(int exit_status, bool include_command_not_found)

static void pgstat_report_wait_start(uint32 wait_event_info)

static void pgstat_report_wait_end(void)

void WalSndWakeup(bool physical, bool logical)

void WalSndRqstFileReload(void)

RecoveryState GetRecoveryState(void)

void GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)

#define XLogArchivingActive()

#define XLogArchivingAlways()

static bool IsTLHistoryFileName(const char *fname)

static void StatusFilePath(char *path, const char *xlog, const char *suffix)

#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

void XLogArchiveForceDone(const char *xlog)

bool XLogArchiveIsReadyOrDone(const char *xlog)

bool XLogArchiveIsBusy(const char *xlog)

bool XLogArchiveIsReady(const char *xlog)

void XLogArchiveNotifySeg(XLogSegNo segno, TimeLineID tli)

void ExecuteRecoveryCommand(const char *command, const char *commandName, bool failOnSignal, uint32 wait_event_info)

bool XLogArchiveCheckDone(const char *xlog)

bool RestoreArchivedFile(char *path, const char *xlogfname, const char *recovername, off_t expectedSize, bool cleanupEnabled)

void XLogArchiveNotify(const char *xlog)

void KeepFileRestoredFromArchive(const char *path, const char *xlogfname)

void XLogArchiveCleanup(const char *xlog)

bool ArchiveRecoveryRequested

char * recoveryRestoreCommand