TL;DR: Don't ever set fsync=off, don't kill -9 the postmaster then deletepostmaster.pid, don't run PostgreSQL on network file systems.

Reports of database corruption on the PostgreSQL mailing list are uncommon, but hardly rare. While a few data corruption issues have been found in PostgreSQL over the years, the vast majority of issues are caused by:

  • Administrator action;
  • Misconfiguration; or
  • Bad hardware

recent mailing list post asked what can be done to reduce the chance of corruption and keep data safe.


If you think you have a corrupt PostgreSQL database, stop the database server and take a complete copy of the data directory now. See the Corruption page on the wiki. Then ask for help on the pgsql-general mailing list, or for critical/urgent issues contact a profesional support provider.

Do not attempt to fix the problem before taking a complete copy of the entire data directory. You might make the problem much worse, turning a recoverable problem into unrecoverable data loss.


Here's my advice for avoiding DB corruption issues, with some general PostgreSQL administration advice thrown in for good measure:

Read the manual

Read the reliability section of the manual in detail.

Many of the issues people on the mailing lists ask about could be avoided if they'd only read the manual. The PostgreSQL manual is well written, comprehensive, and will teach you about SQL and databases in general, not just PostgreSQL. Reading it cover-to-cover helped me learn more about database systems than any one other thing I've ever done. Read it.

PostgreSQL is easy enough to use, so you don't have to read the manual, but you'll get a lot more out of the system if you do. You'll be able to tune it for better performance, know how to avoid a few pitfalls, and know how to use more of its features. You'd know to avoid usingSIGKILL (kill -9) to terminate the postmaster, for example.

Updates

Bug fixes are no good if you don't apply them. Keep up to date with the latest PostgreSQL patch releases. Don't be one of those people still running 9.0.0 when 9.0.10 is out. The team releases updates for a reason. Minor point releases require minimal downtime as there's no need to dump and reload; just stop the DB, install the new binaries, and start the DB. Even this downtime can be avoided with failover and failback using something like repmgr.

As per the versioning policy they're extremely conservative about what gets back-patched into released major versions so upgrades are very safe. Read the versioning policy. Just read the release notes for each version in between yours and the new version, then install it.

For more on this topic read the FAQ entry A bug I'm encountering is fixed in a newer minor release of PostgreSQL, but I don't want to upgrade. Can I get a patch for just this issue?

Keep an eye on the end of life dates. Plan to upgrade to a new major version well before your version goes EOL.

Administrator action

Don't kill -9 (SIGKILL) any PostgreSQL processes. It should be fine, but it's still not smart. kill -9 should be your last resort; first use pg_cancel_backend thenpg_terminate_backend (which is effectively a kill -15). If you kill -9 a backend the whole DB will terminate and restart, breaking all connections and forcing crash recovery.

Never delete postmaster.pid.

(Corruption should not actually occur unless you kill -9 the postmaster but leave otherpostgres processes running, then delete postmaster.pid and start a new postmaster. If you insist on unlocking the gun safe, loading the shotgun, aiming it at your foot and pulling the trigger we can't stop you. Don't do that.)

Don't delete anything in the data directory, with the sole exception of the contents ofpg_log if your logs are within your data directory. Everything else is vital and should never be messed with directly by the administrator. Do not delete anything from pg_clog orpg_xlog, these are vital components of the database system.

Do not run your database with fsync = off if it contains any data you care about. Turning off the fsync configuration parameter effectively gives PostgreSQL permission to avoid slow syncs at the risk of severe and unrecoverable data corruption if the server crashes or loses power. If it's possible to recover such a database at all it'll take many hours of time from a serious (and expensive) expert. These days there's rarely a reason to turn fsync = offanyway; you can achieve many of the same benefits without the corruption risks usingasynchronous commit and a commit delay.

The system catalogs (pg_catalog.*) are there for looking, not for touching. Do not everINSERTUPDATE or DELETE in the system catalog tables unless you are absolutelycertain you know what you are doing. Only a superuser or (for some catalogs) the database owner can mess directly with the catalogs, and you shouldn't be running as either day-to-day.

If you need pg_resetxlog then you already have a corruption problem. pg_resetxlog is a last resort for getting a damaged database going again, not a semi-routine operation like MySQL's myisamchk. If you have to pg_resetxlog a DB, you need to work out what went wrong and why, then dump the DB(s), re-initdb, and restore the dumps. Don't just keep on using the damaged database.

