按属性匹配两个表的行

Matching rows of two tables by their attributes

有没有办法在普通 PL/pgSQL 中匹配实体? 假设我想通过使用文本搜索查询来搜索客户 table 中的一个人,该查询比较三个单一属性以确定匹配项:

    SELECT id, first_name, last_name, birthdate
    FROM customers_index
    WHERE first_name @@ plainto_tsquery('simple','John')
        AND last_name @@ plainto_tsquery('simple','Smith')
        AND birthdate = '17.08.1967';

但是对每个人都这样做有点乏味,因为我有整个 table 的人我想用这种方式匹配。在任何 "normal programming language" 的帮助下,我现在将循环 true 一个数组,直到我为每一行找到一个现有的匹配项作为新的 table,但是有没有办法简单地做到这一点PL/pgSQL?

我认为 PL/PGSQL 中的一个函数(类似于 Oracle 的 PL/SQL)结合数组作为输入参数会很好地工作:

CREATE OR REPLACE FUNCTION customers(first_names text[], last_names text[],
    birthdays date[])
  RETURNS SETOF customers_index as
$BODY$
DECLARE
  i integer;
  elements integer;
  rw customers_index%rowtype;
BEGIN

  elements := array_length (first_names);

  for i in 1..elements loop

    for rw in SELECT ci.*
      FROM customers_index ci
      WHERE ci.first_name @@ plainto_tsquery('simple', first_names[i])
        AND ci.last_name @@ plainto_tsquery('simple', last_names[i])
        AND ci.birthdate = birthdays[i]
    loop
      return next rw;
    end loop;
  end loop;

  return;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

实现看起来像这样:

select
  id, first_name, last_name, birthdate
from
  customers(array['John', 'Jane'], array['Smith', 'Doe'], 
      array ['17.08.1967', '16.07.1970'])

如果当然,假设您的数据来自其他地方,实际实施应该看起来更清晰。

此外,我不保证这是超级高效的,但它会是从 A 到 B 的快速路径,并且有一些 GIN 索引,或者至少是生日的索引,它实际上可能 运行 还不错。