public inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH] hex squash commit
8+ messages / 5 participants
[nested] [flat]

* [PATCH] hex squash commit
@ 2021-01-01 20:04 Bruce Momjian <[email protected]>
  0 siblings, 0 replies; 8+ messages in thread

From: Bruce Momjian @ 2021-01-01 20:04 UTC (permalink / raw)

---
 src/backend/replication/backup_manifest.c |  2 +-
 src/backend/utils/adt/encode.c            | 34 +------------------
 src/backend/utils/adt/varlena.c           |  2 +-
 src/common/Makefile                       |  2 +-
 src/common/{hex_decode.c => hex.c}        | 40 ++++++++++++++++++++---
 src/include/common/hex.h (new)            | 18 ++++++++++
 src/include/common/hex_decode.h (gone)    | 16 ---------
 src/include/utils/builtins.h              |  3 --
 src/tools/msvc/Mkvcbuild.pm               |  2 +-
 9 files changed, 59 insertions(+), 60 deletions(-)

diff --git a/src/backend/replication/backup_manifest.c b/src/backend/replication/backup_manifest.c
index c3f339c556..716f114d78 100644
--- a/src/backend/replication/backup_manifest.c
+++ b/src/backend/replication/backup_manifest.c
@@ -17,7 +17,7 @@
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "replication/backup_manifest.h"
-#include "utils/builtins.h"
+#include "common/hex.h"
 #include "utils/json.h"
 
 static void AppendStringToManifest(backup_manifest_info *manifest, char *s);
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index a6c65b1657..bca941a496 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -15,7 +15,7 @@
 
 #include <ctype.h>
 
-#include "common/hex_decode.h"
+#include "common/hex.h"
 #include "mb/pg_wchar.h"
 #include "utils/builtins.h"
 #include "utils/memutils.h"
@@ -141,38 +141,6 @@ binary_decode(PG_FUNCTION_ARGS)
 }
 
 
-/*
- * HEX
- */
-
-static const char hextbl[] = "0123456789abcdef";
-
-uint64
-hex_encode(const char *src, size_t len, char *dst)
-{
-	const char *end = src + len;
-
-	while (src < end)
-	{
-		*dst++ = hextbl[(*src >> 4) & 0xF];
-		*dst++ = hextbl[*src & 0xF];
-		src++;
-	}
-	return (uint64) len * 2;
-}
-
-static uint64
-hex_enc_len(const char *src, size_t srclen)
-{
-	return (uint64) srclen << 1;
-}
-
-static uint64
-hex_dec_len(const char *src, size_t srclen)
-{
-	return (uint64) srclen >> 1;
-}
-
 /*
  * BASE64
  */
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 9300d19e0c..79fcdcd178 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -22,7 +22,7 @@
 #include "catalog/pg_type.h"
 #include "common/hashfn.h"
 #include "common/int.h"
-#include "common/hex_decode.h"
+#include "common/hex.h"
 #include "common/unicode_norm.h"
 #include "lib/hyperloglog.h"
 #include "libpq/pqformat.h"
diff --git a/src/common/Makefile b/src/common/Makefile
index f624977939..93eb27a2aa 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -58,7 +58,7 @@ OBJS_COMMON = \
 	file_perm.o \
 	file_utils.o \
 	hashfn.o \
-	hex_decode.o \
+	hex.o \
 	ip.o \
 	jsonapi.o \
 	keywords.o \
diff --git a/src/common/hex_decode.c b/src/common/hex.c
similarity index 79%
rename from src/common/hex_decode.c
rename to src/common/hex.c
index 3ecdc73b5c..97f57bcc32 100644
--- a/src/common/hex_decode.c
+++ b/src/common/hex.c
@@ -1,7 +1,7 @@
 /*-------------------------------------------------------------------------
  *
- * hex_decode.c
- *		hex decoding
+ * hex.c
+ *		hex processing
  *
  *
  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  src/common/hex_decode.c
+ *	  src/common/hex.c
  *
  *-------------------------------------------------------------------------
  */
@@ -26,7 +26,7 @@
 #else
 #include "mb/pg_wchar.h"
 #endif
