斉藤 登です。

postgres にあった 8192 バイト制限の対処をしてみました。
postgres のコードに触るのは久々でけっこう不安だったりするので、
とりあえずこちらに。

ツッコミ歓迎。

--- postgres.c.org	Fri Jun 30 04:58:02 2000
+++ postgres.c	Sun Dec 17 20:10:49 2000
@@ -20,8 +20,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#define MAX_BUFFER_SIZE   8192           /* maximum transaction size */
-
 #ifndef NO_CLENCODE
 #define CLIENTENCODING    1              /* enable client encoding */
 #endif
@@ -237,7 +235,7 @@ pgconn_insert_table(obj, table, values)
 {
     PGconn *conn = get_pgconn(obj);
     PGresult *result;
-    char *buffer;
+    VALUE s, buffer;    
     int i, j;
     int res = 0;
 
@@ -249,28 +247,28 @@ pgconn_insert_table(obj, table, values)
 	    rb_raise(rb_ePGError, "second arg must contain some kind of arrays.");
 	}
     }
-    buffer = ALLOCA_N(char,MAX_BUFFER_SIZE);
-
+    
+    buffer = rb_str_new(0, RSTRING(table)->len + 17); 
     /* starts query */
-    sprintf(buffer, "copy %s from stdin ", RSTRING(table)->ptr);
+    sprintf(RSTRING(buffer)->ptr, "copy %s from stdin ", RSTRING(table)->ptr);
     
-    result = PQexec(conn, buffer);
+    result = PQexec(conn, RSTRING(buffer)->ptr);
     if (!result){
 	rb_raise(rb_ePGError, PQerrorMessage(conn));
     }
     PQclear(result);
 
-    for (i=0; i<RARRAY(values)->len; i++) {
+    for (i = 0; i < RARRAY(values)->len; i++) {
 	struct RArray *row = RARRAY(RARRAY(values)->ptr[i]);
-        buffer[0] = 0;
-	for (j=0; j<row->len; j++) {
-	    VALUE s = rb_obj_as_string(row->ptr[j]);
-	    if (j > 0) strncat(buffer, "\t", MAX_BUFFER_SIZE-strlen(buffer));
-	    strncat(buffer, RSTRING(s)->ptr, MAX_BUFFER_SIZE-strlen(buffer));
+        buffer = rb_tainted_str_new(0,0);
+	for (j = 0; j < row->len; j++) {
+	    s = rb_obj_as_string(row->ptr[j]);
+	    if (j > 0) rb_str_cat(buffer, "\t", 1);
+	    rb_str_cat(buffer, RSTRING(s)->ptr, strlen(RSTRING(s)->ptr));
 	}
-	strncat(buffer, "\n\0", MAX_BUFFER_SIZE-strlen(buffer));
+	rb_str_cat(buffer, "\n\0", 2);
 	/* sends data */
-	PQputline(conn, buffer);
+	PQputline(conn, RSTRING(buffer)->ptr);
     }
     PQputline(conn, "\\.\n");
     res = PQendcopy(conn);
@@ -290,16 +288,28 @@ pgconn_putline(obj, str)
 static VALUE
 pgconn_getline(obj)
     VALUE obj;
-{
+{    
     PGconn *conn = get_pgconn(obj);
-    char *line = ALLOCA_N(char,MAX_BUFFER_SIZE);
+    VALUE str;
+    long siz = BUFSIZ;
+    long bytes = 0;
+    int	 ret;
+    
+    str = rb_tainted_str_new(0, siz);
 
-    switch (PQgetline(conn, line, MAX_BUFFER_SIZE)) {
-      case 0:
-	return rb_tainted_str_new2(line);
-      case 1:
-	rb_raise(rb_ePGError, "buffer overflow");
-	break;
+    for (;;) {
+      ret = PQgetline(conn, RSTRING(str)->ptr + bytes, siz - bytes);
+      switch (ret) {
+	case EOF:
+	  return Qnil;
+        case 0:
+	  return str;
+        case 1:
+	  break;
+      }
+      bytes += BUFSIZ;
+      siz += BUFSIZ;
+      rb_str_resize(str, siz);
     }
     return Qnil;
 }