Just FYI, refactored/revisited this patch for 20 in this thread:
https://www.postgresql.org/message-id/CAHM0NXia7YNC0nUvMfm4oD5FngopB8xeLsaZ7FFa5wMJ8ctk9Q%40mail.gmail.com