some sequences not in dump from pg_dump - postgresql

I want to pg_dump my database and have the problem that some sequences are generated in the dump file and some are not.
With the table infrastruktur_pictures it works with the table hoehenprofile it doesn't work. Here are the infos about the tables from pgadmin3:
hoehenprofile
CREATE SEQUENCE "hoehenprofile_HID_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 14289
CACHE 1;
ALTER TABLE "hoehenprofile_HID_seq";
CREATE TABLE hoehenprofile
(
"HID" serial NOT NULL,
hoehenprofil character varying(255),
CONSTRAINT "HID" PRIMARY KEY ("HID")
)
WITH (
OIDS=FALSE
);
ALTER TABLE hoehenprofile ADD COLUMN "HID" integer;
ALTER TABLE hoehenprofile ALTER COLUMN "HID" SET NOT NULL;
ALTER TABLE hoehenprofile ALTER COLUMN "HID" SET DEFAULT nextval('"hoehenprofile_HID_seq"'::regclass);
infrastruktur_pictures
CREATE SEQUENCE "infrastruktur_pictures_IPID_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE "infrastruktur_pictures_IPID_seq";
CREATE TABLE infrastruktur_pictures
(
"IPID" serial NOT NULL,
picture character varying(255) NOT NULL,
picture_text text,
fotograf text,
CONSTRAINT prim PRIMARY KEY ("IPID")
)
WITH (
OIDS=FALSE
);
ALTER TABLE infrastruktur_pictures ADD COLUMN "IPID" integer;
ALTER TABLE infrastruktur_pictures ALTER COLUMN "IPID" SET NOT NULL;
ALTER TABLE infrastruktur_pictures ALTER COLUMN "IPID" SET DEFAULT nextval('"infrastruktur_pictures_IPID_seq"'::regclass);
In the dump file the generated Code for hoehenprofile looks like this(no sequence is generated):
--
-- TOC entry 145 (class 1259 OID 67719)
-- Name: hoehenprofile; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE hoehenprofile (
"HID" integer DEFAULT nextval('"hoehenprofile_HID_seq"'::regclass) NOT NULL,
hoehenprofil character varying(255)
);
The code for infrastruktur_pictures looks like this:
-
-- TOC entry 152 (class 1259 OID 67750)
-- Name: infrastruktur_pictures; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE infrastruktur_pictures (
"IPID" integer NOT NULL,
picture character varying(255) NOT NULL,
picture_text text,
fotograf text
);
--
-- TOC entry 4230 (class 0 OID 0)
-- Dependencies: 152
-- Name: TABLE infrastruktur_pictures; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE infrastruktur_pictures IS 'Bilder im Infrastruktur Bereich des Backends';
--
-- TOC entry 153 (class 1259 OID 67756)
-- Name: infrastruktur_pictures_IPID_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE "infrastruktur_pictures_IPID_seq"
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- TOC entry 4231 (class 0 OID 0)
-- Dependencies: 153
-- Name: infrastruktur_pictures_IPID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE "infrastruktur_pictures_IPID_seq" OWNED BY infrastruktur_pictures."IPID";
The only difference between this two tables i can see in pgadmin3. When i click on the specific cloumn(HID, IPID) in the object browser and look at the properties Tab right of it i can see that at the HID column the sequence attribute is not set.

The difference is that in the second case sequence is owned by table. Check last line of pg_dump.
This is because, when you create table using serial pseudo-datatype, the ownership is automatically added. But when you created sequence manually, you didn't set "OWNED BY".

Related

How to reference hypertables properly using foreign key constraints in PostgreSQL?

