postgresql - psql 启动多个并发 .sql 脚本并限制进程

标签 postgresql powershell psql

背景: 操作系统 Win10 postgresql 9.6 邮政地理信息系统 2.5

故事: 我在 win 文件夹中有 200 个带有 postgres(st_union 函数)查询的 .slq 脚本。 我可以在这些文件上并行运行“for %%f in (*.sql) do (start cmd/c "psql -U postgres -f "%%f"abn2030")".bat,我的 8 个内核有一些饲料。 除了 200 个脚本太多了。我正在寻找一种方法来限制正在运行的“cmd”/“psql”实例。我想检查正在运行的 cmd(或 psql)实例,并仅在 cmd/psql 实例数低于 15 时才启动下一个(排队等候).sql 脚本。(如果这有意义;也许还有其他选择) . 我尝试了 powershell 和 python(范围较小),但我不知道继续使用哪一个(如果有的话)。

电源外壳: 直接运行。脚本执行没有问题。

cmd /c ""C:\PostgreSQL\9.6\bin\psql.exe" -U postgres -d db -f "D:\Dropbox\scripts_sql\execute_htunion\pwshl\queries1.SQL""

在脚本中:(主要从另一篇文章中窃取)

function numInstances([string]$process)
{
@(get-process -ea silentlycontinue $process).count
}

$files = Get-ChildItem $scriptDir\*.sql

foreach ($file in $files) {
$running = numInstances("cmd")
if ($running.Count -le 2) {
$script= {& cmd /c psql "-U postgres -f $file db"}
    Start-Job $script
         write-host "$file"
} else {
     $running | Wait-Job
}
Get-Job | Receive-Job
}

输出:psql: FATAL: 角色“postgres -f D:\Dropbox\scripts_sql\execute_htunion\pwshl\queri”不存在

如何在 powershell(或其他语言)的 psql 命令中发送 start .sql?

编辑:
在 .bat 中解决: "C:\Program Files\Git\bin\sh.exe"--login -i -c "find . -iname '*.sql' -print0 | xargs --null --max-procs=14 -I {} psql.exe -U postgres -d db -f {}"

最佳答案

Git for Windows应该有 xargsfind ,这些是源自 Linux 的命令,它们串联起来非常适合该任务:

find . -name '*.sql' -print0 \
| xargs --null --max-procs=16 -I{} psql.exe -U postgres -d db -f {}

关于postgresql - psql 启动多个并发 .sql 脚本并限制进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54263730/

相关文章:

arrays - 使用包含标志的值数组(splat)调用cmdlet

powershell - 代码中无法识别 Get-WmiObject

macos - 在 macOS 上安装没有完整 Postgres 的 psql 的正确方法?

ruby-on-rails - Heroku Postgresql SET datestyle 到欧洲

ruby-on-rails - Docker:Docker-compose ruby​​ + postgres - PG::ConnectionBadcould not connect to server

php - 存储/检索 PGP 私钥和密码的安全方法?

postgresql - 无法使用 pg_restore 为 Postgres 恢复转储备份文件

mysql - 如何在 Laravel Eloquent ORM 中使用数据库特定函数进行模块化使用

powershell - PowerShell 比较运算符背后的推理

python - 在 fabric 脚本的 psql 命令中转义引号