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 1ghNDH-0007QC-9v for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Jan 2019 23:24:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ghNDG-0002bM-0L for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Jan 2019 23:24:42 +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 1ghNDF-0002VN-CC for pgsql-hackers@lists.postgresql.org; Wed, 09 Jan 2019 23:24:41 +0000 Received: from mail-yb1-xb41.google.com ([2607:f8b0:4864:20::b41]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ghNDB-0005LI-DZ for pgsql-hackers@postgresql.org; Wed, 09 Jan 2019 23:24:40 +0000 Received: by mail-yb1-xb41.google.com with SMTP id t16so3700189ybk.10 for ; Wed, 09 Jan 2019 15:24:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=from:subject:to:cc:references:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=BVO3l3cQwYVD8BXFbFig3pySpMecikoMUs9VzRQX7l8=; b=qxSwTw9uIhSCXPgP73ObozBbMKrGWhvxvnj9Fc7h5+U//iseeRmlRc0RcA2wh7anny mbLo373IYPBiLHxVIn1fHRWlVISYnpSoe7W9qRwrqTK2yXJYgDAXEb2gueksk6WbhAkx WXi0HaA2wdg1+OmlfmsE1Ugt1Tq6GmHgQADllzptLI7rYaSKaRCrn+UKhuafS4q8Qja6 RpOgqUgViO05j85hkxPiHdIYQZnIbEP5P5btfrIy+gyLnC+AyC10TwqIBxoi4jwOXGA/ PvHHIgmqY6p85WPl4MBq7E5CLXTgy7LsXDG23RbxGpgAabRKoarKmOyHFCtSbMo1rC6I MP5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language; bh=BVO3l3cQwYVD8BXFbFig3pySpMecikoMUs9VzRQX7l8=; b=gP2TFKMBEnghdFujfVORGFL84WbtQh5fTEkx64pQ1FCc5SdSobLUVoBiIEKj83C+0D fw3ikoJ+yDH6Mr5YVV4A1JmFl+pjqib0Zzj+nfNrIOhDsZpRkqKOf/gChl1ZVwP/a5Hm OXIgWS3WAZr2FE6OjP27ZMBjBZWJ1S6cs1/cG3mj6Y9aLXpa/iLOdbKilow9c7NztVdJ 0I+L1JJRVtqYE/BdREMSRxfY6jA4JDBoHUWqrrtrZwvxto8CjEyA8etpfW+/lXKNVbxL sNvgb46TkZFwSs1ZbgcuXifYTv9FA7Tdf90MhI6x4gRStCh5Nn93IZNuihY7zg6S2Vpv vxtA== X-Gm-Message-State: AJcUukfxSp0MhiYCTar7i+ap0fI76vsmvtgIoIdsI3dm7ghFESoGVJQG QigYj/SqJfTqVwcLC2hocqA97i8g1Htck80aP5Pm1IsWBkXYJw7gy1VakLar9rl065amP7zszfX xvDpTKcWlqppF2rv1lLJgm+ctW59+A7JY+urCifUpr+mAhEDz06FrT7ph6KMHgbSAjna1R+5PCd iIiMqpWIi8M6uT X-Google-Smtp-Source: ALg8bN42P4gUgp0Lgt22CmQORpSGtoL0mjFuvZfK/Cr1sqhyd5k7iIUm4ebEPN+KcE7HC+JM8MxvfA== X-Received: by 2002:a25:3810:: with SMTP id f16mr7511651yba.237.1547076275897; Wed, 09 Jan 2019 15:24:35 -0800 (PST) Received: from [192.168.10.146] ([98.122.175.38]) by smtp.gmail.com with ESMTPSA id g7sm24568348ywe.16.2019.01.09.15.24.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Jan 2019 15:24:34 -0800 (PST) From: Andrew Dunstan X-Google-Original-From: Andrew Dunstan Subject: Re: BUG #15446: Crash on ALTER TABLE To: Dean Rasheed Cc: Andres Freund , Tom Lane , Dmitry Molotkov , Alvaro Herrera , PostgreSQL-development References: <20181027144557.6uil4sqrvor25m3x@alvherre.pgsql> <20190104211700.jloztzladojvpxij@alap3.anarazel.de> <20190104222542.5zcrjv5p24ssw7te@alap3.anarazel.de> <14358.1546648708@sss.pgh.pa.us> <20190105170940.2qrbilpxmmxyf4tr@alap3.anarazel.de> <1f382801-d55c-bc0b-b632-0731867091e4@2ndQuadrant.com> <9e4caaa2-e2af-fd7f-e864-ae67e9560579@2ndQuadrant.com> Openpgp: preference=signencrypt Autocrypt: addr=andrew.dunstan@2ndQuadrant.com; prefer-encrypt=mutual; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SGY+FZv441chh5SU92v4q6z+6LPpoHOh97ptAVXZYNTtU0LevyvD5lja0TzbvJm6C eFXitJfnm1pLEr0DGJCR/iUOl/N62Kh4855zZC7NHIjQHPOvV5Stz/l5ilDhvGVk+xkXFPys SjZoUr1rXhYLpiyi5sR0X9FHXT0KnGuz1F5ERO7ZTLSSQ6fJwPj6gOk9K+vvoKvoeql5ABEB AAHNL0FuZHJldyBEdW5zdGFuIDxhbmRyZXcuZHVuc3RhbkAybmRRdWFkcmFudC5jb20+wsCY BBMBAgBCAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAUJDyV6hBYhBOQ+WEYd/Hy/RGkV pZn6f8tZ/DuBBQJcH/EVAhkBAAoJEJn6f8t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1Oo9mIHFakgWgOBMuxoWfjONiLjjLh/9+ZIqSSLX Message-ID: Date: Wed, 9 Jan 2019 18:24:33 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: <9e4caaa2-e2af-fd7f-e864-ae67e9560579@2ndQuadrant.com> Content-Type: multipart/mixed; boundary="------------222B8EC15F7F2E6E918F3C48" Content-Language: en-MW List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------222B8EC15F7F2E6E918F3C48 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 1/8/19 7:41 PM, Andrew Dunstan wrote: > On 1/8/19 4:48 PM, Dean Rasheed wrote: >> On Tue, 8 Jan 2019 at 19:34, Andrew Dunstan >> wrote: >>> Here's a patch that I think cures the problem. >>> >> Hmm, that doesn't quite work because the table might not actually be >> rewritten as a result of the type change. For example: >> >> DROP TABLE IF EXISTS foo; >> CREATE TABLE foo (a int); >> INSERT INTO foo VALUES (1); >> ALTER TABLE foo ADD COLUMN b varchar(10) DEFAULT 'xxx'; >> ALTER TABLE foo ALTER COLUMN b SET DEFAULT 'yyy'; >> INSERT INTO foo VALUES (2); >> SELECT * FROM foo; >> a | b >> ---+----- >> 1 | xxx >> 2 | yyy >> (2 rows) >> >> ALTER TABLE foo ALTER COLUMN b TYPE varchar(20) USING b::varchar(20); >> SELECT * FROM foo; >> a | b >> ---+----- >> 1 | >> 2 | yyy >> (2 rows) >> > > Ouch, OK, looks like we need something more complex. > > Here's another attempt. For the rewrite case it kept the logic of the previous patch to clear all the missing attributes, but if we're not rewriting we reconstruct the missing value according to the new type settings. cheers andrew -- Andrew Dunstan https://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------222B8EC15F7F2E6E918F3C48 Content-Type: text/x-patch; name="fix-alter-column-type-missing-value-3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-alter-column-type-missing-value-3.patch" diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c8c50e8c98..e72d0b43ed 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -9285,6 +9285,21 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel, HeapTuple depTup; ObjectAddress address; + /* + * Clear all the missing values if we're rewriting the table, since this + * renders them pointless. + */ + if (tab->rewrite) + { + Relation newrel; + + newrel = heap_open(RelationGetRelid(rel), NoLock); + RelationClearMissing(newrel); + relation_close(newrel, NoLock); + /* make sure we don't conflict with later attribute modifications */ + CommandCounterIncrement(); + } + attrelation = heap_open(AttributeRelationId, RowExclusiveLock); /* Look up the target column */ @@ -9601,7 +9616,69 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel, /* * Here we go --- change the recorded column type and collation. (Note * heapTup is a copy of the syscache entry, so okay to scribble on.) + * First fix up the missing value if any. */ + if (attTup->atthasmissing) + { + Datum missingval; + bool missingNull; + + /* if rewrite is true the missing value should already be cleared */ + Assert(tab->rewrite == 0); + + /* Get the missing value datum */ + missingval = heap_getattr(heapTup, + Anum_pg_attribute_attmissingval, + attrelation->rd_att, + &missingNull); + + /* if it's a null array there is nothing to do */ + + if (! missingNull) + { + /* + * Get the datum out of the array and repack it in a new array + * built with the new type data. We assume that since the table + * doesn't need rewriting, the actual Datum doesn't need to be + * changed, only the array metadata. + */ + + int one = 1; + bool isNull; + Datum valuesAtt[Natts_pg_attribute]; + bool nullsAtt[Natts_pg_attribute]; + bool replacesAtt[Natts_pg_attribute]; + + MemSet(valuesAtt, 0, sizeof(valuesAtt)); + MemSet(nullsAtt, false, sizeof(nullsAtt)); + MemSet(replacesAtt, false, sizeof(replacesAtt)); + + missingval = array_get_element(missingval, + 1, + &one, + 0, + attTup->attlen, + attTup->attbyval, + attTup->attalign, + &isNull); + missingval = PointerGetDatum( + construct_array(&missingval, + 1, + targettype, + tform->typlen, + tform->typbyval, + tform->typalign)); + + valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval; + replacesAtt[Anum_pg_attribute_attmissingval - 1] = true; + nullsAtt[Anum_pg_attribute_attmissingval - 1] = false; + + heapTup = heap_modify_tuple(heapTup, RelationGetDescr(attrelation), + valuesAtt, nullsAtt, replacesAtt); + attTup = (Form_pg_attribute) GETSTRUCT(heapTup); + } + } + attTup->atttypid = targettype; attTup->atttypmod = targettypmod; attTup->attcollation = targetcollid; diff --git a/src/test/regress/expected/fast_default.out b/src/test/regress/expected/fast_default.out index 1c1924cd5c..40a15bd2d6 100644 --- a/src/test/regress/expected/fast_default.out +++ b/src/test/regress/expected/fast_default.out @@ -735,7 +735,44 @@ INSERT INTO leader VALUES (1, 1), (2, 2); ALTER TABLE leader ADD c int; ALTER TABLE leader DROP c; DELETE FROM leader; +-- check that ALTER TABLE ... ALTER TYPE does the right thing +CREATE TABLE vtype( a integer); +INSERT INTO vtype VALUES (1); +ALTER TABLE vtype ADD COLUMN b DOUBLE PRECISION DEFAULT 0.2; +ALTER TABLE vtype ADD COLUMN c BOOLEAN DEFAULT true; +SELECT * FROM vtype; + a | b | c +---+-----+--- + 1 | 0.2 | t +(1 row) + +ALTER TABLE vtype + ALTER b TYPE text USING b::text, + ALTER c TYPE text USING c::text; +NOTICE: rewriting table vtype for reason 4 +SELECT * FROM vtype; + a | b | c +---+-----+------ + 1 | 0.2 | true +(1 row) + +-- also check the case that doesn't rewrite the table +CREATE TABLE vtype2 (a int); +INSERT INTO vtype2 VALUES (1); +ALTER TABLE vtype2 ADD COLUMN b varchar(10) DEFAULT 'xxx'; +ALTER TABLE vtype2 ALTER COLUMN b SET DEFAULT 'yyy'; +INSERT INTO vtype2 VALUES (2); +ALTER TABLE vtype2 ALTER COLUMN b TYPE varchar(20) USING b::varchar(20); +SELECT * FROM vtype2; + a | b +---+----- + 1 | xxx + 2 | yyy +(2 rows) + -- cleanup +DROP TABLE vtype; +DROP TABLE vtype2; DROP TABLE follower; DROP TABLE leader; DROP FUNCTION test_trigger(); diff --git a/src/test/regress/sql/fast_default.sql b/src/test/regress/sql/fast_default.sql index 344b5841d7..0f65a79c7f 100644 --- a/src/test/regress/sql/fast_default.sql +++ b/src/test/regress/sql/fast_default.sql @@ -481,7 +481,33 @@ ALTER TABLE leader ADD c int; ALTER TABLE leader DROP c; DELETE FROM leader; +-- check that ALTER TABLE ... ALTER TYPE does the right thing + +CREATE TABLE vtype( a integer); +INSERT INTO vtype VALUES (1); +ALTER TABLE vtype ADD COLUMN b DOUBLE PRECISION DEFAULT 0.2; +ALTER TABLE vtype ADD COLUMN c BOOLEAN DEFAULT true; +SELECT * FROM vtype; +ALTER TABLE vtype + ALTER b TYPE text USING b::text, + ALTER c TYPE text USING c::text; +SELECT * FROM vtype; + +-- also check the case that doesn't rewrite the table + +CREATE TABLE vtype2 (a int); +INSERT INTO vtype2 VALUES (1); +ALTER TABLE vtype2 ADD COLUMN b varchar(10) DEFAULT 'xxx'; +ALTER TABLE vtype2 ALTER COLUMN b SET DEFAULT 'yyy'; +INSERT INTO vtype2 VALUES (2); + +ALTER TABLE vtype2 ALTER COLUMN b TYPE varchar(20) USING b::varchar(20); +SELECT * FROM vtype2; + + -- cleanup +DROP TABLE vtype; +DROP TABLE vtype2; DROP TABLE follower; DROP TABLE leader; DROP FUNCTION test_trigger(); --------------222B8EC15F7F2E6E918F3C48--