Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjpvL-0007qH-Dx for pgsql-admin@arkaria.postgresql.org; Wed, 16 Jan 2019 18:28:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjpvK-0000JY-1O for pgsql-admin@arkaria.postgresql.org; Wed, 16 Jan 2019 18:28:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjpvJ-0000JN-Mk for pgsql-admin@lists.postgresql.org; Wed, 16 Jan 2019 18:28:21 +0000 Received: from mtaout001-public.msg.strl.va.charter.net ([68.114.190.26]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjpvG-000224-GW for pgsql-admin@postgresql.org; Wed, 16 Jan 2019 18:28:20 +0000 Received: from impout001 ([68.114.189.16]) by mtaout001.msg.strl.va.charter.net (InterMail vM.9.00.023.01 201-2473-194) with ESMTP id <20190116182817.ZMPF3845.mtaout001.msg.strl.va.charter.net@impout001>; Wed, 16 Jan 2019 12:28:17 -0600 Received: from cerberus.crazybean.net ([24.151.10.150]) by impout001 with charter.net id R6U91z0083EE3TL016U9mg; Wed, 16 Jan 2019 12:28:17 -0600 X-Authority-Analysis: v=2.2 cv=ffHd8wYF c=1 sm=1 tr=0 a=D2HTw/M6/8MmcHighGO7rg==:117 a=D2HTw/M6/8MmcHighGO7rg==:17 a=xqWC_Br6kY4A:10 a=3JhidrIBZZsA:10 a=bmULpPF2AAAA:8 a=4CTGBjFBAAAA:8 a=NLSvrRud5KlXwlssegcA:9 a=SxL9s4vVDqb9-VNz:21 a=0L0cmr-NX-5lwbeL:21 a=QEXdDO2ut3YA:10 a=UOcufxXEBWAA:10 a=Q8idokytMUgsDHlAeGwA:9 a=bKDJST14sRpgyUSo:21 a=ah3jt99K3r8P5dRt:21 a=Q4kHL_kcSqn1a3px:21 a=_W_S_7VecoQA:10 a=LwXMS3FhJVUSL8bnO8p6:22 a=beU-ecDrQBToTOaihgTF:22 Received: from [IPv6:2001:470:e117:4:9924:1441:4cd2:1a4c] (2001:470:e117:4:9924:1441:4cd2:1a4c [IPv6:2001:470:e117:4:9924:1441:4cd2:1a4c]) by cerberus.crazybean.net (OpenSMTPD) with ESMTPSA id bd35717a (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256:NO); Wed, 16 Jan 2019 13:28:08 -0500 (EST) From: Rui DeSousa Message-Id: <8B3FC027-0DC2-4A7B-9511-F6A06824B1BE@crazybean.net> Content-Type: multipart/alternative; boundary="Apple-Mail=_4D1A578D-8A7C-4D1C-ACE6-3AFA1C258E70" Mime-Version: 1.0 (Mac OS X Mail 12.2 \(3445.102.3\)) Subject: Re: extracting the domain from an email address Date: Wed, 16 Jan 2019 13:28:08 -0500 In-Reply-To: Cc: pgsql-admin To: Mark Steben References: X-Mailer: Apple Mail (2.3445.102.3) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_4D1A578D-8A7C-4D1C-ACE6-3AFA1C258E70 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Jan 16, 2019, at 8:20 AM, Mark Steben = wrote: >=20 > Good morning, > I am searching for a better more efficient way to extract the domain = portion of an email address. The two I have been using are very = expensive. > The one extract I mostly use: substring(email from '@(.*)$') > also has an index on the email column which is the full email = address: > btree (email DESC) WHERE email::text =3D = "substring"(email::text, '@(.*)$'::text) AND length(email::text) > 0, > The other extract: > substr(e.email,(strpos(e.email, '@') + 1)) > Currently has no index. >=20 > The referenced table has 72 million rows. The email column can be = empty (ergo the length =3D 0 check. I am running postgresql 9.4. >=20 > Any insights/observations welcome. > =20 > =20 Not sure of the exact problem you are trying to solve; is it just = querying the table via domain name? Does the index that you created = work? A few things; the regex given does not get domain name for some valid = emails addresses;=20 i.e. foo@bar@nowhere.com is a valid email; however, the regex = expression would return bar@nowhere.com instead = of nowhere.com . Assuming the emails are valid:=20 create or replace function get_domainname(_value text) returns text as $$ begin _value :=3D reverse(_value); return nullif(reverse(substring(_value, 0, strpos(_value, '@'))), ''); end; $$ language plpgsql immutable returns null on null input ; create index table_name_idx1 on table_name (get_domainname(email)); Then in the where clause:=20 where get_domainname(email) =3D 'nowhere.com' p.s. If you want to validate that the email address conforms to the RFC 2822 = you can use this function which makes use of Perl=E2=80=99s = Email::Address module. You would need to install plperl and = Email::Address module. create or replace function is_valid_email_address(eaddr text)=20 returns boolean=20 as=20 $body$=20 use Email::Address; return Email::Address->parse($_[0]); $body$ language 'plperlu' immutable ; --Apple-Mail=_4D1A578D-8A7C-4D1C-ACE6-3AFA1C258E70 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On Jan 16, 2019, at 8:20 AM, Mark Steben <mark.steben@drivedominion.com> wrote:

Good morning,
I am searching for a = better more efficient way to extract the domain portion of an email = address.  The two I have been using are very expensive.
   The one extract I mostly use:  substring(email from = '@(.*)$')
       also = has an index on the email column which is the full email = address:
            =   btree (email DESC) WHERE = email::text =3D "substring"(email::text, '@(.*)$'::text) AND = length(email::text) > 0,
      = The other extract:
  =     substr(e.email,(strpos(e.email, '@') + 1))
   Currently has no index.

The referenced table has 72 million rows.  The email = column can be empty (ergo the length =3D 0 check.  I am running = postgresql 9.4.

Any = insights/observations welcome.
          =   
 
<= br class=3D"">
Not sure of the exact problem you are = trying to solve; is it just querying the table via domain name? Does the = index that you created work?

A few = things; the regex given does not get domain name for some valid emails = addresses; 

i.e.  foo@bar@nowhere.com is a = valid email;  however, the regex expression would return bar@nowhere.com instead of nowhere.com.


Assuming the emails are = valid: 

create or replace function get_domainname(_value = text)
returns text
as = $$
begin
  _value :=3D = reverse(_value);

  return nullif(reverse(substring(_value, 0, = strpos(_value, '@'))), '');
end;
$$ language plpgsql
  immutable = returns null on null input
;

create index = table_name_idx1 on table_name (get_domainname(email));

Then in the where = clause: 

where get_domainname(email) =3D 'nowhere.com'


p.s.

If you want to validate that the email address conforms to = the RFC 2822 you can use this function which makes use of Perl=E2=80=99s = Email::Address module.  You would need to install plperl and = Email::Address module.

create or replace function = is_valid_email_address(eaddr text) 
  = returns boolean 
as 
$body$ 
  use = Email::Address;

  return Email::Address->parse($_[0]);
$body$
language 'plperlu' = immutable
;



= --Apple-Mail=_4D1A578D-8A7C-4D1C-ACE6-3AFA1C258E70--