How to Resolve “SQLServer Quota Exceeded” in PostgreSQL

Galaxy Glossary

How do I fix the “SQLServer quota exceeded” error while querying SQL Server from PostgreSQL?

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.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why does PostgreSQL show “SQLServer quota exceeded”?

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.

How can I check my current quota?

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.

Can I raise the SQL Server result-set quota?

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.

What PostgreSQL settings help avoid the error?

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.

How to trim result size with SQL?

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.

Is there a local workaround when I can’t change the 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.

Best practices to avoid “quota exceeded”

  • Always project only required columns via SELECT col1, col2 rather than *.
  • Push filters (WHERE) and aggregations (GROUP BY) so they execute on SQL Server.
  • Use cursors or LIMIT … OFFSET to page through large datasets.
  • Monitor remote-side quota consumption regularly.

Common mistakes and fixes

Pulling entire tables — Replace SELECT * with explicit columns and conditions.
Ignoring FDW fetch size — Set fetch_size < remote quota / row size.

Why How to Resolve “SQLServer Quota Exceeded” in PostgreSQL is important

How to Resolve “SQLServer Quota Exceeded” in PostgreSQL Example Usage


-- Pull last month’s high-value orders without tripping the quota
SET fetch_size = 2000;

SELECT o.id,
       c.name     AS customer_name,
       o.total_amount
FROM   remote.orders   o
JOIN   remote.customers c ON c.id = o.customer_id
WHERE  o.order_date >= date_trunc('month', CURRENT_DATE) - INTERVAL '1 month'
  AND  o.total_amount > 1000
ORDER  BY o.total_amount DESC;

How to Resolve “SQLServer Quota Exceeded” in PostgreSQL Syntax


-- PostgreSQL using tds_fdw to query SQL Server
CREATE SERVER mssql_srv
  FOREIGN DATA WRAPPER tds_fdw
  OPTIONS (servername 'sql.example.com', port '1433', database 'ShopDB');

CREATE USER MAPPING FOR CURRENT_USER
  SERVER mssql_srv
  OPTIONS (username 'pg_fdw_user', password 'secret');

IMPORT FOREIGN SCHEMA dbo
  LIMIT TO (Customers, Orders, Products, OrderItems)
  FROM SERVER mssql_srv INTO remote;

-- Query with limited columns and filters
SELECT o.id, o.order_date, p.name, oi.quantity
FROM   remote.orders      o
JOIN   remote.orderitems  oi ON oi.order_id = o.id
JOIN   remote.products    p  ON p.id = oi.product_id
WHERE  o.order_date >= CURRENT_DATE - INTERVAL '7 days'
FETCH FIRST 10000 ROWS ONLY;

Common Mistakes

Frequently Asked Questions (FAQs)

Does PostgreSQL itself have quotas?

No. The error comes from SQL Server; PostgreSQL just passes it through.

Can I solve the issue purely on the PostgreSQL side?

You can reduce fetch_size, filter, aggregate, or page through data. To increase the limit, a SQL Server admin must change the quota.

Is raising the quota always the best option?

Not necessarily. Optimizing queries and returning less data often performs better and costs nothing.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.