Hi,

I am trying to revise my patch for counting the number of rows inserted
when using a COPY statement.

I set it up a printf to see the results in the log file.

When I execute a COPY the first time it reports 0 rows inserted(but
actually inserts all rows).  If I execute again it reports the number of
rows + 1.  Execute a third time and the number of rows are reported +
1(same result as 2nd run).  All three tests were done against the same
file.

If there is a simple explanation to why ProcessUtility is not called the
first time COPY is executed and it is from then on it could help me a
lot.  





Index: copy.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/copy.c,v
retrieving revision 1.218
diff -u -r1.218 copy.c
--- copy.c	10 Feb 2004 01:55:24 -0000	1.218
+++ copy.c	16 Mar 2004 04:25:46 -0000
@@ -104,8 +104,7 @@
 static const char *copy_relname;	/* table name for error messages */
 static int	copy_lineno;		/* line number for error messages */
 static const char *copy_attname;	/* current att for error messages */
-
-
+int co_rows_inserted;
 /*
  * These static variables are used to avoid incurring overhead for each
  * attribute processed.  attribute_buf is reused on each CopyReadAttribute
@@ -1271,7 +1270,8 @@
 	attr = tupDesc->attrs;
 	num_phys_attrs = tupDesc->natts;
 	attr_count = length(attnumlist);
-	num_defaults = 0;
+	num_defaults = 0; 
+
 
 	/*
 	 * We need a ResultRelInfo so we can use the regular executor's
@@ -1734,6 +1734,7 @@
 	ExecCloseIndices(resultRelInfo);
 
 	FreeExecutorState(estate);
+/*looks like we return success here*/
 }
 
 
@@ -1779,12 +1780,27 @@
 	 * of a frontend multibyte character couldn't look like ASCII characters.
 	 */
 	for (;;)
-	{
+	{               
+
 		c = CopyGetChar();
 		if (c == EOF)
 		{
 			result = true;
-			break;
+                     co_rows_inserted = copy_lineno;
+/*Now report number of rows imported to user*/
+                              
+                    if (co_rows_inserted == 0)
+                       {
+                           ereport(WARNING,(errcode(ERRCODE_BAD_COPY_FILE_FORMAT),
+                                            errmsg ("File used to import data is empty."),
+                                            errhint("Double check file name used")));
+                       }else if (co_rows_inserted < 0)
+                                {
+                                    ereport(ERROR,
+                                           (errcode(ERRCODE_BAD_COPY_FILE_FORMAT),                                                                                       errmsg("Rows copied were less than zero.  Copy function has been broken"),
+                                            errhint("File a bug please")));
+                                }
+                              break;
 		}
 		if (c == '\r')
 		{
Index: utility.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/tcop/utility.c,v
retrieving revision 1.210
diff -u -r1.210 utility.c
--- utility.c	10 Feb 2004 01:55:26 -0000	1.210
+++ utility.c	16 Mar 2004 04:28:07 -0000
@@ -60,7 +60,7 @@
 /*
  * Error-checking support for DROP commands
  */
-
+extern int co_rows_inserted;
 struct msgstrings
 {
 	char		kind;
@@ -1283,8 +1283,9 @@
 			tag = "COMMENT";
 			break;
 
-		case T_CopyStmt:
-			tag = "COPY";
+		case T_CopyStmt:	
+                        tag = "COPY";
+                        printf("COPY %i ROWS FROM FLAT FILE\n",co_rows_inserted );
 			break;
 
 		case T_RenameStmt:

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to