Skip to content

Conversation

@m3hm3t
Copy link
Contributor

@m3hm3t m3hm3t commented Dec 23, 2025

DESCRIPTION: PG18: Fix distributed MIN/MAX for arrays types

fixes #8400

error: #8400 (comment)

PostgreSQL 18 added/expanded min()/max() support to cover additional type categories (notably composite/record values, and other polymorphic signatures).

postgres/postgres@a0f1fce

In Citus, distributed aggregate planning needs to resolve the correct aggregate function OID for the worker-side aggregate based on (function name, input type). On PG18, that lookup can fail for polymorphic declarations (e.g., ANYARRAY, RECORD), resulting in errors like no matching oid for function: min on distributed tables.

What this PR changes

  • Make master aggregate return typing robust for polymorphic aggregates by treating all polymorphic return types via IsPolymorphicType(...) (instead of only ANYELEMENT). This ensures the master aggregate’s aggtype is set to the concrete worker return type.

  • Improve aggregate OID resolution by ranking candidate matches and picking the “best” one:

    • exact match
    • array-polymorphic match (ANYARRAY)
    • general polymorphic match (ANYELEMENT / ANYENUM)
    • record match (RECORD for rowtypes)

    This prevents the PG18 regressions where the old logic only handled exact + ANYELEMENT.

@m3hm3t m3hm3t self-assigned this Dec 23, 2025
@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.80%. Comparing base (80c2bce) to head (a7318e6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8421      +/-   ##
==========================================
+ Coverage   88.78%   88.80%   +0.02%     
==========================================
  Files         287      287              
  Lines       63257    63275      +18     
  Branches     7931     7933       +2     
==========================================
+ Hits        56163    56194      +31     
+ Misses       4758     4747      -11     
+ Partials     2336     2334       -2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@m3hm3t m3hm3t force-pushed the m3hm3t/pg18_minmax_array branch from 3806b4b to e960c5c Compare December 29, 2025 06:40
@m3hm3t m3hm3t changed the title M3hm3t/pg18 minmax array PG18: Fix distributed MIN/MAX for arrays and composite (record) types Dec 29, 2025
@m3hm3t m3hm3t force-pushed the m3hm3t/pg18_minmax_array branch 2 times, most recently from 88dd8dd to 49ed52e Compare December 29, 2025 07:02
@m3hm3t m3hm3t marked this pull request as ready for review December 29, 2025 07:14
@m3hm3t m3hm3t requested a review from naisila December 29, 2025 07:14
@m3hm3t m3hm3t changed the title PG18: Fix distributed MIN/MAX for arrays and composite (record) types PG18: Fix distributed MIN/MAX for arrays types Dec 30, 2025
@m3hm3t m3hm3t force-pushed the m3hm3t/pg18_minmax_array branch 2 times, most recently from 933d3bf to 8cec88d Compare December 31, 2025 08:06
@m3hm3t m3hm3t requested a review from onurctirtir December 31, 2025 08:15
Copy link
Member

@naisila naisila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some questions and recommendations for clarifications on this PR as it's a bit unclear to me what we are trying to do

Comment on lines +3563 to +3620
typedef enum AggregateArgMatchLevel
{
AGG_MATCH_NONE = 0,
AGG_MATCH_RECORD = 1,
AGG_MATCH_GENERAL_POLY = 2,
AGG_MATCH_ARRAY_POLY = 3,
AGG_MATCH_EXACT = 4
} AggregateArgMatchLevel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test case for each of these AggregateArgMatchLevel? In that way we can validate all cases and better understand the differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for

	AGG_MATCH_RECORD = 1,
	AGG_MATCH_GENERAL_POLY = 2,
	AGG_MATCH_ARRAY_POLY = 3,

I believe can not add for matach none.

records support will come with another PR after this one is merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record support is AGG_MATCH_RECORD, right ? So if that is tbd the tests in this PR cover AGG_MATCH_EXACT, AGG_MATCH_ARRAY_POLY and AGG_MATCH_GENERAL_POLY ?

Copy link
Contributor Author

@m3hm3t m3hm3t Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, may be a nit, but the list in the second message of this thread should read:

Added tests for

AGG_MATCH_GENERAL_POLY = 2
AGG_MATCH_ARRAY_POLY = 3
AGG_MATCH_EXACT = 4

@m3hm3t m3hm3t force-pushed the m3hm3t/pg18_minmax_array branch 2 times, most recently from 958b157 to 2719bad Compare January 5, 2026 08:34
@m3hm3t m3hm3t requested a review from naisila January 5, 2026 09:53
@m3hm3t m3hm3t requested a review from colm-mchugh January 5, 2026 16:44
@m3hm3t m3hm3t force-pushed the m3hm3t/pg18_minmax_array branch 3 times, most recently from caae21c to 42c04c6 Compare January 7, 2026 09:36
Copy link
Contributor

@colm-mchugh colm-mchugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm overall, couple of minor comments on macro usage and code flow.

m3hm3t added 2 commits January 7, 2026 18:24
…ORD types in distributed tables

PG18: Add tests for MIN/MAX aggregate OID resolution on ANYARRAY and RECORD types
@m3hm3t m3hm3t force-pushed the m3hm3t/pg18_minmax_array branch from abf7f31 to a7318e6 Compare January 7, 2026 18:24
@m3hm3t m3hm3t merged commit bfefc27 into main Jan 8, 2026
153 checks passed
@m3hm3t m3hm3t deleted the m3hm3t/pg18_minmax_array branch January 8, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow MIN()/MAX() aggregates on arrays and composite types

4 participants