Brute-force the PG planner to make it use index scans instead of
sequential scans in the hold queue position stored proc. This resolves
a problem where the planner starts using seq scans over time as the
stats morph in the presence of lots of active, large-queue holds.
Avoidin the seq scan shows consistently better performance.
Signed-off-by: Bill Erickson <berickxx@gmail.com>
related_id INTEGER;
queue_pos INTEGER DEFAULT 1;
BEGIN
+ SET enable_seqscan = false;
FOR related_id IN SELECT id FROM (
SELECT ahr.id, ahr.cut_in_line, ahr.request_time
FROM action.hold_request ahr
ORDER BY COALESCE (ahr.cut_in_line, FALSE) DESC, ahr.request_time
) related_holds LOOP
IF related_id = hold_id THEN
+ RESET enable_seqscan;
RETURN queue_pos;
END IF;
queue_pos = queue_pos + 1;
END LOOP;
+ RESET enable_seqscan;
RETURN -1; -- No such hold
END;
$FUNK$ LANGUAGE PLPGSQL;