Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
Despite the wording, this 53200 is usually not about RAM. Every table a transaction creates, drops or reads is locked until the transaction ends, and all those locks live in one shared lock table sized from max_locks_per_transaction (here 64) and max_connections (here 100). When a single transaction touches thousands of tables, the table fills up and you get out of shared memory with the HINT You might need to increase "max_locks_per_transaction".
Below, a DO block created and dropped 10,000 tables in one transaction. It failed after a few thousand tables (the CONTEXT line shows where). A procedure that COMMITs every 1,000 tables finished the same work without changing any setting.
01Creating thousands of tables in one transaction
The DO block loops CREATE TABLE sales_1 ... sales_10000. A DO block runs as one transaction, so locks pile up until it fails; the CONTEXT shows the statement it was on. Everything it had created was rolled back: the table count afterwards is 0.
We moved the loop into a procedure make_tables(n, batch) that runs COMMIT every batch tables, which releases the locks. CALL make_tables(10000, 1000) created all 10000 tables.
Setup
CREATE PROCEDURE drop_tables(n int, batch int) LANGUAGE plpgsql AS $$
BEGIN
FOR i IN 1..n LOOP
EXECUTE format('DROP TABLE IF EXISTS sales_%s', i);
IF i % batch = 0 THEN COMMIT; END IF;
END LOOP;
END $$;CREATE PROCEDUREDO $$
BEGIN
FOR i IN 1..10000 LOOP
EXECUTE format('CREATE TABLE sales_%s (id int, amount numeric)', i);
END LOOP;
END $$;ERROR: 53200: out of shared memory
HINT: You might need to increase "max_locks_per_transaction".
CONTEXT: SQL statement "CREATE TABLE sales_3242 (id int, amount numeric)"
PL/pgSQL function inline_code_block line 4 at EXECUTESHOW max_locks_per_transaction;
SHOW max_connections;
SELECT count(*) AS sales_tables FROM pg_tables WHERE schemaname = 'lab_a' AND tablename LIKE 'sales\_%'; max_locks_per_transaction
---------------------------
64
(1 row)
max_connections
-----------------
100
(1 row)
sales_tables
--------------
0
(1 row)max_locks_per_transaction is 64 and max_connections is 100, and the failed transaction left 0 tables behind.
CREATE PROCEDURE make_tables(n int, batch int) LANGUAGE plpgsql AS $$
BEGIN
FOR i IN 1..n LOOP
EXECUTE format('CREATE TABLE sales_%s (id int, amount numeric)', i);
IF i % batch = 0 THEN COMMIT; END IF;
END LOOP;
END $$;CREATE PROCEDURECALL make_tables(10000, 1000);
SELECT count(*) AS sales_tables FROM pg_tables WHERE schemaname = 'lab_a' AND tablename LIKE 'sales\_%';
CALL drop_tables(10000, 1000);CALL
sales_tables
--------------
10000
(1 row)
CALL02Dropping thousands of tables in one transaction
Dropping takes the same locks. With 10,000 tables in place, a DO block running DROP TABLE sales_1 ... sales_10000 failed the same way and dropped nothing.
A procedure drop_tables(n, batch) with COMMIT every 1,000 drops removed them all; the count went from 10000 to 0.
Setup
CREATE PROCEDURE make_tables(n int, batch int) LANGUAGE plpgsql AS $$
BEGIN
FOR i IN 1..n LOOP
EXECUTE format('CREATE TABLE sales_%s (id int, amount numeric)', i);
IF i % batch = 0 THEN COMMIT; END IF;
END LOOP;
END $$;
CALL make_tables(10000, 1000);CREATE PROCEDURE
CALLDO $$
BEGIN
FOR i IN 1..10000 LOOP
EXECUTE format('DROP TABLE sales_%s', i);
END LOOP;
END $$;ERROR: 53200: out of shared memory
HINT: You might need to increase "max_locks_per_transaction".
CONTEXT: SQL statement "DROP TABLE sales_2594"
PL/pgSQL function inline_code_block line 4 at EXECUTESELECT count(*) AS sales_tables FROM pg_tables WHERE schemaname = 'lab_a' AND tablename LIKE 'sales\_%'; sales_tables
--------------
10000
(1 row)All 10000 tables are still there: the failed DO block was rolled back as a whole.
CREATE PROCEDURE drop_tables(n int, batch int) LANGUAGE plpgsql AS $$
BEGIN
FOR i IN 1..n LOOP
EXECUTE format('DROP TABLE IF EXISTS sales_%s', i);
IF i % batch = 0 THEN COMMIT; END IF;
END LOOP;
END $$;CREATE PROCEDURECALL drop_tables(10000, 1000);
SELECT count(*) AS sales_tables FROM pg_tables WHERE schemaname = 'lab_a' AND tablename LIKE 'sales\_%';CALL
sales_tables
--------------
0
(1 row)FAQ
Is out of shared memory a RAM problem?
Not in these cases. The server had memory; the failure came from too many locks held by one transaction, and committing in batches fixed it with the same settings (max_locks_per_transaction = 64).
Should I raise max_locks_per_transaction?
That is what the HINT suggests, but we did not change it here because it only takes effect after a server restart. Splitting the work into several transactions was enough in both cases.
Can I COMMIT inside the loop?
Yes, from a procedure. Here CREATE PROCEDURE ... LANGUAGE plpgsql called with CALL (outside an explicit transaction) ran COMMIT every 1,000 tables.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12