From 2d2ab39bcbc7ea13cd04986b0c8aad6f14449ebd Mon Sep 17 00:00:00 2001
From: Chris Bandy <bandy.chris@gmail.com>
Date: Mon, 2 Mar 2020 22:13:28 -0600
Subject: [PATCH] Tests for partition error fields

---
 src/test/regress/expected/partition_errors.out | 37 ++++++++++++++++++++++++++
 src/test/regress/parallel_schedule             |  2 +-
 src/test/regress/sql/partition_errors.sql      | 23 ++++++++++++++++
 3 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 src/test/regress/expected/partition_errors.out
 create mode 100644 src/test/regress/sql/partition_errors.sql

diff --git src/test/regress/expected/partition_errors.out src/test/regress/expected/partition_errors.out
new file mode 100644
index 0000000000..c5a3b8c815
--- /dev/null
+++ src/test/regress/expected/partition_errors.out
@@ -0,0 +1,37 @@
+--
+-- Tests for partition error fields
+--
+\set VERBOSITY verbose
+-- no partitions
+CREATE TABLE pterr1 (x int, y int, PRIMARY KEY (x, y)) PARTITION BY RANGE (y);
+INSERT INTO pterr1 VALUES (10, 10);
+ERROR:  23514: no partition of relation "pterr1" found for row
+DETAIL:  Partition key of the failing row contains (y) = (10).
+SCHEMA NAME:  public
+TABLE NAME:  pterr1
+LOCATION:  ExecFindPartition, execPartition.c:349
+-- outside the only partition
+CREATE TABLE pterr1_p1 PARTITION OF pterr1 FOR VALUES FROM (1) TO (5);
+INSERT INTO pterr1 VALUES (10, 10);
+ERROR:  23514: no partition of relation "pterr1" found for row
+DETAIL:  Partition key of the failing row contains (y) = (10).
+SCHEMA NAME:  public
+TABLE NAME:  pterr1
+LOCATION:  ExecFindPartition, execPartition.c:349
+INSERT INTO pterr1_p1 VALUES (10, 10);
+ERROR:  23514: new row for relation "pterr1_p1" violates partition constraint
+DETAIL:  Failing row contains (10, 10).
+SCHEMA NAME:  public
+TABLE NAME:  pterr1_p1
+LOCATION:  ExecPartitionCheckEmitError, execMain.c:1882
+-- conflict with default
+CREATE TABLE pterr1_default PARTITION OF pterr1 DEFAULT;
+INSERT INTO pterr1 VALUES (10, 10);
+CREATE TABLE pterr1_p2 PARTITION OF pterr1 FOR VALUES FROM (6) TO (20);
+ERROR:  23514: updated partition constraint for default partition "pterr1_default" would be violated by some row
+SCHEMA NAME:  public
+TABLE NAME:  pterr1_default
+LOCATION:  check_default_partition_contents, partbounds.c:1370
+-- cleanup
+\set VERBOSITY default
+DROP TABLE pterr1, pterr1_default, pterr1_p1;
diff --git src/test/regress/parallel_schedule src/test/regress/parallel_schedule
index d2b17dd3ea..e1708c87ec 100644
--- src/test/regress/parallel_schedule
+++ src/test/regress/parallel_schedule
@@ -112,7 +112,7 @@ test: plancache limit plpgsql copy2 temp domain rangefuncs prepare conversion tr
 # ----------
 # Another group of parallel tests
 # ----------
-test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain
+test: partition_errors partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain
 
 # event triggers cannot run concurrently with any test that runs DDL
 test: event_trigger
diff --git src/test/regress/sql/partition_errors.sql src/test/regress/sql/partition_errors.sql
new file mode 100644
index 0000000000..7b9197d507
--- /dev/null
+++ src/test/regress/sql/partition_errors.sql
@@ -0,0 +1,23 @@
+--
+-- Tests for partition error fields
+--
+
+\set VERBOSITY verbose
+
+-- no partitions
+CREATE TABLE pterr1 (x int, y int, PRIMARY KEY (x, y)) PARTITION BY RANGE (y);
+INSERT INTO pterr1 VALUES (10, 10);
+
+-- outside the only partition
+CREATE TABLE pterr1_p1 PARTITION OF pterr1 FOR VALUES FROM (1) TO (5);
+INSERT INTO pterr1 VALUES (10, 10);
+INSERT INTO pterr1_p1 VALUES (10, 10);
+
+-- conflict with default
+CREATE TABLE pterr1_default PARTITION OF pterr1 DEFAULT;
+INSERT INTO pterr1 VALUES (10, 10);
+CREATE TABLE pterr1_p2 PARTITION OF pterr1 FOR VALUES FROM (6) TO (20);
+
+-- cleanup
+\set VERBOSITY default
+DROP TABLE pterr1, pterr1_default, pterr1_p1;
-- 
2.11.0

