2016-03-05 20:10:19 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* controldata_utils.c
|
|
|
|
* Common code for control data file output.
|
|
|
|
*
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2016-03-05 20:10:19 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/common/controldata_utils.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FRONTEND
|
|
|
|
#include "postgres.h"
|
|
|
|
#else
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2021-11-29 05:36:13 +01:00
|
|
|
#include <time.h>
|
2016-03-05 20:10:19 +01:00
|
|
|
|
2019-03-12 02:03:33 +01:00
|
|
|
#include "access/xlog_internal.h"
|
2016-03-05 20:10:19 +01:00
|
|
|
#include "catalog/pg_control.h"
|
|
|
|
#include "common/controldata_utils.h"
|
2019-03-12 02:03:33 +01:00
|
|
|
#include "common/file_perm.h"
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
#ifdef FRONTEND
|
2019-05-14 20:19:49 +02:00
|
|
|
#include "common/logging.h"
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
#endif
|
2016-03-05 20:10:19 +01:00
|
|
|
#include "port/pg_crc32c.h"
|
2019-03-18 04:59:35 +01:00
|
|
|
|
2019-02-28 21:57:40 +01:00
|
|
|
#ifndef FRONTEND
|
2019-03-18 04:59:35 +01:00
|
|
|
#include "pgstat.h"
|
2019-02-28 21:57:40 +01:00
|
|
|
#include "storage/fd.h"
|
|
|
|
#endif
|
2016-03-05 20:10:19 +01:00
|
|
|
|
|
|
|
/*
|
2019-02-28 21:57:40 +01:00
|
|
|
* get_controlfile()
|
2016-03-05 20:10:19 +01:00
|
|
|
*
|
2016-09-28 18:00:00 +02:00
|
|
|
* Get controlfile values. The result is returned as a palloc'd copy of the
|
|
|
|
* control file data.
|
2016-07-26 17:23:43 +02:00
|
|
|
*
|
2016-09-28 18:00:00 +02:00
|
|
|
* crc_ok_p can be used by the caller to see whether the CRC of the control
|
|
|
|
* file data is correct.
|
2016-03-05 20:10:19 +01:00
|
|
|
*/
|
|
|
|
ControlFileData *
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
get_controlfile(const char *DataDir, bool *crc_ok_p)
|
2016-03-05 20:10:19 +01:00
|
|
|
{
|
|
|
|
ControlFileData *ControlFile;
|
|
|
|
int fd;
|
|
|
|
char ControlFilePath[MAXPGPATH];
|
|
|
|
pg_crc32c crc;
|
2018-05-18 17:52:18 +02:00
|
|
|
int r;
|
2016-03-05 20:10:19 +01:00
|
|
|
|
2016-09-28 18:00:00 +02:00
|
|
|
AssertArg(crc_ok_p);
|
|
|
|
|
2022-09-12 08:31:56 +02:00
|
|
|
ControlFile = palloc_object(ControlFileData);
|
2016-03-05 20:10:19 +01:00
|
|
|
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
|
|
|
|
|
2016-03-08 00:14:20 +01:00
|
|
|
#ifndef FRONTEND
|
2019-02-28 21:57:40 +01:00
|
|
|
if ((fd = OpenTransientFile(ControlFilePath, O_RDONLY | PG_BINARY)) == -1)
|
2016-03-08 00:14:20 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open file \"%s\" for reading: %m",
|
|
|
|
ControlFilePath)));
|
|
|
|
#else
|
2019-02-28 21:57:40 +01:00
|
|
|
if ((fd = open(ControlFilePath, O_RDONLY | PG_BINARY, 0)) == -1)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not open file \"%s\" for reading: %m",
|
|
|
|
ControlFilePath);
|
2016-03-08 00:14:20 +01:00
|
|
|
#endif
|
2016-03-05 20:10:19 +01:00
|
|
|
|
2018-05-18 17:52:18 +02:00
|
|
|
r = read(fd, ControlFile, sizeof(ControlFileData));
|
|
|
|
if (r != sizeof(ControlFileData))
|
|
|
|
{
|
|
|
|
if (r < 0)
|
2016-03-08 00:14:20 +01:00
|
|
|
#ifndef FRONTEND
|
2018-05-18 17:52:18 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m", ControlFilePath)));
|
2016-03-08 00:14:20 +01:00
|
|
|
#else
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not read file \"%s\": %m", ControlFilePath);
|
2016-03-08 00:14:20 +01:00
|
|
|
#endif
|
2018-05-18 17:52:18 +02:00
|
|
|
else
|
|
|
|
#ifndef FRONTEND
|
|
|
|
ereport(ERROR,
|
2018-07-23 02:37:36 +02:00
|
|
|
(errcode(ERRCODE_DATA_CORRUPTED),
|
|
|
|
errmsg("could not read file \"%s\": read %d of %zu",
|
Rework error messages around file handling
Some error messages related to file handling are using the code path
context to define their state. For example, 2PC-related errors are
referring to "two-phase status files", or "relation mapping file" is
used for catalog-to-filenode mapping, however those prove to be
difficult to translate, and are not more helpful than just referring to
the path of the file being worked on. So simplify all those error
messages by just referring to files with their path used. In some
cases, like the manipulation of WAL segments, the context is actually
helpful so those are kept.
Calls to the system function read() have also been rather inconsistent
with their error handling sometimes not reporting the number of bytes
read, and some other code paths trying to use an errno which has not
been set. The in-core functions are using a more consistent pattern
with this patch, which checks for both errno if set or if an
inconsistent read is happening.
So as to care about pluralization when reading an unexpected number of
byte(s), "could not read: read %d of %zu" is used as error message, with
%d field being the output result of read() and %zu the expected size.
This simplifies the work of translators with less variations of the same
message.
Author: Michael Paquier
Reviewed-by: Álvaro Herrera
Discussion: https://postgr.es/m/20180520000522.GB1603@paquier.xyz
2018-07-18 01:01:23 +02:00
|
|
|
ControlFilePath, r, sizeof(ControlFileData))));
|
2018-05-18 17:52:18 +02:00
|
|
|
#else
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not read file \"%s\": read %d of %zu",
|
|
|
|
ControlFilePath, r, sizeof(ControlFileData));
|
2018-05-18 17:52:18 +02:00
|
|
|
#endif
|
|
|
|
}
|
2016-03-05 20:10:19 +01:00
|
|
|
|
2019-02-28 21:57:40 +01:00
|
|
|
#ifndef FRONTEND
|
2019-07-06 23:18:46 +02:00
|
|
|
if (CloseTransientFile(fd) != 0)
|
Tighten use of OpenTransientFile and CloseTransientFile
This fixes two sets of issues related to the use of transient files in
the backend:
1) OpenTransientFile() has been used in some code paths with read-write
flags while read-only is sufficient, so switch those calls to be
read-only where necessary. These have been reported by Joe Conway.
2) When opening transient files, it is up to the caller to close the
file descriptors opened. In error code paths, CloseTransientFile() gets
called to clean up things before issuing an error. However in normal
exit paths, a lot of callers of CloseTransientFile() never actually
reported errors, which could leave a file descriptor open without
knowing about it. This is an issue I complained about a couple of
times, but never had the courage to write and submit a patch, so here we
go.
Note that one frontend code path is impacted by this commit so as an
error is issued when fetching control file data, making backend and
frontend to be treated consistently.
Reported-by: Joe Conway, Michael Paquier
Author: Michael Paquier
Reviewed-by: Álvaro Herrera, Georgios Kokolatos, Joe Conway
Discussion: https://postgr.es/m/20190301023338.GD1348@paquier.xyz
Discussion: https://postgr.es/m/c49b69ec-e2f7-ff33-4f17-0eaa4f2cef27@joeconway.com
2019-03-09 00:50:55 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close file \"%s\": %m",
|
|
|
|
ControlFilePath)));
|
2019-02-28 21:57:40 +01:00
|
|
|
#else
|
2019-07-06 23:18:46 +02:00
|
|
|
if (close(fd) != 0)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not close file \"%s\": %m", ControlFilePath);
|
2019-02-28 21:57:40 +01:00
|
|
|
#endif
|
2016-03-05 20:10:19 +01:00
|
|
|
|
|
|
|
/* Check the CRC. */
|
|
|
|
INIT_CRC32C(crc);
|
|
|
|
COMP_CRC32C(crc,
|
|
|
|
(char *) ControlFile,
|
|
|
|
offsetof(ControlFileData, crc));
|
|
|
|
FIN_CRC32C(crc);
|
|
|
|
|
2016-09-28 18:00:00 +02:00
|
|
|
*crc_ok_p = EQ_CRC32C(crc, ControlFile->crc);
|
2016-03-05 20:10:19 +01:00
|
|
|
|
|
|
|
/* Make sure the control file is valid byte order. */
|
|
|
|
if (ControlFile->pg_control_version % 65536 == 0 &&
|
|
|
|
ControlFile->pg_control_version / 65536 != 0)
|
|
|
|
#ifndef FRONTEND
|
|
|
|
elog(ERROR, _("byte ordering mismatch"));
|
|
|
|
#else
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
pg_log_warning("possible byte ordering mismatch\n"
|
|
|
|
"The byte ordering used to store the pg_control file might not match the one\n"
|
|
|
|
"used by this program. In that case the results below would be incorrect, and\n"
|
|
|
|
"the PostgreSQL installation would be incompatible with this data directory.");
|
2016-03-05 20:10:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return ControlFile;
|
|
|
|
}
|
2019-03-12 02:03:33 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* update_controlfile()
|
|
|
|
*
|
|
|
|
* Update controlfile values with the contents given by caller. The
|
2019-03-18 04:59:35 +01:00
|
|
|
* contents to write are included in "ControlFile". "do_sync" can be
|
|
|
|
* optionally used to flush the updated control file. Note that it is up
|
|
|
|
* to the caller to properly lock ControlFileLock when calling this
|
|
|
|
* routine in the backend.
|
2019-03-12 02:03:33 +01:00
|
|
|
*/
|
|
|
|
void
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
update_controlfile(const char *DataDir,
|
2019-03-18 04:59:35 +01:00
|
|
|
ControlFileData *ControlFile, bool do_sync)
|
2019-03-12 02:03:33 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char buffer[PG_CONTROL_FILE_SIZE];
|
|
|
|
char ControlFilePath[MAXPGPATH];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Apply the same static assertions as in backend's WriteControlFile().
|
|
|
|
*/
|
|
|
|
StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_MAX_SAFE_SIZE,
|
|
|
|
"pg_control is too large for atomic disk writes");
|
|
|
|
StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE,
|
|
|
|
"sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE");
|
|
|
|
|
2021-11-29 05:36:13 +01:00
|
|
|
/* Update timestamp */
|
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
|
|
|
|
2019-03-12 02:03:33 +01:00
|
|
|
/* Recalculate CRC of control file */
|
|
|
|
INIT_CRC32C(ControlFile->crc);
|
|
|
|
COMP_CRC32C(ControlFile->crc,
|
|
|
|
(char *) ControlFile,
|
|
|
|
offsetof(ControlFileData, crc));
|
|
|
|
FIN_CRC32C(ControlFile->crc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out PG_CONTROL_FILE_SIZE bytes into pg_control by zero-padding
|
|
|
|
* the excess over sizeof(ControlFileData), to avoid premature EOF related
|
|
|
|
* errors when reading it.
|
|
|
|
*/
|
|
|
|
memset(buffer, 0, PG_CONTROL_FILE_SIZE);
|
|
|
|
memcpy(buffer, ControlFile, sizeof(ControlFileData));
|
|
|
|
|
|
|
|
snprintf(ControlFilePath, sizeof(ControlFilePath), "%s/%s", DataDir, XLOG_CONTROL_FILE);
|
|
|
|
|
|
|
|
#ifndef FRONTEND
|
2019-03-18 04:59:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* All errors issue a PANIC, so no need to use OpenTransientFile() and to
|
|
|
|
* worry about file descriptor leaks.
|
|
|
|
*/
|
|
|
|
if ((fd = BasicOpenFile(ControlFilePath, O_RDWR | PG_BINARY)) < 0)
|
2019-03-12 02:03:33 +01:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open file \"%s\": %m",
|
|
|
|
ControlFilePath)));
|
|
|
|
#else
|
|
|
|
if ((fd = open(ControlFilePath, O_WRONLY | PG_BINARY,
|
|
|
|
pg_file_create_mode)) == -1)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not open file \"%s\": %m", ControlFilePath);
|
2019-03-12 02:03:33 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
errno = 0;
|
2019-03-18 04:59:35 +01:00
|
|
|
#ifndef FRONTEND
|
|
|
|
pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_WRITE_UPDATE);
|
|
|
|
#endif
|
2019-03-12 02:03:33 +01:00
|
|
|
if (write(fd, buffer, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE)
|
|
|
|
{
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
if (errno == 0)
|
|
|
|
errno = ENOSPC;
|
|
|
|
|
|
|
|
#ifndef FRONTEND
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not write file \"%s\": %m",
|
|
|
|
ControlFilePath)));
|
|
|
|
#else
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not write file \"%s\": %m", ControlFilePath);
|
2019-03-12 02:03:33 +01:00
|
|
|
#endif
|
|
|
|
}
|
2019-03-18 04:59:35 +01:00
|
|
|
#ifndef FRONTEND
|
|
|
|
pgstat_report_wait_end();
|
|
|
|
#endif
|
2019-03-12 02:03:33 +01:00
|
|
|
|
2019-03-18 04:59:35 +01:00
|
|
|
if (do_sync)
|
|
|
|
{
|
|
|
|
#ifndef FRONTEND
|
|
|
|
pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_SYNC_UPDATE);
|
|
|
|
if (pg_fsync(fd) != 0)
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not fsync file \"%s\": %m",
|
|
|
|
ControlFilePath)));
|
|
|
|
pgstat_report_wait_end();
|
|
|
|
#else
|
|
|
|
if (fsync(fd) != 0)
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not fsync file \"%s\": %m", ControlFilePath);
|
2019-03-18 04:59:35 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-06 23:18:46 +02:00
|
|
|
if (close(fd) != 0)
|
2019-03-18 04:59:35 +01:00
|
|
|
{
|
2019-03-12 02:03:33 +01:00
|
|
|
#ifndef FRONTEND
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close file \"%s\": %m",
|
|
|
|
ControlFilePath)));
|
|
|
|
#else
|
2022-04-08 20:55:14 +02:00
|
|
|
pg_fatal("could not close file \"%s\": %m", ControlFilePath);
|
2019-03-12 02:03:33 +01:00
|
|
|
#endif
|
2019-03-18 04:59:35 +01:00
|
|
|
}
|
2019-03-12 02:03:33 +01:00
|
|
|
}
|