Backups, replication and PITR

The backups section of the manual is good. Read it. You should also look at the Barmantool, which helps automate many of these tasks.

Maintain rolling backup dumps with proper ageing. For example, keep one a day for the last 7 days, then one a week for the last 4 weeks, then one a month for the rest of the year, then one a year. Backups won't stop corruption or damage happening, but they'll help you recover with a minimum of pain. In my view good old pg_dump is the tool of choice here; I tend to produce one -Fc dump per database and a pg_dumpall --globals-only for users/roles/etc.

Use warm standby with log shipping and/or replication to maintain a live copy of the DB, so you don't have that painful window between your last backup and when the failure occurred. Do not rely solely on replication, take dumps too.

If you want point-in-time recovery, keep a few days or weeks worth of WAL archives and a basebackup around. That'll help you recover from those "oops I meant DROP TABLE unimportant; not DROP TABLE vital_financial_records;" issues with a minimum of data loss.

Barman can help with warm standby and PITR.

If taking filesystem-level backups of the data directory, you don't have to stop the DB if you use pg_basebackup, or use pg_start_backup and pg_stop_backup correctly. Seethe manual. You do have to include the whole data directory. Do not omit folders you think are unimportant from the backup. People who left pg_xlog or pg_clog out of their backup as "unimportant" periodically turn up asking for help on the mailing list. Don't be one of them.

Remember to test your backups, don't just assume they're fine.

Hardware

Plug-pull test your system when you're testing it before going live. Put it under load with something like pgbench, then literally pull the power plug out so your server loses power and shuts down uncleanly. If your database doesn't come back up fine you have hardware, OS or configuration problems. Do this repeatedly while under load, until you're confident it'll come up reliably.

Use good quality hardware with proper cooling and a good quality power supply. If possible, ECC RAM is a nice extra.

Never, ever, ever use cheap SSDs. Use good quality hard drives or (after proper testing) high end SSDs. Read the SSD reviews periodically posted on this mailing list if considering using SSDs. Make sure the SSD has power-loss protection for its write cache - a supercapacitor/ultracapacitor, low-power capacitor array, secondary battery, or other write-cache protection. Always do repeated plug-pull testing when using SSDs, don't just trust the specifications.

Avoid RAID 5, mostly because the performance is terrible, but also because I've seen corruption issues with rebuilds from parity on failing disks. If you're using spinning disks RAID 10 is a better option for a database.

If you have a hardware RAID controller with a battery backed cache (BBU), test the BBU regularly. A battery backup that doesn't work is no good, and a RAID controller in write-back caching mode without a working BBU is guaranteed to severely corrupt your data if the system loses power.

Use a good quality hardware RAID controller with a battery backup cache unit if you're using spinning disks in RAID. This is as much for performance as reliability; a BBU will make an immense difference to database performance. Be aware that if your RAID controller fails you must be able to get a hold of a compatible one before you can read your array. If you don't add a battery backed cache you won't gain much over Linux's md software RAID, which has the advantage that arrays are portable from machine to machine. Do not use cheap on-motherboard/in-BIOS "RAID" systems, they combine the worst features of hardware and software RAID.

If you're going to have a UPS (you shouldn't need one as your system should be crash-safe), don't waste your money on a cheap one. Get a good online double-conversion unit that does proper power filtering. Cheap UPSs are just a battery with a fast switch, they provide no power filtering and what little surge protection they offer is done with a component that wears out over time and load, becoming totally ineffective. Since your system should be crash-safe a cheap UPS will do nothing for corruption protection, it'll only help with uptime.

I shouldn't have to say this, but ... don't run your database off a USB flash drive ("USB memory key" or "USB stick").

Software and OS

Use a solid, reliable file system. zfs-on-linux, btrfs, etc are not the right choices for a database you care about. Never, ever, ever use FAT32.

If on Windows, do not run an anti-virus program on your
database server. Nobody should be using it for other things or running programs on it anyway. Antivirus software intercepts system calls in all sorts of fun and interesting ways. Many of these are subject to exciting race conditions and bugs when software like PostgreSQL has several processes all reading from and writing to the same files. Antivirus software may also try to "clean" the database files, causing severe corruption. If you absolutely must run antivirus software, add exclusions for the PostgreSQL data directory and PostgreSQL install directory. If your AV software supports process exclusions, add an exclusion for all programs in the PostgreSQL bin directory, especially postgres.exe.