-#include "common/hex_decode.h"
+#include "common/hex.h"
 
 
 static const int8 hexlookup[128] = {
@@ -40,6 +40,26 @@ static const int8 hexlookup[128] = {
 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 };
 
+/*
+ * HEX
+ */
+
+static const char hextbl[] = "0123456789abcdef";
+
+uint64
+hex_encode(const char *src, size_t len, char *dst)
+{
+	const char *end = src + len;
+
+	while (src < end)
+	{
+		*dst++ = hextbl[(*src >> 4) & 0xF];
+		*dst++ = hextbl[*src & 0xF];
+		src++;
+	}
+	return (uint64) len * 2;
+}
+
 static inline char
 get_hex(const char *cp)
 {
@@ -104,3 +124,15 @@ hex_decode(const char *src, size_t len, char *dst)
 
 	return p - dst;
 }
+
+uint64
+hex_enc_len(const char *src, size_t srclen)
+{
+	return (uint64) srclen << 1;
+}
+
+uint64
+hex_dec_len(const char *src, size_t srclen)
+{
+	return (uint64) srclen >> 1;
+}
diff --git a/src/include/common/hex.h b/src/include/common/hex.h
new file mode 100644
index 0000000000..76154b65af
--- /dev/null
+++ b/src/include/common/hex.h
@@ -0,0 +1,18 @@
+/*
+ *	hex.h
+ *		hex processing
+ *
+ *	Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ *	Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *	src/include/common/hex.h
+ */
+#ifndef COMMON_HEX_H
+#define COMMON_HEX_H
+
+extern uint64 hex_decode(const char *src, size_t len, char *dst);
+extern uint64 hex_encode(const char *src, size_t len, char *dst);
+extern uint64 hex_enc_len(const char *src, size_t srclen);
+extern uint64 hex_dec_len(const char *src, size_t srclen);
+
+#endif							/* COMMON_HEX_H */
diff --git a/src/include/common/hex_decode.h b/src/include/common/hex_decode.h
deleted file mode 100644
index 1f99f069b2..0000000000
--- a/src/include/common/hex_decode.h
+++ /dev/null
@@ -1,16 +0,0 @@
-/*
- *	hex_decode.h
- *		hex decoding
- *
- *	Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
- *	Portions Copyright (c) 1994, Regents of the University of California
- *
- *	src/include/common/hex_decode.h
- */
-#ifndef COMMON_HEX_DECODE_H
-#define COMMON_HEX_DECODE_H
-
-extern uint64 hex_decode(const char *src, size_t len, char *dst);
-
-
-#endif							/* COMMON_HEX_DECODE_H */
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 19271e0696..11ba6ae565 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -31,9 +31,6 @@ extern void domain_check(Datum value, bool isnull, Oid domainType,
 extern int	errdatatype(Oid datatypeOid);
 extern int	errdomainconstraint(Oid datatypeOid, const char *conname);
 
-/* encode.c */
-extern uint64 hex_encode(const char *src, size_t len, char *dst);
-
 /* int.c */
 extern int2vector *buildint2vector(const int16 *int2s, int n);
 
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 7f014a12c9..60b216cce0 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -121,7 +121,7 @@ sub mkvcbuild
 	our @pgcommonallfiles = qw(
 	  archive.c base64.c checksum_helper.c
 	  config_info.c controldata_utils.c d2s.c encnames.c exec.c
-	  f2s.c file_perm.c file_utils.c hashfn.c hex_decode.c ip.c jsonapi.c
+	  f2s.c file_perm.c file_utils.c hashfn.c hex.c ip.c jsonapi.c
 	  keywords.c kwlookup.c link-canary.c md5_common.c
 	  pg_get_line.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
 	  saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c
-- 
2.20.1


--IJpNTDwzlM2Ie8A6--





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
@ 2024-04-24 23:35 Michael Paquier <[email protected]>
  2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
  0 siblings, 1 reply; 8+ messages in thread

From: Michael Paquier @ 2024-04-24 23:35 UTC (permalink / raw)
  To: David G. Johnston <[email protected]>; +Cc: Nathan Bossart <[email protected]>; Postgres hackers <[email protected]>

On Wed, Apr 24, 2024 at 03:36:35PM -0700, David G. Johnston wrote:
> On Wed, Apr 24, 2024 at 1:26 PM Nathan Bossart <[email protected]>
> wrote:
>> On Wed, Apr 24, 2024 at 04:17:44PM +0900, Michael Paquier wrote:
>>> - CREATE TABLE PARTITION OF would make a new partition inherit the
>>> logged ness of the parent if UNLOGGED is not directly specified.
>>
>> This one seems generally reasonable to me, provided it's properly noted in
>> the docs.
> 
> I don't see this being desirable at this point.  And especially not by
> itself.  It is an error to not specify TEMP on the partition create table
> command when the parent is temporary, and that one is a no-brainer for
> having the persistence mode of the child be derived from the parent.  The
> current policy of requiring the persistence of the child to be explicitly
> specified seems perfectly reasonable.  Or, put differently, the specific
> current persistence of the parent doesn't get copied or even considered
> when creating children.

I disagree here, actually.  Temporary tables are a different beast
because they require automated cleanup which would include interacting
with the partitionining information if temp and non-temp relations are
mixed.  That's why the current restrictions are in place: you should
be able to mix them.  Mixing unlogged and logged is OK, they retain a
state in their catalogs.

> In any case we aren't going to be able to do exactly what it means by
> marking a partitioned table unlogged - namely that we execute the truncate
> command on it after a crash.  Forcing the implementation here just so that
> our existing decision to ignore unlogged on the parent table is, IMO,
> letting optics corrupt good design.

It depends on retention policies, for one.  I could imagine an
application where partitioning is used based on a key where we
classify records based on their persistency, and one does not care
about a portion of them, still would like some retention as long as
the application is healthy.

> I do agree with having an in-core way for the DBA to communicate that they
> wish for partitions to be created with a known persistence unless the
> create table command specifies something different.  The way I would
> approach this is to add something like the following to the syntax/system:
> 
> CREATE [ persistence_mode ] TABLE ...
> ...
> WITH (partition_default_persistence = { logged, unlogged, temporary }) --
> storage_parameter, logged is effectively the default

While we have keywords to drive that at query level for TEMP and
UNLOGGED?  Not sure to be on board with this idea.  pg_dump may need
some changes to reflect the loggedness across the partitions, now that
I think about it even if there should be an ATTACH once the table is
created to link it to its partitioned table.  There should be no
rewrites at restore.

> I do agree with adding LOGGED to the list of optional persistence_mode
> specifiers, possibly regardless of whether any of this happens.  Seems
> desirable to give our default mode a name.

Yeah, at least it looks like Nathan and you are OK with this addition.
--
Michael


Attachments:

  [application/pgp-signature] signature.asc (833B, ../../[email protected]/2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
  2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
@ 2024-04-24 23:43 ` David G. Johnston <[email protected]>
  2024-04-24 23:55   ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  0 siblings, 1 reply; 8+ messages in thread

From: David G. Johnston @ 2024-04-24 23:43 UTC (permalink / raw)
  To: Michael Paquier <[email protected]>; +Cc: Nathan Bossart <[email protected]>; Postgres hackers <[email protected]>

On Wed, Apr 24, 2024 at 4:35 PM Michael Paquier <[email protected]> wrote:

>
> I disagree here, actually.  Temporary tables are a different beast
> because they require automated cleanup which would include interacting
> with the partitionining information if temp and non-temp relations are
> mixed.  That's why the current restrictions are in place: you should
>
[ not ] ?

> be able to mix them.
>

My point is that if you feel that treating logged as a copy-able property
is OK then doing the following should also just work:

postgres=# create temp table parentt ( id integer ) partition by range (id);
CREATE TABLE
postgres=# create table child10t partition of parentt for values from (0)
to (9);
ERROR:  cannot create a permanent relation as partition of temporary
relation "parentt"

i.e., child10t should be created as a temporary partition under parentt.

David J.


^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
  2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
@ 2024-04-24 23:55   ` Michael Paquier <[email protected]>
  2024-05-02 06:06     ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  0 siblings, 1 reply; 8+ messages in thread

From: Michael Paquier @ 2024-04-24 23:55 UTC (permalink / raw)
  To: David G. Johnston <[email protected]>; +Cc: Nathan Bossart <[email protected]>; Postgres hackers <[email protected]>

On Wed, Apr 24, 2024 at 04:43:58PM -0700, David G. Johnston wrote:
> My point is that if you feel that treating logged as a copy-able property
> is OK then doing the following should also just work:
> 
> postgres=# create temp table parentt ( id integer ) partition by range (id);
> CREATE TABLE
> postgres=# create table child10t partition of parentt for values from (0)
> to (9);
> ERROR:  cannot create a permanent relation as partition of temporary
> relation "parentt"
> 
> i.e., child10t should be created as a temporary partition under parentt.

Ah, indeed, I've missed your point here.  Lifting the error and
inheriting temporary in this case would make sense.
--
Michael


Attachments:

  [application/pgp-signature] signature.asc (833B, ../../[email protected]/2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
  2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
  2024-04-24 23:55   ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
@ 2024-05-02 06:06     ` Michael Paquier <[email protected]>
  2024-08-27 21:01       ` Re: Partitioned tables and [un]loggedness Nathan Bossart <[email protected]>
  2024-09-01 05:24       ` Re: Partitioned tables and [un]loggedness Junwang Zhao <[email protected]>
  0 siblings, 2 replies; 8+ messages in thread

From: Michael Paquier @ 2024-05-02 06:06 UTC (permalink / raw)
  To: David G. Johnston <[email protected]>; +Cc: Nathan Bossart <[email protected]>; Postgres hackers <[email protected]>

On Thu, Apr 25, 2024 at 08:55:27AM +0900, Michael Paquier wrote:
> On Wed, Apr 24, 2024 at 04:43:58PM -0700, David G. Johnston wrote:
>> My point is that if you feel that treating logged as a copy-able property
>> is OK then doing the following should also just work:
>> 
>> postgres=# create temp table parentt ( id integer ) partition by range (id);
>> CREATE TABLE
>> postgres=# create table child10t partition of parentt for values from (0)
>> to (9);
>> ERROR:  cannot create a permanent relation as partition of temporary
>> relation "parentt"
>> 
>> i.e., child10t should be created as a temporary partition under parentt.
> 
> Ah, indeed, I've missed your point here.  Lifting the error and
> inheriting temporary in this case would make sense.

The case of a temporary persistence is actually *very* tricky.  The
namespace, where the relation is created, is guessed and locked with
permission checks done based on the RangeVar when the CreateStmt is
transformed, which is before we try to look at its inheritance tree to
find its partitioned parent.  So we would somewhat need to shortcut
the existing RangeVar lookup and include the parents in the loop to
find out the correct namespace.  And this is much earlier than now.
The code complexity is not trivial, so I am getting cold feet when
trying to support this case in a robust fashion.  For now, I have
discarded this case and focused on the main problem with SET LOGGED
and UNLOGGED.

Switching between logged <-> unlogged does not have such
complications, because the namespace where the relation is created is
going to be the same.  So we won't lock or perform permission checks
on an incorrect namespace.

The addition of LOGGED makes the logic deciding how the loggedness of
a partition table based on its partitioned table or the query quite
easy to follow, but this needs some safety nets in the sequence, view
and CTAS code paths to handle with the case where the query specifies
no relpersistence.

I have also looked at support for ONLY, and I've been surprised that
it is not that complicated.  tablecmds.c has a ATSimpleRecursion()
that is smart enough to do an inheritance tree lookup and apply the
rewrites where they should happen in the step 3 of ALTER TABLE, while
handling ONLY on its own.  The relpersistence of partitioned tables is
updated in step 2, with the catalog changes.

Attached is a new patch series:
- 0001 refactors some code around ATPrepChangePersistence() that I
found confusing after applying the operation to partitioned tables.
- 0002 adds support for a LOGGED keyword.
- 0003 expands ALTER TABLE SET [UN]LOGGED to partitioned tables,
without recursion to partitions.
- 0004 adds the recursion logic, expanding regression tests to show
the difference.

0003 and 0004 should be merged together, I think.  Still, splitting
them makes reviews a bit easier.
--
Michael


Attachments:

  [text/x-diff] v2-0001-Refactor-some-code-of-ALTER-TABLE-SET-LOGGED-UNLO.patch (3.9K, ../../[email protected]/2-v2-0001-Refactor-some-code-of-ALTER-TABLE-SET-LOGGED-UNLO.patch)
  download | inline diff:
From 30d572fac2aa58ce2d62ba929c30fded9b020e0b Mon Sep 17 00:00:00 2001
From: Michael Paquier <[email protected]>
Date: Thu, 2 May 2024 08:16:33 +0900
Subject: [PATCH v2 1/4] Refactor some code of ALTER TABLE SET LOGGED/UNLOGGED

This is in preparation for an upcoming patch set to extend the
possibilities in this area, making the code more consistent with the
surroundings related to access methods and tablespaces.
---
 src/backend/commands/tablecmds.c | 38 +++++++++++++-------------------
 1 file changed, 15 insertions(+), 23 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 08c87e6029..baabbf82e7 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -602,7 +602,8 @@ static ObjectAddress ATExecClusterOn(Relation rel, const char *indexName,
 static void ATExecDropCluster(Relation rel, LOCKMODE lockmode);
 static void ATPrepSetAccessMethod(AlteredTableInfo *tab, Relation rel, const char *amname);
 static void ATExecSetAccessMethodNoStorage(Relation rel, Oid newAccessMethod);
-static bool ATPrepChangePersistence(Relation rel, bool toLogged);
+static void ATPrepSetPersistence(AlteredTableInfo *tab, Relation rel,
+								 bool toLogged);
 static void ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel,
 								const char *tablespacename, LOCKMODE lockmode);
 static void ATExecSetTableSpace(Oid tableOid, Oid newTableSpace, LOCKMODE lockmode);
@@ -5037,13 +5038,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 				ereport(ERROR,
 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						 errmsg("cannot change persistence setting twice")));
-			tab->chgPersistence = ATPrepChangePersistence(rel, true);
-			/* force rewrite if necessary; see comment in ATRewriteTables */
-			if (tab->chgPersistence)
-			{
-				tab->rewrite |= AT_REWRITE_ALTER_PERSISTENCE;
-				tab->newrelpersistence = RELPERSISTENCE_PERMANENT;
-			}
+			ATPrepSetPersistence(tab, rel, true);
 			pass = AT_PASS_MISC;
 			break;
 		case AT_SetUnLogged:	/* SET UNLOGGED */
@@ -5052,13 +5047,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 				ereport(ERROR,
 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						 errmsg("cannot change persistence setting twice")));
-			tab->chgPersistence = ATPrepChangePersistence(rel, false);
-			/* force rewrite if necessary; see comment in ATRewriteTables */
-			if (tab->chgPersistence)
-			{
-				tab->rewrite |= AT_REWRITE_ALTER_PERSISTENCE;
-				tab->newrelpersistence = RELPERSISTENCE_UNLOGGED;
-			}
+			ATPrepSetPersistence(tab, rel, false);
 			pass = AT_PASS_MISC;
 			break;
 		case AT_DropOids:		/* SET WITHOUT OIDS */
@@ -17775,12 +17764,9 @@ ATExecSetCompression(Relation rel,
  * This verifies that we're not trying to change a temp table.  Also,
  * existing foreign key constraints are checked to avoid ending up with
  * permanent tables referencing unlogged tables.
- *
- * Return value is false if the operation is a no-op (in which case the
- * checks are skipped), otherwise true.
  */
-static bool
-ATPrepChangePersistence(Relation rel, bool toLogged)
+static void
+ATPrepSetPersistence(AlteredTableInfo *tab, Relation rel, bool toLogged)
 {
 	Relation	pg_constraint;
 	HeapTuple	tuple;
@@ -17804,12 +17790,12 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
 		case RELPERSISTENCE_PERMANENT:
 			if (toLogged)
 				/* nothing to do */
-				return false;
+				return;
 			break;
 		case RELPERSISTENCE_UNLOGGED:
 			if (!toLogged)
 				/* nothing to do */
-				return false;
+				return;
 			break;
 	}
 
@@ -17892,7 +17878,13 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
 
 	table_close(pg_constraint, AccessShareLock);
 
-	return true;
+	/* force rewrite if necessary; see comment in ATRewriteTables */
+	tab->rewrite |= AT_REWRITE_ALTER_PERSISTENCE;
+	if (toLogged)
+		tab->newrelpersistence = RELPERSISTENCE_PERMANENT;
+	else
+		tab->newrelpersistence = RELPERSISTENCE_UNLOGGED;
+	tab->chgPersistence = true;
 }
 
 /*
-- 
2.43.0



  [text/x-diff] v2-0002-Add-support-for-LOGGED-keyword-similar-to-UNLOGGE.patch (20.1K, ../../[email protected]/3-v2-0002-Add-support-for-LOGGED-keyword-similar-to-UNLOGGE.patch)
  download | inline diff:
From f00c1295bf7d1a7f48409f8ddfedeffab87dabaa Mon Sep 17 00:00:00 2001
From: Michael Paquier <[email protected]>
Date: Thu, 2 May 2024 09:29:48 +0900
Subject: [PATCH v2 2/4] Add support for LOGGED keyword, similar to UNLOGGED
 but for permanent

This extends the following statements with a new keyword called LOGGED,
to be able to force a relation to be permanent:
- CREATE SEQUENCE
- CREATE TABLE AS
- SELECT INTO
- CREATE TABLE

The implementation is done here with the introduction of a
RELPERSISTENCE_INVALID, which is set by the grammar when neither TEMP,
UNLOGGED or LOGGED are specified.  This is handy for an upcoming patch
that aims to introduce relpersistence inheritance for partitions, based
on its partitioned table.
---
 src/include/catalog/pg_class.h            |  1 +
 src/backend/catalog/namespace.c           |  8 ++++++
 src/backend/commands/createas.c           |  7 +++++
 src/backend/commands/sequence.c           |  7 +++++
 src/backend/commands/tablecmds.c          |  9 +++++++
 src/backend/commands/view.c               |  7 +++++
 src/backend/parser/gram.y                 |  8 +++++-
 src/test/regress/expected/identity.out    | 11 ++++++++
 src/test/regress/expected/select_into.out | 32 +++++++++++++++++++++++
 src/test/regress/expected/sequence.out    |  9 +++++++
 src/test/regress/sql/identity.sql         |  6 +++++
 src/test/regress/sql/select_into.sql      | 13 +++++++++
 src/test/regress/sql/sequence.sql         |  5 ++++
 doc/src/sgml/ref/create_sequence.sgml     | 12 ++++++++-
 doc/src/sgml/ref/create_table.sgml        | 22 +++++++++++++---
 doc/src/sgml/ref/create_table_as.sgml     | 12 ++++++++-
 doc/src/sgml/ref/select_into.sgml         | 12 ++++++++-
 17 files changed, 174 insertions(+), 7 deletions(-)

diff --git a/src/include/catalog/pg_class.h b/src/include/catalog/pg_class.h
index 0fc2c093b0..1b3fd060ca 100644
--- a/src/include/catalog/pg_class.h
+++ b/src/include/catalog/pg_class.h
@@ -175,6 +175,7 @@ MAKE_SYSCACHE(RELNAMENSP, pg_class_relname_nsp_index, 128);
 #define		  RELPERSISTENCE_PERMANENT	'p' /* regular table */
 #define		  RELPERSISTENCE_UNLOGGED	'u' /* unlogged permanent table */
 #define		  RELPERSISTENCE_TEMP		't' /* temporary table */
+#define		  RELPERSISTENCE_INVALID	'\0'	/* persistence not allowed */
 
 /* default selection for replica identity (primary key or nothing) */
 #define		  REPLICA_IDENTITY_DEFAULT	'd'
diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespace.c
index a2510cf80c..4bb9fafe1d 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -853,6 +853,14 @@ RangeVarAdjustRelationPersistence(RangeVar *newRelation, Oid nspid)
 						(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
 						 errmsg("cannot create relations in temporary schemas of other sessions")));
 			break;
+		case RELPERSISTENCE_INVALID:	/* persistence not specified by grammar */
+			if (isTempOrTempToastNamespace(nspid))
+				newRelation->relpersistence = RELPERSISTENCE_TEMP;
+			else if (isAnyTempNamespace(nspid))
+				ereport(ERROR,
+						(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+						 errmsg("only temporary relations may be created in temporary schemas")));
+			break;
 		default:
 			if (isAnyTempNamespace(nspid))
 				ereport(ERROR,
diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c
index 62050f4dc5..2a26fe0b6f 100644
--- a/src/backend/commands/createas.c
+++ b/src/backend/commands/createas.c
@@ -238,6 +238,13 @@ ExecCreateTableAs(ParseState *pstate, CreateTableAsStmt *stmt,
 	if (CreateTableAsRelExists(stmt))
 		return InvalidObjectAddress;
 
+	/*
+	 * If the grammar did not specify a relpersistence, assume that the
+	 * relation is permanent.
+	 */
+	if (into->rel->relpersistence == RELPERSISTENCE_INVALID)
+		into->rel->relpersistence = RELPERSISTENCE_PERMANENT;
+
 	/*
 	 * Create the tuple receiver object and insert info it will need
 	 */
diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c
index 46103561c3..3388e59f56 100644
--- a/src/backend/commands/sequence.c
+++ b/src/backend/commands/sequence.c
@@ -162,6 +162,13 @@ DefineSequence(ParseState *pstate, CreateSeqStmt *seq)
 		}
 	}
 
+	/*
+	 * If the grammar did not specify a relpersistence, assume that the
+	 * relation is permanent.
+	 */
+	if (seq->sequence->relpersistence == RELPERSISTENCE_INVALID)
+		seq->sequence->relpersistence = RELPERSISTENCE_PERMANENT;
+
 	/* Check and set all option values */
 	init_params(pstate, seq->options, seq->for_identity, true,
 				&seqform, &seqdataform,
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index baabbf82e7..97ba34f0d5 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -810,6 +810,15 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 		inheritOids = lappend_oid(inheritOids, parentOid);
 	}
 
+	/*
+	 * If the grammar did not specify a relpersistence, assume that the
+	 * relation is permanent.  Note that this is done before selecting
+	 * the relation's tablespace, as this change may impact the tablespace
+	 * location depending on the persistence set here.
+	 */
+	if (stmt->relation->relpersistence == RELPERSISTENCE_INVALID)
+		stmt->relation->relpersistence = RELPERSISTENCE_PERMANENT;
+
 	/*
 	 * Select tablespace to use: an explicitly indicated one, or (in the case
 	 * of a partitioned table) the parent's, if it has one.
diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c
index fdad833832..6e691525d7 100644
--- a/src/backend/commands/view.c
+++ b/src/backend/commands/view.c
@@ -388,6 +388,13 @@ DefineView(ViewStmt *stmt, const char *queryString,
 	if (viewParse->commandType != CMD_SELECT)
 		elog(ERROR, "unexpected parse analysis result");
 
+	/*
+	 * If the grammar did not specify a relpersistence, assume that the
+	 * relation is permanent.
+	 */
+	if (stmt->view->relpersistence == RELPERSISTENCE_INVALID)
+		stmt->view->relpersistence = RELPERSISTENCE_PERMANENT;
+
 	/*
 	 * Check for unsupported cases.  These tests are redundant with ones in
 	 * DefineQueryRewrite(), but that function will complain about a bogus ON
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index e8b619926e..85ef8dd372 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -3775,7 +3775,8 @@ OptTemp:	TEMPORARY					{ $$ = RELPERSISTENCE_TEMP; }
 					$$ = RELPERSISTENCE_TEMP;
 				}
 			| UNLOGGED					{ $$ = RELPERSISTENCE_UNLOGGED; }
-			| /*EMPTY*/					{ $$ = RELPERSISTENCE_PERMANENT; }
+			| LOGGED					{ $$ = RELPERSISTENCE_PERMANENT; }
+			| /*EMPTY*/					{ $$ = RELPERSISTENCE_INVALID; }
 		;
 
 OptTableElementList:
@@ -13129,6 +13130,11 @@ OptTempTableName:
 					$$ = $3;
 					$$->relpersistence = RELPERSISTENCE_UNLOGGED;
 				}
+			| LOGGED opt_table qualified_name
+				{
+					$$ = $3;
+					$$->relpersistence = RELPERSISTENCE_PERMANENT;
+				}
 			| TABLE qualified_name
 				{
 					$$ = $2;
diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out
index f357b9b63b..373df20661 100644
--- a/src/test/regress/expected/identity.out
+++ b/src/test/regress/expected/identity.out
@@ -365,6 +365,17 @@ SELECT seqtypid::regtype FROM pg_sequence WHERE seqrelid = 'itest3_a_seq'::regcl
 
 ALTER TABLE itest3 ALTER COLUMN a TYPE text;  -- error
 ERROR:  identity column type must be smallint, integer, or bigint
+-- check that LOGGED propagates to sequence (for grammar)
+CREATE LOGGED TABLE itest16 (a int NOT NULL, b text);
+ALTER TABLE itest16 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;
+\d itest16_a_seq
+                   Sequence "public.itest16_a_seq"
+  Type   | Start | Minimum |  Maximum   | Increment | Cycles? | Cache 
+---------+-------+---------+------------+-----------+---------+-------
+ integer |     1 |       1 | 2147483647 |         1 | no      |     1
+Sequence for identity column: public.itest16.a
+
+DROP TABLE itest16;
 -- check that unlogged propagates to sequence
 CREATE UNLOGGED TABLE itest17 (a int NOT NULL, b text);
 ALTER TABLE itest17 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;
diff --git a/src/test/regress/expected/select_into.out b/src/test/regress/expected/select_into.out
index b79fe9a1c0..fb9c3eaa2a 100644
--- a/src/test/regress/expected/select_into.out
+++ b/src/test/regress/expected/select_into.out
@@ -220,3 +220,35 @@ NOTICE:  relation "ctas_ine_tbl" already exists, skipping
 (0 rows)
 
 DROP TABLE ctas_ine_tbl;
+-- CREATE TABLE AS with LOGGED and UNLOGGED.
+CREATE UNLOGGED TABLE ctas_unlogged_tbl AS SELECT 1 AS a;
+\d ctas_unlogged_tbl
+     Unlogged table "public.ctas_unlogged_tbl"
+ Column |  Type   | Collation | Nullable | Default 
+--------+---------+-----------+----------+---------
+ a      | integer |           |          | 
+
+CREATE LOGGED TABLE ctas_logged_tbl AS SELECT 1 AS a;
+\d ctas_logged_tbl
+          Table "public.ctas_logged_tbl"
+ Column |  Type   | Collation | Nullable | Default 
+--------+---------+-----------+----------+---------
+ a      | integer |           |          | 
+
+DROP TABLE ctas_logged_tbl, ctas_unlogged_tbl;
+-- SELECT INTO with LOGGED and UNLOGGED.
+SELECT 1 AS a INTO UNLOGGED ctas_unlogged_tbl;
+\d ctas_unlogged_tbl
+     Unlogged table "public.ctas_unlogged_tbl"
+ Column |  Type   | Collation | Nullable | Default 
+--------+---------+-----------+----------+---------
+ a      | integer |           |          | 
+
+SELECT 1 AS a INTO LOGGED ctas_logged_tbl;
+\d ctas_logged_tbl
+          Table "public.ctas_logged_tbl"
+ Column |  Type   | Collation | Nullable | Default 
+--------+---------+-----------+----------+---------
+ a      | integer |           |          | 
+
+DROP TABLE ctas_logged_tbl, ctas_unlogged_tbl;
diff --git a/src/test/regress/expected/sequence.out b/src/test/regress/expected/sequence.out
index 2b47b7796b..653428ddf6 100644
--- a/src/test/regress/expected/sequence.out
+++ b/src/test/regress/expected/sequence.out
@@ -599,6 +599,15 @@ DROP SEQUENCE seq2;
 -- should fail
 SELECT lastval();
 ERROR:  lastval is not yet defined in this session
+-- logged sequences (for grammar)
+CREATE LOGGED SEQUENCE sequence_test_logged;
+\d sequence_test_logged
+                    Sequence "public.sequence_test_logged"
+  Type  | Start | Minimum |       Maximum       | Increment | Cycles? | Cache 
+--------+-------+---------+---------------------+-----------+---------+-------
+ bigint |     1 |       1 | 9223372036854775807 |         1 | no      |     1
+
+DROP SEQUENCE sequence_test_logged;
 -- unlogged sequences
 -- (more tests in src/test/recovery/)
 CREATE UNLOGGED SEQUENCE sequence_test_unlogged;
diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql
index 7b0800226c..833c6f49fb 100644
--- a/src/test/regress/sql/identity.sql
+++ b/src/test/regress/sql/identity.sql
@@ -214,6 +214,12 @@ SELECT seqtypid::regtype FROM pg_sequence WHERE seqrelid = 'itest3_a_seq'::regcl
 
 ALTER TABLE itest3 ALTER COLUMN a TYPE text;  -- error
 
+-- check that LOGGED propagates to sequence (for grammar)
+CREATE LOGGED TABLE itest16 (a int NOT NULL, b text);
+ALTER TABLE itest16 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;
+\d itest16_a_seq
+DROP TABLE itest16;
+
 -- check that unlogged propagates to sequence
 CREATE UNLOGGED TABLE itest17 (a int NOT NULL, b text);
 ALTER TABLE itest17 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;
diff --git a/src/test/regress/sql/select_into.sql b/src/test/regress/sql/select_into.sql
index 689c448cc2..55ffb25c12 100644
--- a/src/test/regress/sql/select_into.sql
+++ b/src/test/regress/sql/select_into.sql
@@ -136,3 +136,16 @@ EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
 EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
   CREATE TABLE IF NOT EXISTS ctas_ine_tbl AS EXECUTE ctas_ine_query; -- ok
 DROP TABLE ctas_ine_tbl;
+
+-- CREATE TABLE AS with LOGGED and UNLOGGED.
+CREATE UNLOGGED TABLE ctas_unlogged_tbl AS SELECT 1 AS a;
+\d ctas_unlogged_tbl
+CREATE LOGGED TABLE ctas_logged_tbl AS SELECT 1 AS a;
+\d ctas_logged_tbl
+DROP TABLE ctas_logged_tbl, ctas_unlogged_tbl;
+-- SELECT INTO with LOGGED and UNLOGGED.
+SELECT 1 AS a INTO UNLOGGED ctas_unlogged_tbl;
+\d ctas_unlogged_tbl
+SELECT 1 AS a INTO LOGGED ctas_logged_tbl;
+\d ctas_logged_tbl
+DROP TABLE ctas_logged_tbl, ctas_unlogged_tbl;
diff --git a/src/test/regress/sql/sequence.sql b/src/test/regress/sql/sequence.sql
index 674f5f1f66..189112fef7 100644
--- a/src/test/regress/sql/sequence.sql
+++ b/src/test/regress/sql/sequence.sql
@@ -271,6 +271,11 @@ DROP SEQUENCE seq2;
 -- should fail
 SELECT lastval();
 
+-- logged sequences (for grammar)
+CREATE LOGGED SEQUENCE sequence_test_logged;
+\d sequence_test_logged
+DROP SEQUENCE sequence_test_logged;
+
 -- unlogged sequences
 -- (more tests in src/test/recovery/)
 CREATE UNLOGGED SEQUENCE sequence_test_unlogged;
diff --git a/doc/src/sgml/ref/create_sequence.sgml b/doc/src/sgml/ref/create_sequence.sgml
index 34e9084b5c..ef8654cee5 100644
--- a/doc/src/sgml/ref/create_sequence.sgml
+++ b/doc/src/sgml/ref/create_sequence.sgml
@@ -21,7 +21,7 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-CREATE [ { TEMPORARY | TEMP } | UNLOGGED ] SEQUENCE [ IF NOT EXISTS ] <replaceable class="parameter">name</replaceable>
+CREATE [ { TEMPORARY | TEMP } | LOGGED | UNLOGGED ] SEQUENCE [ IF NOT EXISTS ] <replaceable class="parameter">name</replaceable>
     [ AS <replaceable class="parameter">data_type</replaceable> ]
     [ INCREMENT [ BY ] <replaceable class="parameter">increment</replaceable> ]
     [ MINVALUE <replaceable class="parameter">minvalue</replaceable> | NO MINVALUE ] [ MAXVALUE <replaceable class="parameter">maxvalue</replaceable> | NO MAXVALUE ]
@@ -92,6 +92,16 @@ SELECT * FROM <replaceable>name</replaceable>;
     </listitem>
    </varlistentry>
 
+   <varlistentry>
+    <term><literal>LOGGED</literal></term>
+    <listitem>
+     <para>
+      If specified, the sequence is created as an permanent sequence.  Changes
+      to permanent sequences are written to the write-ahead log.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry>
     <term><literal>UNLOGGED</literal></term>
     <listitem>
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index 02f31d2d6f..29dfd68dc8 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -21,7 +21,7 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
+CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | LOGGED | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
   { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION <replaceable>compression_method</replaceable> ] [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
     | <replaceable>table_constraint</replaceable>
     | LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ] }
@@ -34,7 +34,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
 [ TABLESPACE <replaceable class="parameter">tablespace_name</replaceable> ]
 
-CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable>
+CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | LOGGED | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable>
     OF <replaceable class="parameter">type_name</replaceable> [ (
   { <replaceable class="parameter">column_name</replaceable> [ WITH OPTIONS ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
     | <replaceable>table_constraint</replaceable> }
@@ -46,7 +46,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
 [ TABLESPACE <replaceable class="parameter">tablespace_name</replaceable> ]
 
-CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable>
+CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | LOGGED | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable>
     PARTITION OF <replaceable class="parameter">parent_table</replaceable> [ (
   { <replaceable class="parameter">column_name</replaceable> [ WITH OPTIONS ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
     | <replaceable>table_constraint</replaceable> }
@@ -203,6 +203,22 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
     </listitem>
    </varlistentry>
 
+   <varlistentry id="sql-createtable-logged">
+    <term><literal>LOGGED</literal></term>
+    <listitem>
+     <para>
+      If specified, the table is created as an permanent table.  Data written
+      to permanent tables is written to the write-ahead log (see
+      <xref linkend="wal"/>).
+     </para>
+
+     <para>
+      If this is specified, any sequences created together with the permanent
+      table (for identity or serial columns) are also created as permanent.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry id="sql-createtable-unlogged">
     <term><literal>UNLOGGED</literal></term>
     <listitem>
diff --git a/doc/src/sgml/ref/create_table_as.sgml b/doc/src/sgml/ref/create_table_as.sgml
index 8429333e3a..4f250f059c 100644
--- a/doc/src/sgml/ref/create_table_as.sgml
+++ b/doc/src/sgml/ref/create_table_as.sgml
@@ -21,7 +21,7 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable>table_name</replaceable>
+CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | LOGGED | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable>table_name</replaceable>
     [ (<replaceable>column_name</replaceable> [, ...] ) ]
     [ USING <replaceable class="parameter">method</replaceable> ]
     [ WITH ( <replaceable class="parameter">storage_parameter</replaceable> [= <replaceable class="parameter">value</replaceable>] [, ... ] ) | WITHOUT OIDS ]
@@ -86,6 +86,16 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
     </listitem>
    </varlistentry>
 
+   <varlistentry>
+    <term><literal>LOGGED</literal></term>
+    <listitem>
+     <para>
+      If specified, the table is created as a permanent table.
+      Refer to <xref linkend="sql-createtable"/> for details.
+     </para>
+    </listitem>
+   </varlistentry>
+
    <varlistentry>
     <term><literal>UNLOGGED</literal></term>
     <listitem>
diff --git a/doc/src/sgml/ref/select_into.sgml b/doc/src/sgml/ref/select_into.sgml
index 82a77784b9..d818c620fc 100644
--- a/doc/src/sgml/ref/select_into.sgml
+++ b/doc/src/sgml/ref/select_into.sgml
@@ -24,7 +24,7 @@ PostgreSQL documentation
 [ WITH [ RECURSIVE ] <replaceable class="parameter">with_query</replaceable> [, ...] ]
 SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replaceable> [, ...] ) ] ]
     * | <replaceable class="parameter">expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] [, ...]
-    INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] <replaceable class="parameter">new_table</replaceable>
+    INTO [ TEMPORARY | TEMP | LOGGED | UNLOGGED ] [ TABLE ] <replaceable class="parameter">new_table</replaceable>
     [ FROM <replaceable class="parameter">from_item</replaceable> [, ...] ]
     [ WHERE <replaceable class="parameter">condition</replaceable> ]
     [ GROUP BY <replaceable class="parameter">expression</replaceable> [, ...] ]
@@ -75,6 +75,16 @@ SELECT [ ALL | DISTINCT [ ON ( <replaceable class="parameter">expression</replac
    </listitem>
   </varlistentry>
 
+  <varlistentry>
+   <term><literal>LOGGED</literal></term>
+   <listitem>
+    <para>
+     If specified, the table is created as a permanent table.  Refer
+     to <xref linkend="sql-createtable"/> for details.
+    </para>
+   </listitem>
+  </varlistentry>
+
    <varlistentry>
     <term><replaceable class="parameter">new_table</replaceable></term>
     <listitem>
-- 
2.43.0



  [text/x-diff] v2-0003-Support-LOGGED-UNLOGGED-for-partitioned-tables.patch (13.8K, ../../[email protected]/4-v2-0003-Support-LOGGED-UNLOGGED-for-partitioned-tables.patch)
  download | inline diff:
From de44be520f8c157dc558f63dd4059d80b6ad00e9 Mon Sep 17 00:00:00 2001
From: Michael Paquier <[email protected]>
Date: Thu, 2 May 2024 13:45:18 +0900
Subject: [PATCH v2 3/4] Support LOGGED/UNLOGGED for partitioned tables

When using ALTER TABLE SET LOGGED/UNLOGGED, indexes and sequences that
are owned by the partitioned table changed need to have their
relpersistence also changed.

This patch does not apply recursion when using ALTER TABLE on a
partitioned table: only new partitions inherit the loggedness of the
parent if the query does not give a persistence (either LOGGED or
UNLOGGED).
---
 src/backend/commands/tablecmds.c          | 118 +++++++++++++++++++++-
 src/test/regress/expected/alter_table.out |  88 ++++++++++++++++
 src/test/regress/sql/alter_table.sql      |  37 +++++++
 doc/src/sgml/ref/alter_table.sgml         |   6 ++
 doc/src/sgml/ref/create_table.sgml        |   5 +
 5 files changed, 249 insertions(+), 5 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 97ba34f0d5..8854e31f14 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -602,6 +602,7 @@ static ObjectAddress ATExecClusterOn(Relation rel, const char *indexName,
 static void ATExecDropCluster(Relation rel, LOCKMODE lockmode);
 static void ATPrepSetAccessMethod(AlteredTableInfo *tab, Relation rel, const char *amname);
 static void ATExecSetAccessMethodNoStorage(Relation rel, Oid newAccessMethod);
+static void ATExecSetPersistenceNoStorage(Relation rel, char newrelpersistence);
 static void ATPrepSetPersistence(AlteredTableInfo *tab, Relation rel,
 								 bool toLogged);
 static void ATPrepSetTableSpace(AlteredTableInfo *tab, Relation rel,
@@ -811,13 +812,39 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 	}
 
 	/*
-	 * If the grammar did not specify a relpersistence, assume that the
-	 * relation is permanent.  Note that this is done before selecting
-	 * the relation's tablespace, as this change may impact the tablespace
-	 * location depending on the persistence set here.
+	 * If the grammar did not specify a relpersistence, determine which one
+	 * to use depending on the relation to create.  Note that this is done
+	 * before selecting the relation's tablespace, as this change may impact
+	 * the tablespace location depending on the persistence set here.
+	 *
+	 * If the relation is not partitioned, assume that it is permanent.
+	 *
+	 * A partition inherits the persistence of its partitioned table, it the
+	 * latter is unlogged or logged as the namespace where the relation will
+	 * be created is known.  This property cannot be enforced for temporary
+	 * partitioned tables because the namespace of the relation is locked
+	 * before it is possible to know the inheritance tree of this new
+	 * relation, when its RangeVar is locked earlier when transforming the
+	 * CreateStmt query.
 	 */
 	if (stmt->relation->relpersistence == RELPERSISTENCE_INVALID)
-		stmt->relation->relpersistence = RELPERSISTENCE_PERMANENT;
+	{
+		if (stmt->partbound)
+		{
+			Oid		parentOid = linitial_oid(inheritOids);
+			char	relpersistence = get_rel_persistence(parentOid);
+
+			Assert(list_length(inheritOids) == 1);
+			/*
+			 * The parent's persistence is logged or unlogged, so rely on
+			 * it when creating the new relation.
+			 */
+			if (relpersistence != RELPERSISTENCE_TEMP)
+				stmt->relation->relpersistence = relpersistence;
+		}
+		else
+			stmt->relation->relpersistence = RELPERSISTENCE_PERMANENT;
+	}
 
 	/*
 	 * Select tablespace to use: an explicitly indicated one, or (in the case
@@ -5426,6 +5453,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
 			break;
 		case AT_SetLogged:		/* SET LOGGED */
 		case AT_SetUnLogged:	/* SET UNLOGGED */
+
+			/*
+			 * Only do this for partitioned tables, for which this is just a
+			 * catalog change.  Tables with storage are handled by Phase 3.
+			 */
+			if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE &&
+				tab->chgPersistence)
+				ATExecSetPersistenceNoStorage(rel, tab->newrelpersistence);
 			break;
 		case AT_DropOids:		/* SET WITHOUT OIDS */
 			/* nothing to do here, oid columns don't exist anymore */
@@ -15542,6 +15577,79 @@ ATExecSetAccessMethodNoStorage(Relation rel, Oid newAccessMethodId)
 	table_close(pg_class, RowExclusiveLock);
 }
 
+/*
+ * Special handling of ALTER TABLE SET LOGGED/UNLOGGED for relations with no
+ * storage that have an interest in changing their persistence.
+ *
+ * Since these have no storage, setting the persistence to permanent or
+ * unlogged is a catalog-only operation.  This needs to switch the
+ * persistence of all sequences and indexes related to this relation.
+ */
+static void
+ATExecSetPersistenceNoStorage(Relation rel, char newrelpersistence)
+{
+	Relation	pg_class;
+	HeapTuple	tuple;
+	List	   *reloids;	/* for indexes and sequences */
+	ListCell   *elt;
+	Form_pg_class rd_rel;
+	Oid			reloid = RelationGetRelid(rel);
+
+	/*
+	 * Shouldn't be called on relations having storage; these are processed in
+	 * phase 3.
+	 */
+	Assert(!RELKIND_HAS_STORAGE(rel->rd_rel->relkind));
+
+	/* Get a modifiable copy of the relation's pg_class row. */
+	pg_class = table_open(RelationRelationId, RowExclusiveLock);
+
+	tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(reloid));
+	if (!HeapTupleIsValid(tuple))
+		elog(ERROR, "cache lookup failed for relation %u", reloid);
+	rd_rel = (Form_pg_class) GETSTRUCT(tuple);
+
+	/* Leave if no update required */
+	if (rd_rel->relpersistence == newrelpersistence)
+	{
+		heap_freetuple(tuple);
+		table_close(pg_class, RowExclusiveLock);
+		return;
+	}
+
+	/* Update the pg_class row. */
+	rd_rel->relpersistence = newrelpersistence;
+	CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
+
+	InvokeObjectPostAlterHook(RelationRelationId, RelationGetRelid(rel), 0);
+
+	heap_freetuple(tuple);
+
+	/* Update the per-sequence and per-index relpersistence */
+	reloids = getOwnedSequences(reloid);
+	reloids = list_union_oid(reloids, RelationGetIndexList(rel));
+	foreach(elt, reloids)
+	{
+		Oid			classoid = lfirst_oid(elt);
+
+		tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(classoid));
+		if (!HeapTupleIsValid(tuple))
+			elog(ERROR, "cache lookup failed for relation %u", classoid);
+		rd_rel = (Form_pg_class) GETSTRUCT(tuple);
+
+		rd_rel->relpersistence = newrelpersistence;
+		CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
+		InvokeObjectPostAlterHook(RelationRelationId, classoid, 0);
+
+		heap_freetuple(tuple);
+	}
+
+	/* Make sure the persistence changes are visible */
+	CommandCounterIncrement();
+
+	table_close(pg_class, RowExclusiveLock);
+}
+
 /*
  * ALTER TABLE SET TABLESPACE
  */
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
index 7666c76238..5071e7d963 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -3597,6 +3597,94 @@ ALTER TABLE logged1 SET UNLOGGED; -- silently do nothing
 DROP TABLE logged3;
 DROP TABLE logged2;
 DROP TABLE logged1;
+-- SET LOGGED/UNLOGGED with partitioned tables
+CREATE TABLE logged_part_1(f1 SERIAL PRIMARY KEY)
+  PARTITION BY LIST (f1); -- has sequence, index
+CREATE TABLE logged_part_2(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged_part_1)
+  PARTITION BY LIST (f1); -- foreign key
+CREATE TABLE logged_part_3(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged_part_3)
+  PARTITION BY LIST (f1); -- self-referencing foreign key
+-- Check relpersistence of all the objects created.
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part'
+  ORDER BY relname;
+       relname        | relpersistence 
+----------------------+----------------
+ logged_part_1        | p
+ logged_part_1_f1_seq | p
+ logged_part_1_pkey   | p
+ logged_part_2        | p
+ logged_part_2_f1_seq | p
+ logged_part_2_pkey   | p
+ logged_part_3        | p
+ logged_part_3_f1_seq | p
+ logged_part_3_pkey   | p
+(9 rows)
+
+ALTER TABLE logged_part_1 SET UNLOGGED; -- fails as a foreign-key exists
+ERROR:  could not change table "logged_part_1" to unlogged because it references logged table "logged_part_2"
+ALTER TABLE logged_part_2 SET UNLOGGED;
+ALTER TABLE logged_part_3 SET UNLOGGED; -- skip self-referencing foreign key
+-- Re-check relpersistence of all the objects created.
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part'
+  ORDER BY relname;
+       relname        | relpersistence 
+----------------------+----------------
+ logged_part_1        | p
+ logged_part_1_f1_seq | p
+ logged_part_1_pkey   | p
+ logged_part_2        | u
+ logged_part_2_f1_seq | u
+ logged_part_2_pkey   | u
+ logged_part_3        | u
+ logged_part_3_f1_seq | u
+ logged_part_3_pkey   | u
+(9 rows)
+
+ALTER TABLE logged_part_1 SET LOGGED; -- no-op
+ALTER TABLE logged_part_2 SET LOGGED;
+ALTER TABLE logged_part_3 SET LOGGED; -- skip self-referencing foreign key
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_[2|3]'
+  ORDER BY relname;
+       relname        | relpersistence 
+----------------------+----------------
+ logged_part_2        | p
+ logged_part_2_f1_seq | p
+ logged_part_2_pkey   | p
+ logged_part_3        | p
+ logged_part_3_f1_seq | p
+ logged_part_3_pkey   | p
+(6 rows)
+
+-- Partitions
+CREATE TABLE logged_part_2_1 PARTITION OF logged_part_2
+  FOR VALUES IN (1); -- permanent, inherited
+CREATE UNLOGGED TABLE logged_part_2_2 PARTITION OF logged_part_2
+  FOR VALUES IN (2); -- unlogged, not inherited
+ALTER TABLE logged_part_2 SET UNLOGGED;
+CREATE TABLE logged_part_2_3 PARTITION OF logged_part_2
+  FOR VALUES IN (3); -- unlogged, inherited
+CREATE LOGGED TABLE logged_part_2_4 PARTITION OF logged_part_2
+  FOR VALUES IN (4); -- logged, not inherited
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
+  ORDER BY relname;
+       relname        | relpersistence 
+----------------------+----------------
+ logged_part_2        | u
+ logged_part_2_1      | p
+ logged_part_2_1_pkey | p
+ logged_part_2_2      | u
+ logged_part_2_2_pkey | u
+ logged_part_2_3      | u
+ logged_part_2_3_pkey | u
+ logged_part_2_4      | p
+ logged_part_2_4_pkey | p
+ logged_part_2_f1_seq | u
+ logged_part_2_pkey   | u
+(11 rows)
+
+DROP TABLE logged_part_3;
+DROP TABLE logged_part_2;
+DROP TABLE logged_part_1;
 -- test ADD COLUMN IF NOT EXISTS
 CREATE TABLE test_add_column(c1 integer);
 \d test_add_column
diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql
index 9df5a63bdf..30aa62d256 100644
--- a/src/test/regress/sql/alter_table.sql
+++ b/src/test/regress/sql/alter_table.sql
@@ -2259,6 +2259,43 @@ DROP TABLE logged3;
 DROP TABLE logged2;
 DROP TABLE logged1;
 
+-- SET LOGGED/UNLOGGED with partitioned tables
+CREATE TABLE logged_part_1(f1 SERIAL PRIMARY KEY)
+  PARTITION BY LIST (f1); -- has sequence, index
+CREATE TABLE logged_part_2(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged_part_1)
+  PARTITION BY LIST (f1); -- foreign key
+CREATE TABLE logged_part_3(f1 SERIAL PRIMARY KEY, f2 INTEGER REFERENCES logged_part_3)
+  PARTITION BY LIST (f1); -- self-referencing foreign key
+-- Check relpersistence of all the objects created.
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part'
+  ORDER BY relname;
+ALTER TABLE logged_part_1 SET UNLOGGED; -- fails as a foreign-key exists
+ALTER TABLE logged_part_2 SET UNLOGGED;
+ALTER TABLE logged_part_3 SET UNLOGGED; -- skip self-referencing foreign key
+-- Re-check relpersistence of all the objects created.
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part'
+  ORDER BY relname;
+ALTER TABLE logged_part_1 SET LOGGED; -- no-op
+ALTER TABLE logged_part_2 SET LOGGED;
+ALTER TABLE logged_part_3 SET LOGGED; -- skip self-referencing foreign key
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_[2|3]'
+  ORDER BY relname;
+-- Partitions
+CREATE TABLE logged_part_2_1 PARTITION OF logged_part_2
+  FOR VALUES IN (1); -- permanent, inherited
+CREATE UNLOGGED TABLE logged_part_2_2 PARTITION OF logged_part_2
+  FOR VALUES IN (2); -- unlogged, not inherited
+ALTER TABLE logged_part_2 SET UNLOGGED;
+CREATE TABLE logged_part_2_3 PARTITION OF logged_part_2
+  FOR VALUES IN (3); -- unlogged, inherited
+CREATE LOGGED TABLE logged_part_2_4 PARTITION OF logged_part_2
+  FOR VALUES IN (4); -- logged, not inherited
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
+  ORDER BY relname;
+DROP TABLE logged_part_3;
+DROP TABLE logged_part_2;
+DROP TABLE logged_part_1;
+
 -- test ADD COLUMN IF NOT EXISTS
 CREATE TABLE test_add_column(c1 integer);
 \d test_add_column
diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml
index ebd8c62038..7937194462 100644
--- a/doc/src/sgml/ref/alter_table.sgml
+++ b/doc/src/sgml/ref/alter_table.sgml
@@ -803,6 +803,12 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
       (for identity or serial columns).  However, it is also possible to
       change the persistence of such sequences separately.
      </para>
+
+     <para>
+      Setting this property for a partitioned table has no direct effect,
+      because such tables have no storage of their own, but the configured
+      value will be inherited by newly-created partitions.
+     </para>
     </listitem>
    </varlistentry>
 
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index 29dfd68dc8..f65c6d14c7 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -216,6 +216,11 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
       If this is specified, any sequences created together with the permanent
       table (for identity or serial columns) are also created as permanent.
      </para>
+
+     <para>
+      When applied to a partitioned table, newly-created partitions and
+      their objects (sequences and indexes) will inherit this property.
+     </para>
     </listitem>
    </varlistentry>
 
-- 
2.43.0



  [text/x-diff] v2-0004-Recurse-ALTER-TABLE-SET-LOGGED-UNLOGGED-for-parti.patch (7.1K, ../../[email protected]/5-v2-0004-Recurse-ALTER-TABLE-SET-LOGGED-UNLOGGED-for-parti.patch)
  download | inline diff:
From 0f2d517d28042a16d3ebd89d18327f0cfc7525c5 Mon Sep 17 00:00:00 2001
From: Michael Paquier <[email protected]>
Date: Thu, 2 May 2024 14:43:56 +0900
Subject: [PATCH v2 4/4] Recurse ALTER TABLE SET LOGGED/UNLOGGED for
 partitioned tables

This commit recurses the command of $subject to apply on all the
partitions of a partitioned table, except if ONLY is used.  Regression
tests are expanded for both cases, with multiple levels of partitioning.
---
 src/backend/commands/tablecmds.c          |  2 +
 src/test/regress/expected/alter_table.out | 91 +++++++++++++++++++----
 src/test/regress/sql/alter_table.sql      | 15 ++++
 doc/src/sgml/ref/alter_table.sgml         |  6 +-
 4 files changed, 97 insertions(+), 17 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 8854e31f14..8f18b7aa39 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5075,6 +5075,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						 errmsg("cannot change persistence setting twice")));
 			ATPrepSetPersistence(tab, rel, true);
+			ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode, context);
 			pass = AT_PASS_MISC;
 			break;
 		case AT_SetUnLogged:	/* SET UNLOGGED */
@@ -5084,6 +5085,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						 errmsg("cannot change persistence setting twice")));
 			ATPrepSetPersistence(tab, rel, false);
+			ATSimpleRecursion(wqueue, rel, cmd, recurse, lockmode, context);
 			pass = AT_PASS_MISC;
 			break;
 		case AT_DropOids:		/* SET WITHOUT OIDS */
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
index 5071e7d963..8ec5c668c8 100644
--- a/src/test/regress/expected/alter_table.out
+++ b/src/test/regress/expected/alter_table.out
@@ -3665,22 +3665,85 @@ CREATE TABLE logged_part_2_3 PARTITION OF logged_part_2
   FOR VALUES IN (3); -- unlogged, inherited
 CREATE LOGGED TABLE logged_part_2_4 PARTITION OF logged_part_2
   FOR VALUES IN (4); -- logged, not inherited
+-- Partitions of partitions
+CREATE TABLE logged_part_2_56 PARTITION OF logged_part_2
+  FOR VALUES IN (5, 6) PARTITION BY LIST(f1);
+CREATE TABLE logged_part_2_5 PARTITION OF logged_part_2_56
+  FOR VALUES IN (5); -- unlogged, inherited
+CREATE LOGGED TABLE logged_part_2_6 PARTITION OF logged_part_2_56
+  FOR VALUES IN (6); -- logged, not inherited
 SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
   ORDER BY relname;
-       relname        | relpersistence 
-----------------------+----------------
- logged_part_2        | u
- logged_part_2_1      | p
- logged_part_2_1_pkey | p
- logged_part_2_2      | u
- logged_part_2_2_pkey | u
- logged_part_2_3      | u
- logged_part_2_3_pkey | u
- logged_part_2_4      | p
- logged_part_2_4_pkey | p
- logged_part_2_f1_seq | u
- logged_part_2_pkey   | u
-(11 rows)
+        relname        | relpersistence 
+-----------------------+----------------
+ logged_part_2         | u
+ logged_part_2_1       | u
+ logged_part_2_1_pkey  | u
+ logged_part_2_2       | u
+ logged_part_2_2_pkey  | u
+ logged_part_2_3       | u
+ logged_part_2_3_pkey  | u
+ logged_part_2_4       | p
+ logged_part_2_4_pkey  | p
+ logged_part_2_5       | u
+ logged_part_2_56      | u
+ logged_part_2_56_pkey | u
+ logged_part_2_5_pkey  | u
+ logged_part_2_6       | p
+ logged_part_2_6_pkey  | p
+ logged_part_2_f1_seq  | u
+ logged_part_2_pkey    | u
+(17 rows)
+
+-- All partitions are logged.
+ALTER TABLE logged_part_2 SET LOGGED;
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
+  ORDER BY relname;
+        relname        | relpersistence 
+-----------------------+----------------
+ logged_part_2         | p
+ logged_part_2_1       | p
+ logged_part_2_1_pkey  | p
+ logged_part_2_2       | p
+ logged_part_2_2_pkey  | p
+ logged_part_2_3       | p
+ logged_part_2_3_pkey  | p
+ logged_part_2_4       | p
+ logged_part_2_4_pkey  | p
+ logged_part_2_5       | p
+ logged_part_2_56      | p
+ logged_part_2_56_pkey | p
+ logged_part_2_5_pkey  | p
+ logged_part_2_6       | p
+ logged_part_2_6_pkey  | p
+ logged_part_2_f1_seq  | p
+ logged_part_2_pkey    | p
+(17 rows)
+
+-- Only the partitioned partition is unlogged.
+ALTER TABLE ONLY logged_part_2_56 SET UNLOGGED;
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
+  ORDER BY relname;
+        relname        | relpersistence 
+-----------------------+----------------
+ logged_part_2         | p
+ logged_part_2_1       | p
+ logged_part_2_1_pkey  | p
+ logged_part_2_2       | p
+ logged_part_2_2_pkey  | p
+ logged_part_2_3       | p
+ logged_part_2_3_pkey  | p
+ logged_part_2_4       | p
+ logged_part_2_4_pkey  | p
+ logged_part_2_5       | p
+ logged_part_2_56      | u
+ logged_part_2_56_pkey | u
+ logged_part_2_5_pkey  | p
+ logged_part_2_6       | p
+ logged_part_2_6_pkey  | p
+ logged_part_2_f1_seq  | p
+ logged_part_2_pkey    | p
+(17 rows)
 
 DROP TABLE logged_part_3;
 DROP TABLE logged_part_2;
diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql
index 30aa62d256..3393c67b9c 100644
--- a/src/test/regress/sql/alter_table.sql
+++ b/src/test/regress/sql/alter_table.sql
@@ -2290,6 +2290,21 @@ CREATE TABLE logged_part_2_3 PARTITION OF logged_part_2
   FOR VALUES IN (3); -- unlogged, inherited
 CREATE LOGGED TABLE logged_part_2_4 PARTITION OF logged_part_2
   FOR VALUES IN (4); -- logged, not inherited
+-- Partitions of partitions
+CREATE TABLE logged_part_2_56 PARTITION OF logged_part_2
+  FOR VALUES IN (5, 6) PARTITION BY LIST(f1);
+CREATE TABLE logged_part_2_5 PARTITION OF logged_part_2_56
+  FOR VALUES IN (5); -- unlogged, inherited
+CREATE LOGGED TABLE logged_part_2_6 PARTITION OF logged_part_2_56
+  FOR VALUES IN (6); -- logged, not inherited
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
+  ORDER BY relname;
+-- All partitions are logged.
+ALTER TABLE logged_part_2 SET LOGGED;
+SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
+  ORDER BY relname;
+-- Only the partitioned partition is unlogged.
+ALTER TABLE ONLY logged_part_2_56 SET UNLOGGED;
 SELECT relname, relpersistence FROM pg_class WHERE relname ~ '^logged_part_2'
   ORDER BY relname;
 DROP TABLE logged_part_3;
diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml
index 7937194462..79827796cc 100644
--- a/doc/src/sgml/ref/alter_table.sgml
+++ b/doc/src/sgml/ref/alter_table.sgml
@@ -805,9 +805,9 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
      </para>
 
      <para>
-      Setting this property for a partitioned table has no direct effect,
-      because such tables have no storage of their own, but the configured
-      value will be inherited by newly-created partitions.
+      Setting this property on a partitioned table updates any partitions
+      attached to it, unless <literal>ONLY</literal> is specified. The
+      configured value is inherited by newly-created partitions.
      </para>
     </listitem>
    </varlistentry>
-- 
2.43.0



  [application/pgp-signature] signature.asc (833B, ../../[email protected]/6-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
  2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
  2024-04-24 23:55   ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-05-02 06:06     ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
@ 2024-08-27 21:01       ` Nathan Bossart <[email protected]>
  2024-08-29 06:44         ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  1 sibling, 1 reply; 8+ messages in thread

From: Nathan Bossart @ 2024-08-27 21:01 UTC (permalink / raw)
  To: Michael Paquier <[email protected]>; +Cc: David G. Johnston <[email protected]>; Postgres hackers <[email protected]>

On Thu, May 02, 2024 at 03:06:51PM +0900, Michael Paquier wrote:
> The case of a temporary persistence is actually *very* tricky.  The
> namespace, where the relation is created, is guessed and locked with
> permission checks done based on the RangeVar when the CreateStmt is
> transformed, which is before we try to look at its inheritance tree to
> find its partitioned parent.  So we would somewhat need to shortcut
> the existing RangeVar lookup and include the parents in the loop to
> find out the correct namespace.  And this is much earlier than now.
> The code complexity is not trivial, so I am getting cold feet when
> trying to support this case in a robust fashion.  For now, I have
> discarded this case and focused on the main problem with SET LOGGED
> and UNLOGGED.
> 
> Switching between logged <-> unlogged does not have such
> complications, because the namespace where the relation is created is
> going to be the same.  So we won't lock or perform permission checks
> on an incorrect namespace.

I've been thinking about this thread some more, and I'm finding myself -0.5
for adding relpersistence inheritance for UNLOGGED.  There are a few
reasons:

* Existing partitioned tables may be marked UNLOGGED, and after upgrade,
  new partitions would be UNLOGGED unless the user discovers that they need
  to begin specifying LOGGED or change the persistence of the partitioned
  table.  I've seen many problems with UNLOGGED over the years, so I am
  wary about anything that might increase the probability of someone using
  it accidentally.

* I don't think partitions inheriting persistence is necessarily intuitive.
  IIUC there's nothing stopping you from having a mix of LOGGED and
  UNLOGGED partitions, so it's not clear to me why we should assume that
  users want them to be the same by default.  IMHO UNLOGGED is dangerous
  enough that we really want users to unambiguously indicate that's what
  they want.

* Inheriting certain persistences (e.g., UNLOGGED) and not others (e.g.,
  TEMPORARY) seems confusing.  Furthermore, if a partitioned table is
  marked TEMPORARY, its partitions must also be marked TEMPORARY.  There is
  no such restriction when a partitioned table is marked UNLOGGED.

My current thinking is that it would be better to disallow marking
partitioned tables as LOGGED/UNLOGGED and continue to have users explicitly
specify what they want for each partition.  It'd still probably be good to
expand the documentation, but a clear ERROR when trying to set a
partitioned table as UNLOGGED would hopefully clue folks in.

-- 
nathan






^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
  2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
  2024-04-24 23:55   ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-05-02 06:06     ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-08-27 21:01       ` Re: Partitioned tables and [un]loggedness Nathan Bossart <[email protected]>
@ 2024-08-29 06:44         ` Michael Paquier <[email protected]>
  0 siblings, 0 replies; 8+ messages in thread

From: Michael Paquier @ 2024-08-29 06:44 UTC (permalink / raw)
  To: Nathan Bossart <[email protected]>; +Cc: David G. Johnston <[email protected]>; Postgres hackers <[email protected]>

On Tue, Aug 27, 2024 at 04:01:58PM -0500, Nathan Bossart wrote:
> I've been thinking about this thread some more, and I'm finding myself -0.5
> for adding relpersistence inheritance for UNLOGGED.  There are a few
> reasons:
> 
> * Existing partitioned tables may be marked UNLOGGED, and after upgrade,
>   new partitions would be UNLOGGED unless the user discovers that they need
>   to begin specifying LOGGED or change the persistence of the partitioned
>   table.  I've seen many problems with UNLOGGED over the years, so I am
>   wary about anything that might increase the probability of someone using
>   it accidentally.
> 
> * I don't think partitions inheriting persistence is necessarily intuitive.
>   IIUC there's nothing stopping you from having a mix of LOGGED and
>   UNLOGGED partitions, so it's not clear to me why we should assume that
>   users want them to be the same by default.  IMHO UNLOGGED is dangerous
>   enough that we really want users to unambiguously indicate that's what
>   they want.

Okay.  Thanks for sharing an opinion.

> * Inheriting certain persistences (e.g., UNLOGGED) and not others (e.g.,
>   TEMPORARY) seems confusing.  Furthermore, if a partitioned table is
>   marked TEMPORARY, its partitions must also be marked TEMPORARY.  There is
>   no such restriction when a partitioned table is marked UNLOGGED.

The reason for temporary tables is different though: we expect
everything to be gone once the backend that created these relations is
gone.  If persistence cocktails were allowed, the worse thing that
could happen would be to have a partitioned table that had temporary
partitions; its catalog state can easily get broken depending on the
DDLs issued on it.  Valid partitioned index that should not be once
the partitions are gone, for example, which would require more exit
logic to flip states in pg_class, pg_index, etc.

> My current thinking is that it would be better to disallow marking
> partitioned tables as LOGGED/UNLOGGED and continue to have users explicitly
> specify what they want for each partition.  It'd still probably be good to
> expand the documentation, but a clear ERROR when trying to set a
> partitioned table as UNLOGGED would hopefully clue folks in.

The addition of the new LOGGED keyword is not required if we limit
ourselves to an error when defining UNLOGGED, so if we drop this
proposal, let's also drop this part entirely and keep DefineRelation()
simpler.  Actually, is really issuing an error the best thing we can
do after so many years allowing this grammar flavor to go through,
even if it is perhaps accidental?  relpersistence is marked correctly
for partitioned tables, it's just useless.  Expanding the
documentation sounds fine to me, one way or the other, to tell what
happens with partitioned tables.

By the way, I was looking at this patch series, and still got annoyed
with the code duplication with ALTER TABLE SET LOGGED/UNLOGGED, so
I've done something about that for now.
--
Michael


Attachments:

  [application/pgp-signature] signature.asc (833B, ../../[email protected]/2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: Partitioned tables and [un]loggedness
  2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
  2024-04-24 23:55   ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
  2024-05-02 06:06     ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
@ 2024-09-01 05:24       ` Junwang Zhao <[email protected]>
  1 sibling, 0 replies; 8+ messages in thread

From: Junwang Zhao @ 2024-09-01 05:24 UTC (permalink / raw)
  To: Michael Paquier <[email protected]>; +Cc: David G. Johnston <[email protected]>; Nathan Bossart <[email protected]>; Postgres hackers <[email protected]>

On Thu, May 2, 2024 at 2:07 PM Michael Paquier <[email protected]> wrote:
>
> On Thu, Apr 25, 2024 at 08:55:27AM +0900, Michael Paquier wrote:
> > On Wed, Apr 24, 2024 at 04:43:58PM -0700, David G. Johnston wrote:
> >> My point is that if you feel that treating logged as a copy-able property
> >> is OK then doing the following should also just work:
> >>
> >> postgres=# create temp table parentt ( id integer ) partition by range (id);
> >> CREATE TABLE
> >> postgres=# create table child10t partition of parentt for values from (0)
> >> to (9);
> >> ERROR:  cannot create a permanent relation as partition of temporary
> >> relation "parentt"
> >>
> >> i.e., child10t should be created as a temporary partition under parentt.
> >
> > Ah, indeed, I've missed your point here.  Lifting the error and
> > inheriting temporary in this case would make sense.
>
> The case of a temporary persistence is actually *very* tricky.  The
> namespace, where the relation is created, is guessed and locked with
> permission checks done based on the RangeVar when the CreateStmt is
> transformed, which is before we try to look at its inheritance tree to
> find its partitioned parent.  So we would somewhat need to shortcut
> the existing RangeVar lookup and include the parents in the loop to
> find out the correct namespace.  And this is much earlier than now.
> The code complexity is not trivial, so I am getting cold feet when
> trying to support this case in a robust fashion.  For now, I have
> discarded this case and focused on the main problem with SET LOGGED
> and UNLOGGED.
>
> Switching between logged <-> unlogged does not have such
> complications, because the namespace where the relation is created is
> going to be the same.  So we won't lock or perform permission checks
> on an incorrect namespace.
>
> The addition of LOGGED makes the logic deciding how the loggedness of
> a partition table based on its partitioned table or the query quite
> easy to follow, but this needs some safety nets in the sequence, view
> and CTAS code paths to handle with the case where the query specifies
> no relpersistence.
>
> I have also looked at support for ONLY, and I've been surprised that
> it is not that complicated.  tablecmds.c has a ATSimpleRecursion()
> that is smart enough to do an inheritance tree lookup and apply the
> rewrites where they should happen in the step 3 of ALTER TABLE, while
> handling ONLY on its own.  The relpersistence of partitioned tables is
> updated in step 2, with the catalog changes.
>
> Attached is a new patch series:
> - 0001 refactors some code around ATPrepChangePersistence() that I
> found confusing after applying the operation to partitioned tables.
> - 0002 adds support for a LOGGED keyword.
> - 0003 expands ALTER TABLE SET [UN]LOGGED to partitioned tables,
> without recursion to partitions.
> - 0004 adds the recursion logic, expanding regression tests to show
> the difference.
>
> 0003 and 0004 should be merged together, I think.  Still, splitting
> them makes reviews a bit easier.
> --
> Michael

While reviewing the patches, I found a weird error msg:

+ALTER TABLE logged_part_1 SET UNLOGGED; -- fails as a foreign-key exists
+ERROR:  could not change table "logged_part_1" to unlogged because it
references logged table "logged_part_2"

should this be *it is referenced by* here?

The error msg is from ATPrepChangePersistence, and I think we should
do something like:

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index b3cc6f8f69..30fbc3836a 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -16986,7 +16986,7 @@ ATPrepChangePersistence(AlteredTableInfo *tab,
Relation rel, bool toLogged)
                                if (RelationIsPermanent(foreignrel))
                                        ereport(ERROR,

(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
-                                                        errmsg("could
not change table \"%s\" to unlogged because it references logged table
\"%s\"",
+                                                        errmsg("could
not change table \"%s\" to unlogged because it is referenced by logged
table \"%s\"",


What do you think?

-- 
Regards
Junwang Zhao






^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2024-09-01 05:24 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-01-01 20:04 [PATCH] hex squash commit Bruce Momjian <[email protected]>
2024-04-24 23:35 Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
2024-04-24 23:43 ` Re: Partitioned tables and [un]loggedness David G. Johnston <[email protected]>
2024-04-24 23:55   ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
2024-05-02 06:06     ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
2024-08-27 21:01       ` Re: Partitioned tables and [un]loggedness Nathan Bossart <[email protected]>
2024-08-29 06:44         ` Re: Partitioned tables and [un]loggedness Michael Paquier <[email protected]>
2024-09-01 05:24       ` Re: Partitioned tables and [un]loggedness Junwang Zhao <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox