#!/usr/bin/php * @since 2007-09-23 * @copyright 2007 Insider Guides, Inc. * @license BSD */ // Default Maximum number of processes $processes = 15; // Usage example $usage = <<query("SELECT schemaname || '.' || tablename AS tablename FROM pg_tables WHERE hasindexes = 't' ORDER BY tablename;"); $temp = $query->fetchAll(PDO::FETCH_OBJ); // Build a simple array of the tablename column $tables = array(); foreach ( $temp AS $row ) $tables[] = $row->tablename; unset($temp); // Maximum # of tables for each thread $max = Count($tables) / $processes; // Loop through each thread and build an array of tables to process // Once that's set, fork and process them for ( $y = 0; $y < $processes; $y++ ) { // Build array of tables to process $process = array(); for ( $x = 0; $x < $max; $x++ ) $process[] = array_shift($tables); // Fork $pid = pcntl_fork(); if ( $pid == -1 ) { die("Could not fork\n"); } elseif ( $pid == 0 ) { // As the child process loop through and reindex the chunk of tables allocated to us $start = time(); $z = 0; foreach ( $process AS $table ) { if ( strlen($table) ) { $query = "REINDEX TABLE " . $table; echo "Executing " . $query . "\n"; $pdo->query($query); $z++; } } // Let the user know we're done and exit, otherwise we'll participate in the for ( $y ) loop echo "Process completed with $z tables reindexed in " . ( time() - $start ) . " seconds.\n"; exit(); } } ?>