Bad query performance postgres 16

We are trying to make the step from postgres 10 to 16, we already tried a while ago to lower versions 12 or 13, but updates were added
in the machine and the poor performance we attribute to those updates.
Now we are testing on a new machine, and we have 2 virtual machines with the same resources and same configuration in postgres.conf.
The query in question is:
select numero, mov.id, mov.tipo_movimiento, mov.cliente_origen_id, mov.cliente_destino_id, mov.motivo_baja, mov.estado estado_movimiento, mov.tipo_notificacion, mov.fecha_movimiento, ( select i.id from industria i join clientesprecintos c on c.id_industria = i.id where c.id = mov.cliente_origen_id) as industria_origen_id, ( select i.id from industria i join clientesprecintos c on c.id_industria = i.id where c.id = mov.cliente_destino_id) as industria_destino_id
from (values (0,’13704147270′)) as precintos (position, numero)
left join lateral ( select mp.id, mp.estado , mpt.clave as tipo_movimiento, mp.cliente_origen_id, mp.cliente_destino_id, mpmb.clave as motivo_baja, tn.clave as tipo_notificacion, fecha_ini as fecha_movimiento
from movimiento_piezas_rangos mpr
join movimiento_piezas mp on mp.id = mpr.movimiento_piezas_id
left join notificaciones n on n.id_movimiento_piezas = mp.id
left join tipos_notificaciones tn on tn.id = n.id_tipo_notificacion
join movimiento_piezas_tipo mpt on mpt.id = mp.tipo_movimiento_id
left join movimiento_piezas_motivo_baja mpmb on mpmb.id = mp.motivo_baja_id
where numero::bigint between mpr.desde and mpr.hasta and mp.estado not in (‘ANULADO’, ‘RECHAZADO’)
order by mp.fecha_ini desc limit 1 ) as mov on true
order by precintos.position;

On the machine with postgres 10, the response time is approximately 750 ms.
On the machine with postgres 16, the response time goes to 4 minutes.

Reviewed documentation, it seems that there are changes from version 12.
We have made changes to the query until we found a more optimal solution for v16:
Select numero, mov.id, mov.tipo_movimiento, mov.cliente_origen_id, mov.cliente_destino_id, mov.motivo_baja, mov.estado estado_movimiento, mov.tipo_notificacion, mov.fecha_movimiento, (select i.id from industria i join clientesprecintos c on c.id_industria = i.id where c.id = mov.cliente_origen_id) as industria_origen_id, (select i.id from industria i join clientesprecintos c on c.id_industria = i.id where c.id = mov.cliente_destino_id) as industria_destino_id
from (values (0,’15727625912′)) as precintos (position, numero)
left join lateral (
select mp.id, mp.estado , mp.tipo_movimiento, mp.cliente_origen_id, mp.cliente_destino_id, mp.motivo_baja, tn.clave as tipo_notificacion, mp.fecha_movimiento
from
(WITH mp_consulta AS (
select mp.id, mp.estado , mpt.clave as tipo_movimiento, mp.cliente_origen_id, mp.cliente_destino_id, mpmb.clave as motivo_baja, fecha_ini as fecha_movimiento
from movimiento_piezas_rangos mpr
join movimiento_piezas mp on mp.id = mpr.movimiento_piezas_id
join movimiento_piezas_tipo mpt on mpt.id = mp.tipo_movimiento_id
left join movimiento_piezas_motivo_baja mpmb on mpmb.id = mp.motivo_baja_id
where
numero::bigint between mpr.desde and mpr.hasta and mp.estado NOT IN (‘ANULADO’, ‘RECHAZADO’))
SELECT * FROM (
SELECT * FROM mp_consulta
UNION ALL
SELECT * FROM mp_consulta ORDER BY fecha_movimiento DESC LIMIT 1
) AS union_mp
ORDER BY fecha_movimiento DESC LIMIT 1) mp
left join notificaciones n on n.id_movimiento_piezas = mp.id
left join tipos_notificaciones tn on tn.id = n.id_tipo_notificacion
) as mov on true
order by precintos.position;

We have seen

  • that order by and limit can lead to bad strategies, they suggest applying + 0, in our case increasing the date, but we don’t see improvements either.
  • that the pg_hint_plan extension can be added, but the tests have not been satisfactory, possibly due to lack of knowledge on our part.

If the change were for a single query there would be no problem, but it is a large application, where we do not know how many queries we may encounter
with such a different and bad performance, or if it is the same pattern.

Initially it seems that it is some scheduler configuration, which would allow good results to be obtained without changing the SQL queries.

New contributor

Víctor Martín Aguilar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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