postgresql Powershell PG_转储脚本,RAM已达到最大值

42fyovps  于 2023-01-05  发布在  PostgreSQL
关注(0)|答案(2)|浏览(94)

我有一个powershell脚本,它循环遍历我们的postgres数据库,并在数据库上运行pg_dump。这个脚本写了一个sql转储文件。问题是它占用了我所有的可用内存。我想知道是否有一种方法可以优化它,这样就不会发生这种情况。

Powershell脚本:

$file = "output.csv"
$pguser = "postgres"

# start log file
Start-Transcript -Path "C:\transcripts\pg-backup.$(Get-Date -Format yyyy-MM-dd-hh-mm).transcript.txt"

# get password
Write-Host "Reading password file..."
$password = Get-Content "C:\Scripts\pg_pass.txt"

Write-Host "Password read."
$env:PGPASSWORD = $password

# get database names and put them in a csv
# Name,Size,etc
psql -U $pguser -l -A -F "," > $file

# remove first line 
get-content $file |
    select -Skip 1 |
    set-content "$file-temp"
move "$file-temp" $file -Force

$result = Import-Csv $file
Remove-Item $file

Write-Host "Databases queried: $($result.length)"

# Loop through each database name
# and dump it, upload it, delete it
ForEach($row in $result){
    Write-Host "Processing database $(1 + $result::IndexOf($result, $row)) of $($result.length)"
    $db = $row.Name

    # skip rows that aren't databases
    if(!$db.Contains('/') -and !$db.Contains(')')){

        Write-Host "Backing up: $($db)"
        $dumpfile = "$(Get-Date -Format yyyy-MM-dd-hh-mm).$($db).dump"

        # dump it 
        Write-Host "Creating Dump File: $dumpfile"
        pg_dump -U $pguser -F c $db > $dumpfile
        Write-Host "Dump File Created."

        # s3 it 
        Write-Host "Uploading to S3..."
        aws s3 cp $dumpfile s3://my-s3-bucket
        Write-Host "File Uploaded successfully."

        # delete it
        Write-Host "Removing dumpfile."
        Remove-Item $dumpfile
        Write-Host "File Removed."
    }
}

Stop-Transcript

我是如何运行的:

脚本:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe

论据:

-noprofile -NonInteractive -WindowStyle hidden –file C:\Scripts\pg-backup.ps1

我的成绩单显示:

**********************
Windows PowerShell transcript start
Start time: 20190904211002
Username: ****
RunAs User: *****
Machine: ***** (Microsoft Windows NT 10.0.14393.0)
Host Application: C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -NonInteractive -WindowStyle hidden –file C:\Scripts\pg-backup.ps1
Process ID: 5840
PSVersion: 5.1.14393.2636
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.2636
BuildVersion: 10.0.14393.2636
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\transcripts\pg-backup.2019-09-04-09-10.transcript.txt
Reading password file...
Password read.
Databases queried: 85
Processing database 1 of 85
Backing up: my_database
Creating Dump File: 2019-09-04-09-10.my_database.dump

最后,任务调度程序终止了这个进程,因为它挂起的时间太长了。

sf6xfgos

sf6xfgos1#

这是一个猜测,但我认为$文件相当大。
Writing it〉阅读it to remove one line〉writing it〉reading it会多次将其放入内存中,我会像这样处理它,以防止所有对象的复制:

psql -U $pguser -l -A -F "," | select-object -skip 1 | convertfrom-csv | foreach {
$db = $_.name
...

如果您仍然需要write-host行:

$result = (psql -U $pguser -l -A -F "," | select-object -skip 1 | convertfrom-csv)
Write-Host "Databases queried: $($result.count)"
foreach ($row in $result) {
    $db = $row.name
...

为命令分配变量或通过管道传输命令只是使命令的输出成为变量的内容(管道传输使其成为管道变量$_)。虽然我不使用postgresql,但我希望类似这样的东西能起作用。它将防止创建对象的多个副本,防止多次磁盘读写,并可能有助于内存使用。

qaxu7uf2

qaxu7uf22#

我找到了一个简单的解决方案。在PG文档中提到默认情况下pg_dump将内容复制到标准输出。我认为这是使用我所有RAM的原因,因为powershell可能在内存中缓存了整个DB转储。
它接受一个将转储到文件中的文件参数,这可以防止RAM问题,因为pg_dump直接将内容放入文件中。

-f file
--file=file
Send output to the specified file. If this is omitted, the standard output is used.

相关问题