@@ -225,3 +225,49 @@ TABLE tab4;
225225 1 | 0.10
226226(1 row)
227227
228+ -- One more test for "manual" validation of FKs, this thime we rewrite the PK
229+ -- table. The NOT VALID constraint cannot be used if the FK table is
230+ -- partitioned, so we need a separate test.
231+ CREATE TABLE tab1_pk(i int primary key);
232+ INSERT INTO tab1_pk(i) VALUES (1);
233+ CREATE TABLE tab1_pk_new(i bigint primary key);
234+ DROP TABLE tab1_fk;
235+ CREATE TABLE tab1_fk(i int REFERENCES tab1_pk);
236+ INSERT INTO tab1_fk(i) VALUES (1);
237+ \d tab1_pk
238+ Table "public.tab1_pk"
239+ Column | Type | Collation | Nullable | Default
240+ --------+---------+-----------+----------+---------
241+ i | integer | | not null |
242+ Indexes:
243+ "tab1_pk_pkey" PRIMARY KEY, btree (i)
244+ Referenced by:
245+ TABLE "tab1_fk" CONSTRAINT "tab1_fk_i_fkey" FOREIGN KEY (i) REFERENCES tab1_pk(i)
246+
247+ SELECT rewrite_table('tab1_pk', 'tab1_pk_new', 'tab1_pk_orig');
248+ rewrite_table
249+ ---------------
250+
251+ (1 row)
252+
253+ \d tab1_pk
254+ Table "public.tab1_pk"
255+ Column | Type | Collation | Nullable | Default
256+ --------+--------+-----------+----------+---------
257+ i | bigint | | not null |
258+ Indexes:
259+ "tab1_pk_new_pkey" PRIMARY KEY, btree (i)
260+ Referenced by:
261+ TABLE "tab1_fk" CONSTRAINT "tab1_fk_i_fkey2" FOREIGN KEY (i) REFERENCES tab1_pk(i) NOT VALID
262+
263+ ALTER TABLE tab1_fk VALIDATE CONSTRAINT tab1_fk_i_fkey2;
264+ \d tab1_pk
265+ Table "public.tab1_pk"
266+ Column | Type | Collation | Nullable | Default
267+ --------+--------+-----------+----------+---------
268+ i | bigint | | not null |
269+ Indexes:
270+ "tab1_pk_new_pkey" PRIMARY KEY, btree (i)
271+ Referenced by:
272+ TABLE "tab1_fk" CONSTRAINT "tab1_fk_i_fkey2" FOREIGN KEY (i) REFERENCES tab1_pk(i)
273+
0 commit comments