postgresql - PHP Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate XX bytes) -
i have php script splits large file , inserts postgresql. import has worked before on php 5.3 , postgresql 8.3 , mac os x 10.5.8. moved on new mac pro. has plenty of ram (16mb), mac os x 10.9.2, php 5.5.8, postgresql 9.3.
the problem when reading large import file. tab-separated file on 181 mb. have tried increase php memory 2gb (!) no more success. guess problem must in code reading text file , splitting it. error:
php fatal error: allowed memory size of 2097152000 bytes exhausted (tried allocate 72 bytes) in /library/filemaker server/data/scripts/getgbifdata.php on line 20
is there better way this? read file , split lines, again split each line \t (tab). error on line:
$arr = explode("\t", $line);
here code:
<?php ## have tried here, memory_limit in php.ini 256m ini_set("memory_limit","1000m"); $db= pg_connect('host=127.0.0.1 dbname=my_db_name user=username password=pass'); ### sett error_state: pg_set_error_verbosity($db, pgsql_errors_verbose); ### emtpy db $result = pg_query("truncate table my_db_name"); $fcontents = file ('///library/filemaker\ server/data/documents/export/export_file.tab'); for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode("\t", $line); $query = "insert my_db_name( field1, field2 etc.... ) values ( '{$arr[0]}','{$arr[1]}','{$arr[2]}','{$arr[3]}', etc........ )"; $result = pg_query($query); echo "\n lines:".$i; pg_send_query($db, $query); $res1 = pg_get_result($db); } ## update geometry column $sql = pg_query(" update darwincore2 set punkt_geom= st_setsrid(st_makepoint(my_db_name.longitude, darwincore2.latitude),4326); ");
?>
i think problem you're using file() function reads whole file in memory @ once. try reading line line using fopen , fgets.
$fp = fopen(filename, "r"); while (($line = fgets($fp)) !== false) { ... insert $line db.... } fclose($fp);
you can import file directly copy command (http://www.postgresql.org/docs/9.2/static/sql-copy.html)
Comments
Post a Comment