#Error description:
It's possible to create a table that has a foreign key into a hypertable provided the foreign key is defined when the table is created
#To Reproduce, there are next tables:
CREATE TABLE ids (
measurement_id int DEFAULT 0,
description text DEFAULT 0,
m_id bigserial NOT NULL,
service_id int DEFAULT NULL,
time bigint NOT NULL DEFAULT cast((EXTRACT(EPOCH FROM now() AT TIME ZONE 'UTC') * 1000) as bigint),
user_id int DEFAULT NULL,
end_time DOUBLE PRECISION DEFAULT 0,
start_time int NOT NULL DEFAULT 0
);
CREATE INDEX ON ids (time DESC, user_id);
CREATE INDEX ON ids (time DESC, service_id);
SELECT create_hypertable('ids', 'start_time', chunk_time_interval => 604800016);
---------
CREATE TABLE IF NOT EXISTS metrics (
id bigserial NOT NULL,
duration real DEFAULT NULL,
metric integer DEFAULT 0,
m_id bigint NOT NULL,
time bigint NOT NULL DEFAULT 0
);
ALTER TABLE metrics ADD PRIMARY KEY (time, m_id);
CREATE INDEX ON metrics (time DESC);
CREATE INDEX ON metrics (time DESC, measurement );
CREATE INDEX ON metrics (time DESC, m_id );
grant all privileges on ids, metrics to your_db_user;
SELECT create_hypertable('metrics', 'time' , chunk_time_interval => 604800016);
SELECT table_catalog, table_schema, table_name, privilege_type FROM information_schema.table_privileges WHERE grantee = 'your_db_user';
---------
DROP TABLE IF EXISTS resource;
CREATE TABLE resource(
id int NOT NULL,
cpu text DEFAULT 0,
storing text DEFAULT 0,
memory text DEFAULT 0
);
ALTER TABLE resource ADD PRIMARY KEY (id);
CREATE SEQUENCE resource_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1;
ALTER TABLE resource_id_seq
OWNER TO your_db_user;
ALTER TABLE resource ALTER COLUMN id SET DEFAULT nextval('resource_id_seq'::regclass);
---------
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
DROP TABLE IF EXISTS ns;
CREATE TABLE ns(
id bigint NOT NULL,
uuid uuid NOT NULL DEFAULT uuid_generate_v4 (),
availability double precision,
faultTolerance boolean,
activated boolean,
UNIQUE (id, uuid),
PRIMARY KEY(id),
CONSTRAINT fk_resource
FOREIGN KEY(id)
REFERENCES resource(id)
ON DELETE CASCADE
);
CREATE SEQUENCE ns_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE ns_id_seq
OWNER TO your_db_user;
ALTER TABLE ns ALTER COLUMN id SET DEFAULT nextval('ns_id_seq'::regclass);
---------
DROP TABLE IF EXISTS authentication;
CREATE TABLE authentication(
id integer NOT NULL,
username character varying(255) NOT NULL,
password character varying(255) NOT NULL,
host character varying(255) NOT NULL,
port character varying(10) NOT NULL,
PRIMARY KEY(id)
);
CREATE SEQUENCE auth_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1;
ALTER TABLE auth_id_seq
OWNER TO your_db_user;
ALTER TABLE authentication ALTER COLUMN id SET DEFAULT nextval('auth_id_seq'::regclass);
---------
DROP TABLE IF EXISTS job;
CREATE TABLE job(
id int NOT NULL,
interval integer NOT NULL,
auth_id integer REFERENCES authentication (id),
ns_id integer REFERENCES ns (id),
UNIQUE (auth_id, ns_id),
PRIMARY KEY(id)
);
ALTER TABLE job
ADD CONSTRAINT fk_auth_id
FOREIGN KEY (id) REFERENCES authentication (id)
ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE job
ADD CONSTRAINT fk_ns_id
FOREIGN KEY (id) REFERENCES ns (id)
ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED;
CREATE SEQUENCE job_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1;
ALTER TABLE job_id_seq
OWNER TO your_db_user;
ALTER TABLE job ALTER COLUMN id SET DEFAULT nextval('job_id_seq'::regclass);
---------
DROP TABLE IF EXISTS job_metric;
CREATE TABLE job_metric (
id int NOT NULL,
j_id int NOT NULL REFERENCES job (id),
mj_id bigint NOT NULL,
jm_time bigint NOT NULL
);
CREATE INDEX ON job_metric (jm_time DESC);
CREATE INDEX ON job_metric (jm_time DESC, id);
CREATE INDEX ON job_metric (jm_time DESC, mj_id);
ALTER TABLE job_metric ADD PRIMARY KEY (jm_time, id);
grant all privileges on job_metric to your_db_user;
SELECT create_hypertable('job_metric', 'jm_time' , chunk_time_interval => 604800016);
CREATE SEQUENCE mjob_metric_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 2147483647
START 1
CACHE 1;
ALTER TABLE mjob_metric_id_seq
OWNER TO your_db_user;
ALTER TABLE job_metric ALTER COLUMN id SET DEFAULT nextval('mjob_metric_id_seq'::regclass);
---------
After creating the tables, I have used the solution proposed by #Laurenz in a database with PostgreSQL 12.6 using the extension of timescaledb 1.7.5 as follows:
#To fill the table with the appropriate values:
UPDATE job_metric AS jm_point
SET jm_time = qm.time
FROM metrics AS qm
WHERE qm.m_id = jm_point.mj_id;
#Then set it NOT NULL:
ALTER TABLE job_metric ALTER jm_time SET NOT NULL;
#To define your foreign key:
ALTER TABLE job_metric
ADD FOREIGN KEY (mj_id, jm_time)
REFERENCES metrics (time, m_id) MATCH FULL;
#Response of the last reference table to enable foreign key: Query returned successfully in 40 msec.
Expected behavior:
The idea is to use the table job_metric in an even many-to-many relationship to access the information of job and metrics tables.
Actual behavior and error:
Tables are created and FKs were created but cannot be used when data is inserted at job_metric as is detailed in the following:
INSERT INTO job_metric (j_id, mj_id, jm_time)
VALUES(13, 185063, 1621957192266);
ERROR: foreign keys to hypertables are not supported CONTEXT: SQL
statement " ALTER TABLE _timescaledb_internal._hyper_5_5_chunk ADD
CONSTRAINT "5_13_job_metric_j_id_mj_id_jm_time_fkey" FOREIGN KEY
(j_id, mj_id, jm_time) REFERENCES qmetrics("time", m_id) MATCH FULL "
PL/pgSQL function
_timescaledb_internal.chunk_constraint_add_table_constraint(_timescaledb_catalog.chunk_constraint)
line 42 at EXECUTE SQL state: 0A000
***According to https://docs.timescale.com/timescaledb/latest/overview/limitations/##distributed-hypertable-limitations, it looks like the above error is part of the hypertable limitations:
Foreign key constraints referencing a hypertable are not supported.
#Request:
Given the above information and errors, does anyone know any solution at the DB level to establish the relationships (many-to-many or one-to-many) using timescaledb extension and mainly hypertables?
Actually, I have obtained similar of above error when I had attempted to create many-to-many relation among the tables metrics and job_metric using the Django Rest Framework:
class Job_Metrics(models.Model):
job = models.OneToOneField(Job, on_delete=models.CASCADE)
metrics = models.ManyToManyField(Metrics)
time = models.IntegerField(default=0)
Running the application metrics pointing out directly metrics_db:
$ python3 manage.py migrate metrics --database=metrics_db
Operations to perform: Apply all migrations: metrics Running migrations: Applying
metrics.0002_job...Traceback (most recent call last): File
"/var/myproject/myprojectenv/lib/python3.8/site-packages/django/db/backends/utils.py",
line 84, in _execute return self.cursor.execute(sql, params)
psycopg2.errors.FeatureNotSupported: foreign keys to hypertables are
not supported
If someone knows a solution or has an idea to deal with the above error at the REST API level, please could you share your idea with the aim to access data associated tables (metrics and jobs) and modify them together when is required to delete e.g., a job_metric. So far, using hypertables amendments of timescaledb extension seems to be not a viable solution.

