What causes a row estimate declared on a custom function to sometimes be ignored?

I have created the following custom SQL function on a PostgreSQL 16.1 server:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE OR REPLACE FUNCTION public.generate_series_monthly(a date, b date)
RETURNS SETOF date
LANGUAGE SQL
IMMUTABLE PARALLEL SAFE
ROWS 10
AS
$function$
select generate_series(date_trunc('month', a), date_trunc('month', b), '1 month')
$function$;
</code>
<code>CREATE OR REPLACE FUNCTION public.generate_series_monthly(a date, b date) RETURNS SETOF date LANGUAGE SQL IMMUTABLE PARALLEL SAFE ROWS 10 AS $function$ select generate_series(date_trunc('month', a), date_trunc('month', b), '1 month') $function$; </code>
CREATE OR REPLACE FUNCTION public.generate_series_monthly(a date, b date)
RETURNS SETOF date
LANGUAGE SQL
IMMUTABLE PARALLEL SAFE
ROWS 10
AS
$function$
select generate_series(date_trunc('month', a), date_trunc('month', b), '1 month')
$function$;

Specifically, I have added the row estimate parameter, and as expected, I am seeing this estimate in some simple queries:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># explain select generate_series_monthly('2024-01-01', '2024-05-01'); QUERY PLAN
------------------------------------------------
ProjectSet (cost=0.00..0.09 rows=10 width=4)
-> Result (cost=0.00..0.01 rows=1 width=0)
(2 rows)
</code>
<code># explain select generate_series_monthly('2024-01-01', '2024-05-01'); QUERY PLAN ------------------------------------------------ ProjectSet (cost=0.00..0.09 rows=10 width=4) -> Result (cost=0.00..0.01 rows=1 width=0) (2 rows) </code>
# explain select generate_series_monthly('2024-01-01', '2024-05-01');                                                                                                                                                                                                                                       QUERY PLAN
------------------------------------------------
 ProjectSet  (cost=0.00..0.09 rows=10 width=4)
   ->  Result  (cost=0.00..0.01 rows=1 width=0)
(2 rows)

However, in some uses in queries, I see it falling back to the default of 1000:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># explain select * from generate_series_monthly('2024-01-01', '2024-05-01');
QUERY PLAN
-------------------------------------------------------
Result (cost=0.00..30.03 rows=1000 width=4)
-> ProjectSet (cost=0.00..5.03 rows=1000 width=8)
-> Result (cost=0.00..0.01 rows=1 width=0)
(3 rows)
</code>
<code># explain select * from generate_series_monthly('2024-01-01', '2024-05-01'); QUERY PLAN ------------------------------------------------------- Result (cost=0.00..30.03 rows=1000 width=4) -> ProjectSet (cost=0.00..5.03 rows=1000 width=8) -> Result (cost=0.00..0.01 rows=1 width=0) (3 rows) </code>
# explain select * from generate_series_monthly('2024-01-01', '2024-05-01');
                      QUERY PLAN
-------------------------------------------------------
 Result  (cost=0.00..30.03 rows=1000 width=4)
   ->  ProjectSet  (cost=0.00..5.03 rows=1000 width=8)
         ->  Result  (cost=0.00..0.01 rows=1 width=0)
(3 rows)

I would expect this second query to also use the 10 row estimate. Why is it resorting to 1000?

It’s because of inlining.

Scalar and table functions have different criteria for inlining – calling your table function as a table function (your second example) meets all of them. Explain verbose can show how the query got rewritten – generate_series_monthly() is gone, replaced by its internal generate_series() so the planner only uses estimates attached to generate_series(), not the _monthly wrapper:
demo at db<>fiddle

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>explain verbose select *
from generate_series_monthly('2024-01-01', '2024-05-01');
</code>
<code>explain verbose select * from generate_series_monthly('2024-01-01', '2024-05-01'); </code>
explain verbose select * 
  from generate_series_monthly('2024-01-01', '2024-05-01');
-> ProjectSet (cost=0.00..5.03 rows=1000 width=8)
Output: generate_series(date_trunc(‘month’::text, (‘2024-01-01’::date)::timestamp with time zone), date_trunc(‘month’::text, (‘2024-05-01’::date)::timestamp with time zone), ‘1 mon’::interval)

According to the db, you’re just calling generate_series() directly. You can check its associated row estimate in pg_catalog.pg_proc:

prorows float4
Estimated number of result rows (zero if not proretset)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>select prorows
from pg_proc
where oid='generate_series(timestamptz,timestamptz,interval)'::regprocedure;
</code>
<code>select prorows from pg_proc where oid='generate_series(timestamptz,timestamptz,interval)'::regprocedure; </code>
select prorows 
from pg_proc 
where oid='generate_series(timestamptz,timestamptz,interval)'::regprocedure;
prorows
1000

Which is the default:

ROWS result_rows
A positive number giving the estimated number of rows that the planner should expect the function to return. This is only allowed when the function is declared to return a set. The default assumption is 1000 rows.

The select generate_series_monthly(...); example is a scalar function call, which fails to meet the 3rd condition on the list:

A scalar function call will be inlined if all of the following conditions are met (Source code):

  • the function is LANGUAGE SQL
  • the function is not SECURITY DEFINER
  • the function is not RETURNS SETOF (or RETURNS TABLE)

Explain verbose shows the call is left unaltered and you get to keep your custom estimate:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>explain verbose select generate_series_monthly('2024-01-01', '2024-05-01');
</code>
<code>explain verbose select generate_series_monthly('2024-01-01', '2024-05-01'); </code>
explain verbose select generate_series_monthly('2024-01-01', '2024-05-01');
ProjectSet (cost=0.00..0.32 rows=10 width=4)
Output: generate_series_monthly(‘2024-01-01’::date, ‘2024-05-01’::date)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật