Skip to content

Avoid order by on sub queries in postgresql reflection #8561

@CaselIT

Description

@CaselIT

I'm fairly sure that the order by we apply to subqueries in the postresql reflection, such as

select(
con_sq.c.conrelid,
con_sq.c.conname,
con_sq.c.description,
pg_catalog.pg_attribute.c.attname,
)
.select_from(pg_catalog.pg_attribute)
.join(
con_sq,
sql.and_(
pg_catalog.pg_attribute.c.attnum == con_sq.c.attnum,
pg_catalog.pg_attribute.c.attrelid == con_sq.c.conrelid,
),
)
.order_by(con_sq.c.conname, con_sq.c.ord)
.subquery("attr")
)
return (
select(
attr_sq.c.conrelid,
sql.func.array_agg(attr_sq.c.attname).label("cols"),
attr_sq.c.conname,
sql.func.min(attr_sq.c.description).label("description"),
)
.group_by(attr_sq.c.conrelid, attr_sq.c.conname)
.order_by(attr_sq.c.conrelid, attr_sq.c.conname)
)

may not correct, since a db can technically ignore it from what I gather.

I don't think this ever happened in our test, but it's probably better to use an alternative or get confirmation that the query is indeed correct. @zzzeek I can try asking to the postgresql mailing list for help if you don't have a better suggestion.

Since the use case is use array_agg and a group by we investigate ordering in the array_agg construct, using aggregate_order_by.
In this case we would need to make aggregate_order_by generate a cache key, since at the moment it's not able to.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpostgresqlreflectionreflection of tables, columns, constraints, defaults, sequences, views, everything else

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions