Uncover why the “SQLServer quota exceeded” error appears when PostgreSQL queries hit external SQL Server data sources and learn the quickest ways to clear space, raise limits, or optimize queries.
Foreign Data Wrappers (FDWs) like tds_fdw
expose remote SQL Server tables inside PostgreSQL. When a query returns more rows or bytes than the SQL Server-side result-set quota, SQL Server stops streaming and sends the error. PostgreSQL simply relays it.
Run a diagnostic query against the SQL Server catalog via the FDW connection:
SELECT quota_mb, used_mb
FROM svv_external_quotas -- SQL Server view name may vary
WHERE external_user = CURRENT_USER;
The result shows your megabyte limit and current usage.
You need SQL Server admin rights. Ask the DBA to execute:
EXEC sp_set_result_set_quota @login = 'pg_fdw_user', @quota_mb = 5000;
This increases the result limit to 5 GB for the PostgreSQL FDW login.
Stream fewer rows at once:
SET fetch_size = 1000; -- limits batches pulled from FDW
SET work_mem = '64MB'; -- caps local sort/hash buffers
The smaller batches keep the SQL Server side under quota.
Filter or aggregate before data leaves SQL Server. Push-down predicates and only return needed columns:
SELECT c.id, c.name, SUM(oi.quantity) AS total_items
FROM remote.orders o
JOIN remote.orderitems oi ON oi.order_id = o.id
JOIN remote.customers c ON c.id = o.customer_id
WHERE o.order_date > CURRENT_DATE - INTERVAL '30 days'
GROUP BY c.id, c.name;
The FDW converts this to a single remote query so SQL Server sends back only customer-level aggregates—well under quota.
Load data in chunks with a cursor:
BEGIN;
DECLARE cur CURSOR FOR
SELECT *
FROM remote.orders
ORDER BY id;
FETCH FORWARD 1000 FROM cur; -- repeat until done
COMMIT;
Each fetch stays below the remote limit.
SELECT col1, col2
rather than *
.WHERE
) and aggregations (GROUP BY
) so they execute on SQL Server.LIMIT … OFFSET
to page through large datasets.Pulling entire tables — Replace SELECT *
with explicit columns and conditions.
Ignoring FDW fetch size — Set fetch_size
< remote quota / row size.
No. The error comes from SQL Server; PostgreSQL just passes it through.
You can reduce fetch_size
, filter, aggregate, or page through data. To increase the limit, a SQL Server admin must change the quota.
Not necessarily. Optimizing queries and returning less data often performs better and costs nothing.