Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYDFR-0006Ua-AM for pgsql-general@arkaria.postgresql.org; Mon, 11 May 2020 18:33:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jYDFQ-0006ct-8y for pgsql-general@arkaria.postgresql.org; Mon, 11 May 2020 18:33:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYDFP-0006cm-U7 for pgsql-general@lists.postgresql.org; Mon, 11 May 2020 18:33:52 +0000 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jYDFN-0006KO-3a for pgsql-general@lists.postgresql.org; Mon, 11 May 2020 18:33:50 +0000 Received: by mail-pf1-x441.google.com with SMTP id f7so5118693pfa.9 for ; Mon, 11 May 2020 11:33:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=WxMEyvUeQ8AWwJWepmLtrflEYC0Rt0b7diZc/hWZW/Y=; b=XtNs7vVMSAIvxr8ffpx0oPehkzFghs6KTXFDmE+ZvZnfQIt11DKeRWQVxynxhpQDn3 QwgScYQnxNnq2C+eWrzeHySebXkLBcswLUoCRYEsdNeGsHQ+yhMwLcqhy21SfuPJx9d7 LwTY3bmfHOiFAS5SX8EiWivXW0NDOzGbnxmb/MDVthPNo/KqMkLuS2dEwkBz85I/YOmQ qHOw4eWhRxEhulq8aZxMc98IpIpb+PP6692XepH6bTlAXLtZ+FIBa6rvpxdgqsxYV5Ty giafKW2OOdAPLOM7NjYDmDKmDJ+SPX8SD9T9wDV+9MRapAZ+uQuGB8NkETpEnH6Xopw+ AabA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=WxMEyvUeQ8AWwJWepmLtrflEYC0Rt0b7diZc/hWZW/Y=; b=rdh0QjflwsJBdBQ7kMkxkEseU+xwYw58vKY1kUsR/vP2rVHfqlRy1El1Qo8ppLLm5/ y/eSYSRRWBb5NvzAQeK5W7In4nNLZFzJ3N38WPHi6SUNfj76LBKm6ArCUHd2SIIpt4EK /C1qy6VIpnLM/qnASvtrIPztixQIx5trpdch05N5hjMydzoKtxIofwtbRPc8j29dGetq 99nbHAAgG0HCp+prnR0qFVkILMVlPqO/j6VI+SJbOpkkppe6+yKpFkULX0KXAmQon45e 9CVrssucOuHqEUBFH03lPX1SSILWPkSrWIinXh3PoyJM1/j9lswH9wMhpLTxBQunl/gj eakg== X-Gm-Message-State: AGi0PubNXV4+E7NS2mtnuXktk46ULAJiDyJ1w/Hn3gZiPh/tMZxJVcG3 wZRYsf3EfkGgJmDZ/FW4a6XFC5M+rpU= X-Google-Smtp-Source: APiQypJbLj8i3+ujViEPUQ2n9sNzngPb3uAnXQMenV3YHzTXUCxAyHdfzGaRYbdr6nSa+MupaTMBHg== X-Received: by 2002:a63:f54d:: with SMTP id e13mr16886711pgk.325.1589222027089; Mon, 11 May 2020 11:33:47 -0700 (PDT) Received: from [192.168.2.101] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id 9sm10784070pju.1.2020.05.11.11.33.46 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 11 May 2020 11:33:46 -0700 (PDT) Subject: Re: Enforcing uniqueness on [real estate/postal] addresses To: pgsql-general@lists.postgresql.org References: From: Paul Jungwirth Message-ID: <8f2d845c-e621-c650-6891-8503d0da8f49@illuminatedcomputing.com> Date: Mon, 11 May 2020 11:33:45 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/11/20 9:55 AM, Peter Devoy wrote: > Of course, if any of the fields are NULL (which they often are) I end > up with duplicates. > > One solution may be to add NOT NULL constraints and use empty strings > instead of NULL values but, until asking around today, I thought this was > generally considered bad practice. If you don't want to store empty strings (which I agree is a little yucky), you could replace NULLs with an empty string *only when checking for uniqueness*. To do this, first replace your unique constraint with a unique index, which gives you some additional features (e.g. indexing expressions, indexing only part of the table with a WHERE clause, building it concurrently, etc.). In this case we only care about indexing expressions. So you can say: CREATE UNIQUE INDEX is_unique_address ON properties ( COALESCE(description, ''), COALESCE(address_identifier_general, ''), COALESCE(street, ''), COALESCE(postcode, '')); Another approach, which I don't think is really a serious suggestion but is sort of interesting to think about: you could define an operator, say ===, that does the same thing as `IS NOT DISTINCT FROM`. Then you could create an exclusion constraint using that operator on all four columns. I've never tried that before but it seems like it would work. Maybe that's too much effort for something like this. I just think it's interesting because it feels like a use case for exclusion constraints that goes in the "opposite direction" of how they are usually used: instead of being less restrictive than =, it is more restrictive. Regards, -- Paul ~{:-) pj@illuminatedcomputing.com