From dd4ca824cc91f0c3156d4ed9774b03005d9580e0 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Wed, 24 Apr 2002 02:22:54 +0000
Subject: [PATCH] Reports missing values as bad.

BAD:  INSERT INTO tab (col1, col2) VALUES ('val1');
GOOD: INSERT INTO tab (col1, col2) VALUES ('val1', 'val2');

Regress tests against DEFAULT and normal values as they're managed
slightly different.

Rod Taylor
---
 src/backend/parser/analyze.c         | 12 +++++++-----
 src/test/regress/expected/insert.out | 20 ++++++++++++++++++++
 src/test/regress/sql/insert.sql      | 10 ++++++++++
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index b3b8859d126..c0fe929e497 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- *	$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.231 2002/04/17 20:57:56 tgl Exp $
+ *	$Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.232 2002/04/24 02:22:54 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -547,10 +547,12 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
 	}
 
 	/*
-	 * XXX It is possible that the targetlist has fewer entries than were
-	 * in the columns list.  We do not consider this an error.	Perhaps we
-	 * should, if the columns list was explicitly given?
+	 * Ensure that the targetlist has the same number of  entries
+	 * that were present in the columns list.  Don't do the check
+	 * for select statements.
 	 */
+	if (stmt->cols != NIL && (icolumns != NIL || attnos != NIL))
+		elog(ERROR, "INSERT has more target columns than expressions");
 
 	/* done building the range table and jointree */
 	qry->rtable = pstate->p_rtable;
@@ -3247,7 +3249,7 @@ analyzeCreateSchemaStmt(CreateSchemaStmt *stmt)
 		}
 	}
 
-	result = NIL;	
+	result = NIL;
 	result = nconc(result, cxt.tables);
 	result = nconc(result, cxt.views);
 	result = nconc(result, cxt.grants);
diff --git a/src/test/regress/expected/insert.out b/src/test/regress/expected/insert.out
index f0688762f57..e4ecf934171 100644
--- a/src/test/regress/expected/insert.out
+++ b/src/test/regress/expected/insert.out
@@ -17,4 +17,24 @@ select * from inserttest;
       |    7 | testing
 (4 rows)
 
+--
+-- insert with similar expression / target_list values (all fail)
+--
+insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT);
+ERROR:  INSERT has more target columns than expressions
+insert into inserttest (col1, col2, col3) values (1, 2);
+ERROR:  INSERT has more target columns than expressions
+insert into inserttest (col1) values (1, 2);
+ERROR:  INSERT has more expressions than target columns
+insert into inserttest (col1) values (DEFAULT, DEFAULT);
+ERROR:  INSERT has more expressions than target columns
+select * from inserttest;
+ col1 | col2 |  col3   
+------+------+---------
+      |    3 | testing
+      |    5 | testing
+      |    5 | test
+      |    7 | testing
+(4 rows)
+
 drop table inserttest;
diff --git a/src/test/regress/sql/insert.sql b/src/test/regress/sql/insert.sql
index 5d42c87649f..90badf37b78 100644
--- a/src/test/regress/sql/insert.sql
+++ b/src/test/regress/sql/insert.sql
@@ -8,5 +8,15 @@ insert into inserttest (col1, col2, col3) values (DEFAULT, 5, DEFAULT);
 insert into inserttest values (DEFAULT, 5, 'test');
 insert into inserttest values (DEFAULT, 7);
 
+select * from inserttest;
+
+--
+-- insert with similar expression / target_list values (all fail)
+--
+insert into inserttest (col1, col2, col3) values (DEFAULT, DEFAULT);
+insert into inserttest (col1, col2, col3) values (1, 2);
+insert into inserttest (col1) values (1, 2);
+insert into inserttest (col1) values (DEFAULT, DEFAULT);
+
 select * from inserttest;
 drop table inserttest;
-- 
GitLab