5

我查看了 postgres 文档,并给出了以下概要:

pg_resetxlog [-f] [-n] [-ooid ] [-x xid ] [-e xid_epoch ] [-m mxid ] [-O mxoff ] [-l timelineid,fileid,seg ] datadir

但是他们在文档中没有任何地方解释 datadir 是什么。是%postgres-path%/9.0/data还是可能%postgres-path%/9.0/data/pgxlog

另外,如果我想更改我的 xlog 目录,我可以简单地移动当前pg_xlog目录中的项目并运行命令以指向另一个目录吗?(假设我当前的 pg_xlog 目录在/data1/postgres/data/pg_xlog并且我希望日志转到的目录是/data2/pg_xlog:)

下面的命令能实现我刚才描述的吗?

mv /data1/postgres/data/pg_xlog /data2/pg_xlog
pg_resetxlog /data2
4

4 回答 4

6

pg_resetxlog is a tool of last resort for getting your database running again after:

  • You deleted files you shouldn't have from pg_xlog;

  • You restored a file system level backup that omitted the pg_xlog directory due to a backup system configuration mistake (this happens more than you'd think, people think "it has log in the name so it must be unimportant; I'll leave it out of the backups").

  • File-system corruption due to a hardware fault or hard drive failure damaged your data directory; or potentially even

  • a PostgreSQL bug or operating system bug damaged the write-ahead logs (exceedingly rare).

As the manual says:

pg_resetxlog clears the write-ahead log (WAL) [...]. This function is sometimes needed if these files have become corrupted. It should be used only as a last resort, when the server will not start due to such corruption.

Do not run pg_resetxlog unless you know exactly what you are doing and why. If you are unsure, ask on the pgsql-general mailing list or on https://dba.stackexchange.com/.

pg_resetxlog may corrupt your database, as the documentation warns. If you have to use it, you should REINDEX, dump your database(s), re-initdb, and reload your databases. Do not just continue using the damaged cluster. As per the documentation:

After running this command, it should be possible to start the server, but bear in mind that the database might contain inconsistent data due to partially-committed transactions. You should immediately dump your data, run initdb, and reload. After reload, check for inconsistencies and repair as needed.

If you simply want to move your write-ahead log directory to another location, you should:

  • Stop PostgreSQL
  • Move pg_xlog
  • Add a symbolic link from the old location to the new location
  • Start PostgreSQL

Or, as the documentation says:

It is advantageous if the log is located on a different disk from the main database files. This can be achieved by moving the pg_xlog directory to another location (while the server is shut down, of course) and creating a symbolic link from the original location in the main data directory to the new location.

If PostgreSQL fails to start, you've done something wrong. Do not use pg_resetxlog to "fix" it. Undo your changes and work out what you did wrong.

于 2012-10-17T05:44:57.330 回答
2

将 pg_xlog 目录的内容移动到所需位置,例如“/home/foo/pg_xlog”

mv pg_xlog/* /home/foo/pg_xlog

删除 pg_xlog 目录

rm -rf pg_xlog

创建 pg_xlog 的软链接

ln -s /home/foo/pg_xlog pg_xlog

验证链接

ls -lrt pg_xlog

注意:pg_resetxlog 不是移动 pg_xlog 的正确工具,请阅读

http://www.postgresql.org/docs/9.2/static/app-pgresetxlog.html

于 2013-10-03T10:35:35.207 回答
1

数据目录对应data_directorypostgresql.conf文件中的条目,或者PGDATA环境变量,也可以用SHOW data_directory语句在SQL中实时查询。它不指向pg_xlog目录,而是指向上一层。

要更改 WAL 文件的位置,必须关闭 PG 服务器,将pg_xlog目录及其内容移至新位置,应创建从旧位置到新位置的符号链接,然后重新启动服务器。pg_resetxlog不应该用于此,因为它可能会抑制最新的事务(此工具通常用于当所有其他方法都失败时的崩溃恢复情况)。

于 2012-10-15T15:44:11.387 回答
0

您永远不应该手动触摸 WAL 文件,这一点非常清楚。

如果目录中有悬空文件,即子文件夹中pg_xlog有以文件结尾的文件需要手动清理,可以通过sql命令完成.done*archive_status

CHECKPOINT;

这会强制执行一个事务检查点,其中包括清理 WAL 段文件。

请参阅9.3 的文档,但存在于所有当前版本的 Postgresql 中。

于 2014-02-11T20:58:44.870 回答