为什么80%的码农都做不了架构师?>>>   

同事做一个单表恢复工作,数据在1000多W,说是报了错误导不进去,环境与流程见下:

OS:CnetOS 5
DB:Postgres 9.2.4

恢复步骤:

1.导出语句
pg_dump -h xxxxx -p 5432 -U postgres -b -Fp  db_test -t t_kenyon -f /var/t_kenyon.bak2.导入语句
psql -h xxxx -d new_db -U postgres < /var/t_kenyon.bak3.报错信息,屏幕上一堆的诸如
invalid command \N
invalid command \N
invalid command \N
invalid command \N
invalid command \N
invalid command \N
........

分析处理:

因为是逻辑导出没有压缩定制的文件,故可以查看备份内容
[postgres@localhost ~]$ more t_kenyon.bak
--
-- PostgreSQL database dump
--SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;SET search_path = public, pg_catalog;SET default_tablespace = '';SET default_with_oids = false;--
-- Name: t_kenyon; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--CREATE TABLE t_kenyon (col1 integer DEFAULT nextval('t_kenyon_col1_seq'::regclass) NOT NULL,col2 ..col3 ..
);ALTER TABLE public.t_kenyon OWNER TO postgres;--
-- Name: COLUMN t_kenyon.col; Type: COMMENT; Schema: public; Owner: postgres
----
-- Data for Name: t_kenyon; Type: TABLE DATA; Schema: public; Owner: postgres
--COPY t_kenyon (col1,col2,col3....) FROM stdin;
3315866 \N      1       5.00    \N      \N      \N      2011-01-12 08:37:07+08  1       4130000 爱我中话        \N      708     Kenyon    HZ      雅安加油    HZ01    HELLO    \N      9       \N
3315934 \N      1       5.00    \N      \N      \N      2011-01-12 09:13:17+08  1       4130000 我哎中华        \N      708     kenyon    HZ      雅安加油
..........此处略去1W字

看起来报错的信息都是\N即空格的地方出错了,检查了一下postgres的日志,有几条信息发现很有意思

2013-04-23 00:16:23.149 PDT,"postgres","postgres",24738,"[local]",51763545.60a2,4,"CREATE TABLE",2013-04-23 00:16:21 PDT,2/331,1856,ERROR,42P01,"relation ""t_kenyon_col1_seq"" does not exist",,,,,,"CREATE TABLE t_kenyon (col1,col2...)..

这看起来是导数据之前的建表失败了,因为sequence不存在,后面的copy操作直接就报了N多的错误,尝试先建一下索引,再重新导入

postgres=#CREATE SEQUENCE t_kenyon_col1_seq  INCREMENT 1  MINVALUE 1  MAXVALUE 9223372036854775807  START 17354062  CACHE 1;
CREATE SEQUENCE
postgres=# \q
[postgres@localhost ~]$ psql -d postgres -U postgres < /var/t_kenyon.bak
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
COMMENT
ALTER TABLE
CREATE INDEX
CREATE INDEX
[postgres@localhost ~]$

OK,导入成功。但是有一个问题,为什么pg_dump导出的时候没有把sequence带出来呢?验证一下

[postgres@localhost ~]$ psql
psql (9.2.4)
Type "help" for help.postgres=# \d
No relations found.
postgres=# create table d_kenyon(id serial,vname varchar(30));
NOTICE:  CREATE TABLE will create implicit sequence "d_kenyon_id_seq" for serial column "d_kenyon.id"
CREATE TABLE
postgres=# insert into d_kenyon(vname) select generate_series(1,10)||'Hi,Kenyon!';
INSERT 0 10
postgres=# select * from d_kenyon;id |    vname
----+--------------1 | 1Hi,Kenyon!2 | 2Hi,Kenyon!3 | 3Hi,Kenyon!4 | 4Hi,Kenyon!5 | 5Hi,Kenyon!6 | 6Hi,Kenyon!7 | 7Hi,Kenyon!8 | 8Hi,Kenyon!9 | 9Hi,Kenyon!10 | 10Hi,Kenyon!
(10 rows)postgres=# \dList of relationsSchema |      Name       |   Type   |  Owner
--------+-----------------+----------+----------public | d_kenyon        | table    | postgrespublic | d_kenyon_id_seq | sequence | postgres[postgres@localhost ~]$ pg_dump -U postgres -b -Fp  postgres -t d_kenyon -f d_kenyon.bak
[postgres@localhost ~]$ more d_kenyon.bak
--
-- PostgreSQL database dump
--SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;SET search_path = public, pg_catalog;SET default_tablespace = '';SET default_with_oids = false;--
-- Name: d_kenyon; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--CREATE TABLE d_kenyon (id integer NOT NULL,vname character varying(30)
);ALTER TABLE public.d_kenyon OWNER TO postgres;--
-- Name: d_kenyon_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--CREATE SEQUENCE d_kenyon_id_seqSTART WITH 1INCREMENT BY 1NO MINVALUENO MAXVALUECACHE 1;ALTER TABLE public.d_kenyon_id_seq OWNER TO postgres;--
-- Name: d_kenyon_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--ALTER SEQUENCE d_kenyon_id_seq OWNED BY d_kenyon.id;--
-- Name: id; Type: DEFAULT; Schema: public; Owner: postgres
--ALTER TABLE ONLY d_kenyon ALTER COLUMN id SET DEFAULT nextval('d_kenyon_id_seq'::regclass);--
-- Data for Name: d_kenyon; Type: TABLE DATA; Schema: public; Owner: postgres
--COPY d_kenyon (id, vname) FROM stdin;
1       1Hi,Kenyon!
2       2Hi,Kenyon!
3       3Hi,Kenyon!
4       4Hi,Kenyon!
5       5Hi,Kenyon!
6       6Hi,Kenyon!
7       7Hi,Kenyon!
8       8Hi,Kenyon!
[postgres@localhost ~]$ psql
psql (9.2.4)
Type "help" for help.postgres=# \dList of relationsSchema |      Name       |   Type   |  Owner
--------+-----------------+----------+----------public | d_kenyon        | table    | postgrespublic | d_kenyon_id_seq | sequence | postgres
(2 rows)postgres=# drop table d_kenyon;
DROP TABLE
postgres=# \q
[postgres@localhost ~]$ psql < d_kenyon.bak
SET
SET
SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE SEQUENCE
ALTER TABLE
ALTER SEQUENCE
ALTER TABLEsetval
--------10
(1 row)[postgres@localhost ~]$

发现用serial产生的sequence是可以导出并导入的,回过头再去看异常的表,发现该表字段不是serial,模拟一下非serial字段的pg_dump导出情况

postgres=# create table d_test as select * from d_kenyon;
SELECT 10postgres=# alter table d_test alter column id set default nextval('d_kenyon_id_seq'::regclass);
ALTER TABLE
postgres=# \d d_testTable "public.d_test"Column |         Type          |                  Modifiers
--------+-----------------------+----------------------------------------------id     | integer               | default nextval('d_kenyon_id_seq'::regclass)vname  | character varying(30) | postgres=# \q
[postgres@localhost ~]$ pg_dump -U postgres -b -Fp  postgres -t d_test -f d_kenyon.bak
[postgres@localhost ~]$ more d_kenyon.bak
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;--
-- Name: d_test; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--CREATE TABLE d_test (id integer DEFAULT nextval('d_kenyon_id_seq'::regclass),vname character varying(30)
);ALTER TABLE public.d_test OWNER TO postgres;--
-- Data for Name: d_test; Type: TABLE DATA; Schema: public; Owner: postgres
--COPY d_test (id, vname) FROM stdin;
1       1Hi,Kenyon!
2       2Hi,Kenyon!
3       3Hi,Kenyon!
4       4Hi,Kenyon!
5       5Hi,Kenyon!
6       6Hi,Kenyon!
7       7Hi,Kenyon!
8       8Hi,Kenyon!
9       9Hi,Kenyon!
10      10Hi,Kenyon!
\.--
-- PostgreSQL database dump complete
--

确实是没有sequence导出来的,查了一下,pg_depend里序列与表并没有关联上,也就是说这样的表与sequence是独立的,可以用以下SQL验证一下表与sequence的关联关系

WITH fq_objects AS (SELECT c.oid,c.relname AS fqname ,c.relkind, c.relname AS relation FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'), tables    AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' ) SELECT s.fqname AS sequence,'->' as depends,t.fqname AS tableFROM pg_depend d JOIN sequences s ON s.oid = d.objid JOIN tables t ON t.oid = d.refobjid WHERE d.deptype = 'a' and t.fqname = 'd_kenyon';

总结: 
扩展开来想,这种备份如果是整个库备份再恢复,应是OK的,后来验证确实如此,故对于整库恢复是不用考虑这个问题的,单表恢复则需要注意一下。

转载于:https://my.oschina.net/Kenyon/blog/124739

Postgres invalid command \N数据恢复处理相关推荐

  1. ERROR MESSAGE: Invalid command line: Malformed walker argument: Could not find walker with name

    介绍和分析 我是用的环境是 GATK v3.7-0-gcfedb67 的 GenomeAnalysisTK.jar Java环境为: > java -version openjdk versio ...

  2. Python pip – error: invalid command ‘bdist_wheel’

    原文@http://software-engineer.gatsbylee.com/python-pip-error-invalid-command-bdist_wheel/ Python pip – ...

  3. sed 执行错误:sed: 1: “…”: Invalid command code f

    运行 grep -l \'texttofind\' * | xargs sed -i 's/toreplace/replacewith/g' Im getting this error when I ...

  4. PHP 500 -Invalid command RewriteEngine的解决

    转自:http://blog.csdn.net/wang02011/article/details/8205903 环境:   wampserver-2.1a 系统 :  win8 错误 :  500 ...

  5. StandardServer.await: Invalid command 'GET /setting/webSocket HTTP/1.1' rece

    今天跑项目,无意间发现访问时会报  StandardServer.await: Invalid command 'GET /setting/webSocket HTTP/1.1' rece错误,最 ...

  6. Invalid command 'SSLCipherSuite', perhaps misspelled or defined by a module not included in the serv

    Invalid command 'SSLCipherSuite', perhaps misspelled or defined by a module not included in the serv ...

  7. sybase Invalid command line argument 'and'.

    解决方法: 先到sybase central安装目录下(我这里是:C:\sybase\ASE-12_0\bin\syconfig.exe)双击看能不能正常运行.如果可以,就证明是开始菜单里的" ...

  8. _tkinter.TclError: invalid command name “tixComboBox“ 的解决办法

    修改为 ttk.Combobox() 根据Pycharm的自动导入class tkinter.tix.ComboBox,但根据Mannuals描述这个库应该已经废弃了Deprecated since ...

  9. 实验记录 | mutect问题详解:No tribble type was provided on the command line and the type of the file?

    出错详情: /home/xxzhang/workplace/software/java/jdk1.7.0_80/bin/java -Djava.io.tmpdir=./output_RNA/mutmp ...

最新文章

  1. 人工智能应用需要高可信性(180806)
  2. CComboBox 类详细说明
  3. Xcode 11 新建项目适配 iOS 13 以下设备
  4. springboot @ConfigurationProperties注入属性流程
  5. Hadoop学习之路(九)HDFS深入理解
  6. C#多线程开发-任务并行库
  7. bootstrap基础学习十篇
  8. 【转】SharePoint Content Database简介
  9. oracl 单行字符型函数
  10. 确保已在无线网络上启用dhcp服务器,WiFi无线网络提示未启用DHCP无法上网的解决方法教程[多图]...
  11. 初学ASP.Net时在论坛收藏收集的一些资料备忘
  12. 注册ActiveX控件时DllRegisterServer调用失败的解决方法
  13. Excel数据分析—折线图
  14. 【视频编码】【Vue】【明星开源项目】| Chat · 预告
  15. signature=d392c0d1876b3909bd8f7e1f3c0bef22,【技术分享】NSA武器库:CVE-2017-9073 EsteemAudit分析...
  16. SpringBoot重点详解--事件监听
  17. thinkphp ajax 跨域请求 Access-Control-Allow-Origin 完美解决
  18. 企业级微信小程序实战详解
  19. java qq聊天界面_【附源码】用Java写了一个类QQ界面聊天小项目,可在线聊天!...
  20. 发表16篇Nature、14篇Science!这位顶尖学者告诉你论文十大诀窍

热门文章

  1. Perl 监控 tomcat,可以安心回家过年了
  2. Java程序模拟QQ空间登录 - 并模拟刷说说的赞(图文) 注意:腾讯修改了加密算法,已失效(2015-01-31)
  3. 广西龙脊梯田景区发生山体崩塌
  4. Oracle ODI 12c之多表联合查询以及定时任务设置
  5. 区分元素特性attribute和对象属性property
  6. three.js加入监控
  7. 排序算法浅析(一)比较排序算法
  8. Golang map 三板斧第一式:快速上手
  9. Linux 命令(51)—— ipcs 命令
  10. Linux pid_t 类型的定义