Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tYOvE-00Aa8X-Af for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2025 12:24:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tYOvC-00BvWS-6f for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2025 12:24:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tYOvB-00BvWG-S3 for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2025 12:24:26 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tYOv9-000krz-2q for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2025 12:24:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description; bh=7BzgIwIbzixys6ASiyYyCCqMVazW8Wm5YEt688UhRoc=; b=KV3J59dYMtpxRbxwJHct2Dxy73 Te04JOROkY+RZVAbWBpXvHQU8F/qdu/9+byDS/k7QEfqvi6EMZpw3fipF/lNhJ1wjM4qFXsdJ6HGk TSXMMnXUHKCDwADfR2Q6buU5lpMZQrQmnGsibxoqn5k/AqEKwHlkX1H9SxS9Ba6hiF7g=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1tYOv8-0033wD-2k; Thu, 16 Jan 2025 13:24:22 +0100 Date: Thu, 16 Jan 2025 13:24:22 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Wiwwo Staff Cc: pgsql-general@lists.postgresql.org Subject: Re: On enforcing default column value, AKA "Bloody nulls" Message-ID: Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Jan 16, 2025 at 12:14:54PM +0000, Wiwwo Staff wrote: > Hi all! > In a scenario like this: > > > =# create table tab1(text1 text default 'from table'); > > > > =# create procedure ins_tab1(p_text1 text default 'from proc') language > sql as > > -# $$ > > $# insert into tab1(text1) values (p_text1); > > $# $$; > > > > =# call ins_tab1 (null); > > > > =# select * from tab1 ; > > text1 > > -------- > > [nUlL] > > Is there a way (apart from triggers) to force at least one of the default > values? If you want default value, then don't provide null as value. insert into tab1 (other, columns) values (1,2) - will put default value in text1. Since you did provide/force value of null, default isn't used. If you want to list the column, then provide "value" of DEFAULT. like: insert into tab1(text1) values (DEFAULT); But, if you want to "insert NULL value, and have NULL get automagically converted to something else" - trigger is your only sane option. Defaults are used *only* in case of "no value for column was provided". Best regards, depesz