Skip to content

Commit 9f87da1

Browse files
committed
Refactor some code for ALTER TABLE SET LOGGED/UNLOGGED in tablecmds.c
Both sub-commands use the same routine to switch the relpersistence of a relation, duplicated the same checks, and used a style inconsistent with access methods and tablespaces. SET LOGEED/UNLOGGED is refactored to avoid any duplication, setting the reason why a relation rewrite happens within ATPrepChangePersistence(). This shaves some code. Discussion: https://postgr.es/m/ZiiyGFTBNkqcMQi_@paquier.xyz
1 parent d7fe02f commit 9f87da1

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

src/backend/commands/tablecmds.c

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ static ObjectAddress ATExecClusterOn(Relation rel, const char *indexName,
590590
static void ATExecDropCluster(Relation rel, LOCKMODE lockmode);
591591
static void ATPrepSetAccessMethod(AlteredTableInfo *tab, Relation rel, const char *amname);
592592
static void ATExecSetAccessMethodNoStorage(Relation rel, Oid newAccessMethodId);
593-
static bool ATPrepChangePersistence(Relation rel, bool toLogged);
593+
static void ATPrepChangePersistence(AlteredTableInfo *tab, Relation rel,
594+
bool toLogged);
594595
static void ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel,
595596
const char *tablespacename, LOCKMODE lockmode);
596597
static void ATExecSetTableSpace(Oid tableOid, Oid newTableSpace, LOCKMODE lockmode);
@@ -4953,33 +4954,13 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
49534954
pass = AT_PASS_MISC;
49544955
break;
49554956
case AT_SetLogged: /* SET LOGGED */
4956-
ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_SEQUENCE);
4957-
if (tab->chgPersistence)
4958-
ereport(ERROR,
4959-
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4960-
errmsg("cannot change persistence setting twice")));
4961-
tab->chgPersistence = ATPrepChangePersistence(rel, true);
4962-
/* force rewrite if necessary; see comment in ATRewriteTables */
4963-
if (tab->chgPersistence)
4964-
{
4965-
tab->rewrite |= AT_REWRITE_ALTER_PERSISTENCE;
4966-
tab->newrelpersistence = RELPERSISTENCE_PERMANENT;
4967-
}
4968-
pass = AT_PASS_MISC;
4969-
break;
49704957
case AT_SetUnLogged: /* SET UNLOGGED */
49714958
ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_SEQUENCE);
49724959
if (tab->chgPersistence)
49734960
ereport(ERROR,
49744961
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
49754962
errmsg("cannot change persistence setting twice")));
4976-
tab->chgPersistence = ATPrepChangePersistence(rel, false);
4977-
/* force rewrite if necessary; see comment in ATRewriteTables */
4978-
if (tab->chgPersistence)
4979-
{
4980-
tab->rewrite |= AT_REWRITE_ALTER_PERSISTENCE;
4981-
tab->newrelpersistence = RELPERSISTENCE_UNLOGGED;
4982-
}
4963+
ATPrepChangePersistence(tab, rel, cmd->subtype == AT_SetLogged);
49834964
pass = AT_PASS_MISC;
49844965
break;
49854966
case AT_DropOids: /* SET WITHOUT OIDS */
@@ -16894,12 +16875,9 @@ ATExecSetCompression(Relation rel,
1689416875
* This verifies that we're not trying to change a temp table. Also,
1689516876
* existing foreign key constraints are checked to avoid ending up with
1689616877
* permanent tables referencing unlogged tables.
16897-
*
16898-
* Return value is false if the operation is a no-op (in which case the
16899-
* checks are skipped), otherwise true.
1690016878
*/
16901-
static bool
16902-
ATPrepChangePersistence(Relation rel, bool toLogged)
16879+
static void
16880+
ATPrepChangePersistence(AlteredTableInfo *tab, Relation rel, bool toLogged)
1690316881
{
1690416882
Relation pg_constraint;
1690516883
HeapTuple tuple;
@@ -16923,12 +16901,12 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
1692316901
case RELPERSISTENCE_PERMANENT:
1692416902
if (toLogged)
1692516903
/* nothing to do */
16926-
return false;
16904+
return;
1692716905
break;
1692816906
case RELPERSISTENCE_UNLOGGED:
1692916907
if (!toLogged)
1693016908
/* nothing to do */
16931-
return false;
16909+
return;
1693216910
break;
1693316911
}
1693416912

@@ -17011,7 +16989,13 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
1701116989

1701216990
table_close(pg_constraint, AccessShareLock);
1701316991

17014-
return true;
16992+
/* force rewrite if necessary; see comment in ATRewriteTables */
16993+
tab->rewrite |= AT_REWRITE_ALTER_PERSISTENCE;
16994+
if (toLogged)
16995+
tab->newrelpersistence = RELPERSISTENCE_PERMANENT;
16996+
else
16997+
tab->newrelpersistence = RELPERSISTENCE_UNLOGGED;
16998+
tab->chgPersistence = true;
1701516999
}
1701617000

1701717001
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy