Postgres 数据库 pg_dump - 转储文件详细说明

Postgres database pg_dump - dump file verbosity explanation

我用 pg_dump 创建了一个 postgres 数据库转储。我现在想知道为什么备份文件这么冗长。

a) 这个配置的目的是什么:

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;

b) 为什么 table 的创建如此冗长:

CREATE TABLE public.tag (
    id integer NOT NULL,
    name character varying(100) NOT NULL,
    description text
);

ALTER TABLE public.tag OWNER TO postgres;

CREATE SEQUENCE public.tag_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER TABLE public.tag_id_seq OWNER TO postgres;
ALTER SEQUENCE public.tag_id_seq OWNED BY public.tag.id;
ALTER TABLE ONLY public.tag ALTER COLUMN id SET DEFAULT nextval('public.tag_id_seq'::regclass);

为什么不像我之前创建的那样:

CREATE TABLE tag (
  id SERIAL PRIMARY KEY, 
  name VARCHAR(100) NOT NULL,
  description TEXT
);

c) 为什么使用 COPY ... FROM stdin; 而不是 INSERT INTO ... VALUES

COPY public.tag (id, name, description) FROM stdin;
1   vegetarian  No animals.
2   vegan   No animal products.
3   vegetable   
\.

SELECT pg_catalog.setval('public.tag_id_seq', 3, true);

ALTER TABLE ONLY public.tag
    ADD CONSTRAINT tag_pkey PRIMARY KEY (id);

我建议在这里花些时间 pg_dump。同时:

a) 将数据库环境设置为与您从中转储的数据库兼容。还有一个安全功能 SELECT pg_catalog.set_config('search_path', '', false); 请参阅此处 CVE-2018-1058 了解更多信息。

b) 这是因为转储包含三个部分“前数据、数据、post-数据”。有关完整说明,请参阅上面的 link。默认情况下,它将转储所有三个,但将各部分分开。它还允许诸如触发器之类的东西,如果单独恢复和稍后恢复效果最好,因此在输入数据时它们不是 运行。

c) COPYINSERTS 快几个数量级。您可以指定使用 INSERTS,但我建议您不要使用,除非您将数据移动到另一个不理解 COPY.

的 SQL 数据库