postgresql 10 altering serial column error 42p01

I am facing an issue when correcting an existing table to use serial column on the primary key. In order to simulate the issue I created a new table:
CREATE TABLE markets."TestSequence" (
"Id" integer NOT NULL,
"Name" text COLLATE pg_catalog."default",
CONSTRAINT "PK_TestSequence" PRIMARY KEY ("Id")
);
Then I ran the query that is causing problem:
ALTER TABLE markets."TestSequence" ALTER COLUMN "Id" TYPE integer;
ALTER TABLE markets."TestSequence" ALTER COLUMN "Id" SET NOT NULL;
CREATE SEQUENCE "TestSequence_Id_seq" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
ALTER TABLE markets."TestSequence" ALTER COLUMN "Id" SET DEFAULT (nextval('"TestSequence_Id_seq"'));
ALTER SEQUENCE "TestSequence_Id_seq" OWNED BY "TestSequence"."Id";
I get the following error:
ERROR: relation "TestSequence" does not exist
SQL state: 42P01
According to the doc OWNED BY does not take any schema prefix. So I tried to create the table without schema and it works fine.
CREATE TABLE "TestSequence" (
"Id" integer NOT NULL,
"Name" text COLLATE pg_catalog."default",
CONSTRAINT "PK_TestSequence" PRIMARY KEY ("Id")
);
and run the corresponding alter queries:
ALTER TABLE "TestSequence" ALTER COLUMN "Id" TYPE integer;
ALTER TABLE "TestSequence" ALTER COLUMN "Id" SET NOT NULL;
CREATE SEQUENCE "TestSequence_Id_seq" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
ALTER TABLE "TestSequence" ALTER COLUMN "Id" SET DEFAULT (nextval('"TestSequence_Id_seq"'));
ALTER SEQUENCE "TestSequence_Id_seq" OWNED BY "TestSequence"."Id";
How can I make this work for relations with schema?
The doc you have linked says, for owned by
The specified table must have the same owner and be in the same schema
as the sequence.
You haven't specified a schema for the sequence, so it is created in public by default, which is not the same as the table schema.
Try creating the sequence as
CREATE SEQUENCE markets."TestSequence_Id_seq" AS integer ...
That being said, nothing prevents you from specifying the schema of both the sequence and the table
ALTER SEQUENCE markets."TestSequence_Id_seq" OWNED BY markets."TestSequence"."Id";