Make sure your OS is disabling your hard drive's write cache or properly flushing it. These days this is a pretty safe bet unless you're using a cheap SSD that ignores flush requests. Plug-pull testing should catch write-cache issues, as should the tools mentioned in the reliability documentation linked to above.

You may also wish to consider reading these wiki articles:

参考:

http://blog.ringerc.id.au/2012/10/avoiding-postgresql-database-corruption.html

Avoiding PostgreSQL database corruption的更多相关文章

  1. How to drop a PostgreSQL database if there are active connections to it?

    1.PostgreSQL 9.1 and below: SELECT pg_terminate_backend(pg_stat_activity.procpid) FROM pg_stat_activ ...

  2. Database Corruption ->> Fix Database In Suspect State

    昨天在工作中遇到一个情况,就是Development环境中的某台服务器上的某个数据库进入了Suspect状态.以前看书倒是知道说这个状态,不过实际工作当中从来没有遇到过.那么一些背景情况是这样的. 环 ...

  3. How to change owner of PostgreSql database?

    ALTER DATABASE name OWNER TO new_owner;

  4. 安装PostgreSQL数据库 ,Database Cluster 失败!

    在安装PG数据库的过程中,会选择安装目录以及数据存放目录和端口,并需要选择Local,如果全部使用默认,并且设置好自己的密码后开始安装,前期进展还比较顺利,到了安装Database Cluster时, ...

  5. How to speed up insertion performance in PostgreSQL

    Disable any triggers on the table Drop indexes before starting the import, re-create them afterwards ...

  6. DataBase异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间

    一,RECOVERY PENDING状态 今天修改了SQL Server的Service Account的密码,然后重启SQL Server的Service,发现有db处于Recovery Pendi ...

  7. CentOS 6安装PostgreSQL

    https://zh.wikipedia.org/wiki/PostgreSQL PostgreSQL是自由的对象-关系型数据库服务器(数据库管理系统),在灵活的BSD-风格许可证下发行.它在其他开放 ...

  8. kali linux 系列教程之metasploit 连接postgresql可能遇见的问题

    kali linux 系列教程之metasploit 连接postgresql可能遇见的问题 文/玄魂   目录 kali linux 下metasploit 连接postgresql可能遇见的问题. ...

  9. PostgreSQL Performance Monitoring Tools

    PostgreSQL Performance Monitoring Tools https://github.com/CloudServer/postgresql-perf-tools This pa ...

随机推荐

  1. kali linux karmetasploit配置

    原理分析:http://www.freebuf.com/articles/77055.html 转官方说明:https://www.offensive-security.com/metasploit- ...

  2. linux 任务调度 系统任务调度

    linux  at 针对运行一次的任务 crontab   控制计划任务的命令 crond系统服务 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程, 与windows ...

  3. ie7下 滚动条内容不动问题

    ie7+ 版式正常 ie7滚动内容不跟着动 解决方法 加上 overflow-x: hidden;    overflow-y: auto;    *position:relative;    *le ...

  4. HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)

    题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...

  5. HDU 5025

    http://acm.hdu.edu.cn/showproblem.php?pid=5025 蛇最多只有五条,状态压缩一下,vis增加两维,表示取得钥匙的状态和蛇的状态,然后一个优先队列的bfs即可解 ...

  6. NSIS

    NSIS 是“Nullsoft 脚本安装系统”(Nullsoft Scriptable Installation System) 的缩写,它是一个免费的 Win32 安装.卸载系统,采用了简洁高效的脚 ...

  7. 【转发】构建高可伸缩性的WEB交互式系统(上)

    原文转自:http://kb.cnblogs.com/page/503460/ 可伸缩性是一种对软件系统处理能力的设计指标,高可伸缩性代表一种弹性,在系统扩展过程中,能够保证旺盛的生命力,通过很少的改 ...

  8. You must install 'hg' on your build machine

    /***************************************************************************************** * You mus ...

  9. LeetCode Set Matrix Zeroes(技巧+逻辑)

    题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...

  10. selenium Gird

    selenium-server selenium Gird testcase-----------------hub -------------------node1 ---------------- ...