错误:共享内存不足

Dmi*_*try 3 memory postgresql loops

我有一个查询,可以插入给定数量的测试记录。看起来像这样:

CREATE OR REPLACE FUNCTION _miscRandomizer(vNumberOfRecords int)
RETURNS void AS $$
declare
    -- declare all the variables that will be used
begin
    select into vTotalRecords count(*) from tbluser;
    vIndexMain := vTotalRecords;

    loop
        exit when vIndexMain >= vNumberOfRecords + vTotalRecords;

        -- set some other variables that will be used for the insert
        -- insert record with these variables in tblUser
        -- insert records in some other tables
        -- run another function that calculates and saves some stats regarding inserted records

        vIndexMain := vIndexMain + 1;
        end loop;
    return;
end
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

当我为300条记录运行此查询时,它将引发以下错误:

********** Error **********

ERROR: out of shared memory
SQL state: 53200
Hint: You might need to increase max_locks_per_transaction.
Context: SQL statement "create temp table _counts(...)"
PL/pgSQL function prcStatsUpdate(integer) line 25 at SQL statement
SQL statement "SELECT prcStatsUpdate(vUserId)"
PL/pgSQL function _miscrandomizer(integer) line 164 at PERFORM
Run Code Online (Sandbox Code Playgroud)

函数prcStatsUpdate看起来像这样:

CREATE OR REPLACE FUNCTION prcStatsUpdate(vUserId int)
RETURNS void AS
$$
declare
    vRequireCount boolean;
    vRecordsExist boolean;
begin
    -- determine if this stats calculation needs to be performed
    select into vRequireCount
        case when count(*) > 0 then true else false end
    from tblSomeTable q
    where [x = y]
      and [x = y];

    -- if above is true, determine if stats were previously calculated
    select into vRecordsExist
        case when count(*) > 0 then true else false end
    from tblSomeOtherTable c
    inner join tblSomeTable q
       on q.Id = c.Id
    where [x = y]
      and [x = y]
      and [x = y]
      and vRequireCount = true;

    -- calculate counts and store them in temp table
    create temp table _counts(...);
    insert into _counts(x, y, z)
    select uqa.x, uqa.y, count(*) as aCount
    from tblSomeOtherTable uqa
    inner join tblSomeTable q
       on uqa.Id = q.Id
    where uqa.Id = vUserId
      and qId = [SomeOtherVariable]
      and [x = y]
      and vRequireCount = true
    group by uqa.x, uqa.y;

    -- if stats records exist, update them; else - insert new
    update tblSomeOtherTable 
    set aCount = c.aCount
    from _counts c
    where c.Id = tblSomeOtherTable.Id
      and c.OtherId = tblSomeOtherTable.OtherId
      and vRecordsExist = true
      and vRequireCount = true;

    insert into tblSomeOtherTable(x, y, z)
    select x, y, z
    from _counts
    where vRecordsExist = false
      and vRequireCount = true;

    drop table _counts;
end;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)

看来该错误是由于某个地方建立了内存而导致的,但是由于我创建了临时表,因此立即使用它并放下(因此,据我了解,释放内存),我不知道怎么可能。

更新资料

我更新了prcStatsUpdate函数以表示我拥有的实际功能。我只是将表名和列名替换为通用名称。我没有第一次发布的原因是它主要是非常简单的sql操作,并且我认为它不会有任何问题。

另外,您从哪里开始进行行计数?它说错误在第25行,但是那不可能成立,因为where如果从头开始计算,则第25行是该子句中的条件。您从开始算起begin吗?

有任何想法吗?

Dan*_*ité 6

删除临时表后,直到事务结束时才释放锁。

看到相关的答案

如果可能,您应该重新组织代码以在函数外部创建临时表,然后在函数内部截断/填充它。