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 1tqb5T-007VWf-CA for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 17:02:15 +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 1tqb5R-00C2QV-7c for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Mar 2025 17:02:13 +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 1tqb5Q-00C2QM-TT for pgsql-hackers@lists.postgresql.org; Fri, 07 Mar 2025 17:02:12 +0000 Received: from mail.nataraj.su ([168.119.121.177]) by magus.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1tqb5N-001XCs-0j for pgsql-hackers@lists.postgresql.org; Fri, 07 Mar 2025 17:02:12 +0000 Received: from thinkpad-pgpro.localnet (unknown [91.203.188.92]) by mail.nataraj.su (Postfix) with ESMTPSA id 1959D1453D2 for ; Fri, 7 Mar 2025 20:02:09 +0300 (MSK) From: Nikolay Shaplov To: pgsql-hackers@lists.postgresql.org Subject: Minor rework of ALTER TABLE SET RelOptions code Date: Fri, 07 Mar 2025 20:02:08 +0300 Message-ID: <1913854.tdWV9SEqCh@thinkpad-pgpro> Organization: Postgres Professional MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart13713915.uLZWGnKmhe"; micalg="pgp-sha512"; protocol="application/pgp-signature" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --nextPart13713915.uLZWGnKmhe Content-Type: multipart/mixed; boundary="nextPart2215029.irdbgypaU6"; protected-headers="v1" Content-Transfer-Encoding: 7Bit From: Nikolay Shaplov To: pgsql-hackers@lists.postgresql.org Subject: Minor rework of ALTER TABLE SET RelOptions code Date: Fri, 07 Mar 2025 20:02:08 +0300 Message-ID: <1913854.tdWV9SEqCh@thinkpad-pgpro> Organization: Postgres Professional MIME-Version: 1.0 This is a multi-part message in MIME format. --nextPart2215029.irdbgypaU6 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" While working with my New Options Engine patch https://commitfest.postgresql.org/patch/4688/ I found out that I can detach a small portion of it as a separate patch. It has own value, even if big patch is never committed, and it would make smoother further committing of big patch if we ever get to it. Patch description is following: 1. `isnull` variable is actually needed in a very narrow scope, so it is better to keep it in that scope, not keeping it in mind in while dealing with the rest of the code. 2. Toast table RelOptions are never altered directly with ALTER command. One should do ATLER to a heap relation and use toast. reloption namespace to address toast's reloption. If you get `ATExecSetRelOptions` called with `RELKIND_TOASTVALUE` relation in the args, something is really wrong. We should throw asserts, errors and whistle as loud as we can. -- Nikolay Shaplov aka Nataraj Fuzzing Engineer at Postgres Professional Matrix IM: @dhyan:nataraj.su --nextPart2215029.irdbgypaU6 Content-Disposition: attachment; filename="v-1-0001-Minor-rework-of-ALTER-TABLE-SET-RelOptions-code.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="unicode-2-0-utf-8"; name="v-1-0001-Minor-rework-of-ALTER-TABLE-SET-RelOptions-code.patch" From 5618baa7a4efd075d402444aedc31437c7652afa Mon Sep 17 00:00:00 2001 From: Nikolay Shaplov Date: Fri, 7 Mar 2025 19:44:14 +0300 Subject: [PATCH v[1] 1/1] Minor rework of ALTER TABLE SET RelOptions code 1. `isnull` variable is actually needed in a very narrow scope, so it is better to keep it in that scope, not keeping it in mind in while dealing with the rest of the code. 2. Toast table RelOptions are never altered directly with ALTER command. One should do ATLER to a heap relation and use toast. reloption namespace to address toast's reloption. If you get `ATExecSetRelOptions` called with `RELKIND_TOASTVALUE` relation in the args, something is really wrong. We should throw asserts, errors and whistle as loud as we can. --- src/backend/commands/tablecmds.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 59156a1c1f..a0f78f036b 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -15804,7 +15804,6 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation, HeapTuple tuple; HeapTuple newtuple; Datum datum; - bool isnull; Datum newOptions; Datum repl_val[Natts_pg_class]; bool repl_null[Natts_pg_class]; @@ -15829,25 +15828,25 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation, * there were none before. */ datum = (Datum) 0; - isnull = true; } else { + bool isnull; /* Get the old reloptions */ datum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions, &isnull); + if (isnull) + datum = (Datum) 0; } /* Generate new proposed reloptions (text array) */ - newOptions = transformRelOptions(isnull ? (Datum) 0 : datum, - defList, NULL, validnsps, false, + newOptions = transformRelOptions(datum, defList, NULL, validnsps, false, operation == AT_ResetRelOptions); /* Validate */ switch (rel->rd_rel->relkind) { case RELKIND_RELATION: - case RELKIND_TOASTVALUE: case RELKIND_MATVIEW: (void) heap_reloptions(rel->rd_rel->relkind, newOptions, true); break; @@ -15861,6 +15860,12 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation, case RELKIND_PARTITIONED_INDEX: (void) index_reloptions(rel->rd_indam->amoptions, newOptions, true); break; + case RELKIND_TOASTVALUE: + /* Should never get here */ + /* TOAST options are never altered directly */ + Assert(0); + /* FALLTHRU */ + /* If we get here in prod. error is the best option */ default: ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), @@ -15950,18 +15955,19 @@ ATExecSetRelOptions(Relation rel, List *defList, AlterTableType operation, * pretend there were none before. */ datum = (Datum) 0; - isnull = true; } else { + bool isnull; /* Get the old reloptions */ datum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_reloptions, &isnull); + if (isnull) + datum = (Datum) 0; } - newOptions = transformRelOptions(isnull ? (Datum) 0 : datum, - defList, "toast", validnsps, false, - operation == AT_ResetRelOptions); + newOptions = transformRelOptions(datum, defList, "toast", validnsps, + false, operation == AT_ResetRelOptions); (void) heap_reloptions(RELKIND_TOASTVALUE, newOptions, true); -- 2.39.2 --nextPart2215029.irdbgypaU6-- --nextPart13713915.uLZWGnKmhe Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE+sk3ebqQKlezKOi8PMbfuIHAGpgFAmfLJpAACgkQPMbfuIHA GpjXJwgAn1piaU1+C9j8iF670iXa6P9fJ/SpV3THh6jcglTDXwUpELwr5IV7xdDf TN8llIT2YSrjmLwyhZ6BJ+Me3/PL+QS2mOuknbxWkT/dLIERFSnf9pv/prQCU6ws DZJu2+XnQVJoR0nKyRKmd2dcIb7zdHRt3+acKRU5iU6EclQrlgPC0KgjFcaBuHuv CVqEivMjOmoV3cYCV0Ntj4Lae42fRIGEbUDRpZ6y2HVArFOgPJgRCTxkxvC4eGo+ J5c+lzlpRhJwDq/WOYjuxof+HvJUmiHsfZlVQQqeUoq84JfLHmGUUP2BG5PP0dPo ffy9gkF8aWrdnb3TwBEdxspjmUUnMA== =51hh -----END PGP SIGNATURE----- --nextPart13713915.uLZWGnKmhe--