Pg_dump -s (schema) of tables omits the defaults for columns that use nextval()

After defining the table, the script generated by PGAdmin via rightclick->SCRIPT->CREATE SCRIPT, looks like this:
-- DROP TABLE public.arr;
CREATE TABLE public.arr
(
rcdno integer NOT NULL DEFAULT nextval('arr_rcdno_seq'::regclass),
rcdstate character varying(10) COLLATE pg_catalog."default" DEFAULT 'ACTIVE'::character varying,
rcdserial integer NOT NULL DEFAULT 0,
msrno integer NOT NULL DEFAULT nextval('arr_msrno_seq'::regclass),
mobileid character varying(50) COLLATE pg_catalog."default" NOT NULL,
edittimestamp timestamp without time zone DEFAULT now(),
editor character varying(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT pk_arr PRIMARY KEY (mobileid, msrno, rcdserial)
)
After using >pg_dump -s -U webmaster -W -F p Test > c:\temp\Test.sql, the table definition in the script OMITS the defaults for the 2 original "serial" columns. This means that the pg_dump script doesn't create the table correctly when it is run!
--
-- Name: arr; Type: TABLE; Schema: public; Owner: webmaster
--
CREATE TABLE public.arr (
rcdno integer NOT NULL, -- default missing!
rcdstate character varying(10) DEFAULT 'ACTIVE'::character varying,
rcdserial integer DEFAULT 0 NOT NULL,
msrno integer NOT NULL, -- default missing!
mobileid character varying(50) NOT NULL,
edittimestamp timestamp without time zone DEFAULT now(),
editor character varying(20) NOT NULL
);
ALTER TABLE public.arr OWNER TO webmaster;
--
-- Name: arr_msrno_seq; Type: SEQUENCE; Schema: public; Owner: webmaster
--
CREATE SEQUENCE public.arr_msrno_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.arr_msrno_seq OWNER TO webmaster;
--
-- Name: arr_msrno_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: webmaster
--
ALTER SEQUENCE public.arr_msrno_seq OWNED BY public.arr.msrno;
--
-- Name: arr_rcdno_seq; Type: SEQUENCE; Schema: public; Owner: webmaster
--
CREATE SEQUENCE public.arr_rcdno_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.arr_rcdno_seq OWNER TO webmaster;
--
-- Name: arr_rcdno_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: webmaster
--
ALTER SEQUENCE public.arr_rcdno_seq OWNED BY public.arr.rcdno;
EDIT: After the generated script has been run, the statement
into public.arr(mobileid, editor)
values
('12345', 'nolaspeaker')
generates
ERROR: null value in column "rcdno" violates not-null constraint DETAIL: Failing row contains (null, ACTIVE, 0, null, 12345, 2020-08-18 08:54:41.34052, nolaspeaker). SQL state: 23502
Which indicates that I am correct to assert that the script omits the default values for the rcdno column!
The ALTER TABLE statements that set the default values are towards the end of the dump, close to where the ALTER SEQUENCE ... OWNED BY are.
If they don't get restored, that might be because there was a problem creating the sequences.
Look at the error messages from the restore, particularly the first ones (later ones are often consequences of the earlier ones).
The statements that assign the default values to the applicable columns come much later in the script
--
-- Name: arr rcdno; Type: DEFAULT; Schema: public; Owner: webmaster
--
ALTER TABLE ONLY public.arr ALTER COLUMN rcdno SET DEFAULT nextval('public.arr_rcdno_seq'::regclass);
--
-- Name: arr msrno; Type: DEFAULT; Schema: public; Owner: webmaster
--
ALTER TABLE ONLY public.arr ALTER COLUMN msrno SET DEFAULT nextval('public.arr_msrno_seq'::regclass);
So you have to be careful if you cut-and-paste a table definition out of the script!

PostgreSQL script references hangs

I"new to PostgreSQL and am struggling with a DDL issue. Below script "hangs" unless I remove column "keel" from table and unique constraint definitions. What may be the error ? Your input highly appreciated.
CREATE TABLE k_keel
(
id integer NOT NULL,
nim character varying(30) NOT NULL,
kood character(2) NOT NULL, -- Kahetäheline ISO 639 keelekood
CONSTRAINT k_keel_pkey PRIMARY KEY (id),
CONSTRAINT k_keel_nim_uniq UNIQUE (nim)
)
WITH (
OIDS=FALSE
);
ALTER TABLE k_keel
OWNER TO postgres;
GRANT ALL ON TABLE k_keel TO postgres;
GRANT SELECT ON TABLE k_keel TO overload;
COMMENT ON COLUMN k_keel.kood IS 'Kahetäheline ISO 639 keelekood';
-- Table: ac.e_kiri
-- DROP TABLE ac.e_kiri CASCADE;
CREATE TABLE ac.e_kiri
(
id INTEGER PRIMARY KEY DEFAULT nextval('ac.email_id_seq'::regclass),
nimi character varying(100) NOT NULL UNIQUE,
memo TEXT
)
WITH (
OIDS=FALSE
);
ALTER TABLE ONLY ac.e_kiri
ADD CONSTRAINT "Nimi peal olema unikaalne" UNIQUE (nimi);
ALTER TABLE ac.e_kiri
OWNER TO postgres;
GRANT ALL ON TABLE ac.e_kiri TO postgres;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE ac.e_kiri TO overload;
COMMENT ON TABLE ac.e_kiri IS 'Tüüpkirjade tabel';
-- DROP TABLE ac.e_kiri_tekst CASCADE;
CREATE TABLE ac.e_kiri_tekst
(
id INTEGER PRIMARY KEY DEFAULT nextval('ac.email_id_seq'::regclass),
e_kiri INTEGER REFERENCES ac.e_kiri (id) NOT NULL,
keel INTEGER REFERENCES public.k_keel (id) NOT NULL,
subjekt text,
sisu text
)
WITH (
OIDS=FALSE
);
ALTER TABLE ONLY ac.e_kiri_tekst
ADD CONSTRAINT "Nimi ja keel peavad olema unikaalsed" UNIQUE (e_kiri,keel);
ALTER TABLE ac.e_kiri_tekst
OWNER TO postgres;
GRANT ALL ON TABLE ac.e_kiri_tekst TO postgres;
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE ac.e_kiri_tekst TO overload;
COMMENT ON TABLE ac.e_kiri_tekst IS 'Tüüpkirja subjekt ja tekst antud keeles';

Why can I not set this unique constraint in PostgreSQL?

I keep getting:
SQL error: ERROR: could not create
unique index
"service_import_checksum_key" DETAIL:
Key (checksum)=() is duplicated.
In statement:
ALTER TABLE "public"."service_import" ADD CONSTRAINT "service_import_checksum_key" UNIQUE ("checksum")
But this constraint ISN'T a duplicate. There is no other constraint like this anywhere in the entire database and I have no idea why on earth it keeps insisting it's a duplicate. I'm assuming this is some weird nuance of postgres that I'm missing here.
What am I doing wrong?
Table dump:
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: service_import; Type: TABLE; Schema: public; Owner: cvs_tar; Tablespace:
--
CREATE TABLE service_import (
id integer NOT NULL,
name character varying(32) NOT NULL,
importfile character varying(64) NOT NULL,
reportfile character varying(64) NOT NULL,
percent smallint NOT NULL,
message text NOT NULL,
stamp timestamp without time zone DEFAULT now() NOT NULL,
complete smallint DEFAULT 0 NOT NULL,
checksum character varying(40) NOT NULL
);
ALTER TABLE public.service_import OWNER TO cvs_tar;
--
-- Name: service_imports_id_seq; Type: SEQUENCE; Schema: public; Owner: cvs_tar
--
CREATE SEQUENCE service_imports_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.service_imports_id_seq OWNER TO cvs_tar;
--
-- Name: service_imports_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: cvs_tar
--
ALTER SEQUENCE service_imports_id_seq OWNED BY service_import.id;
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: cvs_tar
--
ALTER TABLE service_import ALTER COLUMN id SET DEFAULT nextval('service_imports_id_seq'::regclass);
--
-- Name: service_import_name_key; Type: CONSTRAINT; Schema: public; Owner: cvs_tar; Tablespace:
--
ALTER TABLE ONLY service_import
ADD CONSTRAINT service_import_name_key UNIQUE (name);
--
-- Name: service_import_pkey; Type: CONSTRAINT; Schema: public; Owner: cvs_tar; Tablespace:
--
ALTER TABLE ONLY service_import
ADD CONSTRAINT service_import_pkey PRIMARY KEY (id);
--
-- Name: service_import_complete_idx; Type: INDEX; Schema: public; Owner: cvs_tar; Tablespace:
--
CREATE INDEX service_import_complete_idx ON service_import USING btree (complete);
--
-- Name: service_import_stamp_idx; Type: INDEX; Schema: public; Owner: cvs_tar; Tablespace:
--
CREATE INDEX service_import_stamp_idx ON service_import USING btree (stamp);
--
-- PostgreSQL database dump complete
--
Read the error message again:
SQL error: ERROR: could not create unique index "service_import_checksum_key"
DETAIL: Key (checksum)=() is duplicated.
Looks like it is telling you that there are duplicate values in the checksum column and you're trying to enforce uniqueness on that column with your constraint. The constraint isn't duplicated, the data has duplicates.
Furthermore, the "()" part indicates that you have multiple empty strings in the checksum column. Unique constraints allow multiple NULL values (since NULL = NULL is NULL which is not true) but empty strings are not NULL.
An example to clarify what's going on:
=> CREATE TABLE x (s VARCHAR NULL);
CREATE TABLE
=> INSERT INTO x (s) VALUES (''), ('a'), ('');
INSERT 0 3
=> ALTER TABLE x ADD CONSTRAINT ux UNIQUE(s);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "ux" for table "x"
ERROR: could not create unique index "ux"
DETAIL: Key (s)=() is duplicated.
=> delete from x where s='';
DELETE 2
=> INSERT INTO x (s) VALUES ('a');
INSERT 0 1
=> ALTER TABLE x ADD CONSTRAINT ux UNIQUE(s);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "ux" for table "x"
ERROR: could not create unique index "ux"
DETAIL: Key (s)=(a) is duplicated.
In particular, note what the ERROR and DETAIL are saying and compare that to the INSERTs.