Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

Started by Bharath Rupireddyabout 4 years ago10 messages
#1Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com

Hi,

I recently did a small experiment to see how one can create extensions
properly in HA(primary-standby) setup.

Here are my findings:
1) ALTER SYSTEM SET or GUC(configuration parameters) settings are not
replicated to standby.
2) CREATE EXTENSION statements are replicated to standby.
3) If the extension doesn't need to be set up in
shared_preload_libraries GUC, no need to create extension on the
standby, it just works.
4) If the extension needs to be set up in shared_preload_libraries
GUC: the correct way to install the extension on both primary and
standby is:
a) set shared_preload_libraries GUC on primary, reload conf,
restart the primary to make the GUC effective.
b) set shared_preload_libraries GUC on standby, restart the
standby to make the GUC effective.
c) create extension on primary (we don't need to create extension
on standby as the create extension statements are replicated).
d) verify that the extension functions work on both primary and standby.
5) The extensions which perform writes to the database may not work on
standby as the write transactions are not allowed on the standby.
However, the create extension on the standby works just fine but the
functions it provides may not work.

I think I was successful in my experiment, please let me know if
anything is wrong in what I did.

Do we have the documentation on how to create extensions correctly in
HA setup? If what I did is correct and we don't have it documented,
can we have it somewhere in the existing HA related documentation?

Regards,
Bharath Rupireddy.

#2Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Bharath Rupireddy (#1)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

On Wed, Dec 1, 2021 at 12:31 PM Bharath Rupireddy
<bharath.rupireddyforpostgres@gmail.com> wrote:

Hi,

I recently did a small experiment to see how one can create extensions
properly in HA(primary-standby) setup.

Here are my findings:
1) ALTER SYSTEM SET or GUC(configuration parameters) settings are not
replicated to standby.
2) CREATE EXTENSION statements are replicated to standby.
3) If the extension doesn't need to be set up in
shared_preload_libraries GUC, no need to create extension on the
standby, it just works.
4) If the extension needs to be set up in shared_preload_libraries
GUC: the correct way to install the extension on both primary and
standby is:
a) set shared_preload_libraries GUC on primary, reload conf,
restart the primary to make the GUC effective.
b) set shared_preload_libraries GUC on standby, restart the
standby to make the GUC effective.
c) create extension on primary (we don't need to create extension
on standby as the create extension statements are replicated).
d) verify that the extension functions work on both primary and standby.
5) The extensions which perform writes to the database may not work on
standby as the write transactions are not allowed on the standby.
However, the create extension on the standby works just fine but the
functions it provides may not work.

I think I was successful in my experiment, please let me know if
anything is wrong in what I did.

Do we have the documentation on how to create extensions correctly in
HA setup? If what I did is correct and we don't have it documented,
can we have it somewhere in the existing HA related documentation?

I'm thinking of adding the above steps into the "Additional Supplied
Modules" section documentation. Any thoughts please?

[1]: https://www.postgresql.org/docs/devel/contrib.html

Regards,
Bharath Rupireddy.

#3Peter Eisentraut
peter.eisentraut@enterprisedb.com
In reply to: Bharath Rupireddy (#2)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

On 03.12.21 15:28, Bharath Rupireddy wrote:

I'm thinking of adding the above steps into the "Additional Supplied
Modules" section documentation. Any thoughts please?

[1] - https://www.postgresql.org/docs/devel/contrib.html

The chapter about extensions is probably better:
https://www.postgresql.org/docs/devel/extend-extensions.html

#4Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Peter Eisentraut (#3)
1 attachment(s)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

On Tue, Dec 7, 2021 at 9:16 PM Peter Eisentraut
<peter.eisentraut@enterprisedb.com> wrote:

On 03.12.21 15:28, Bharath Rupireddy wrote:

I'm thinking of adding the above steps into the "Additional Supplied
Modules" section documentation. Any thoughts please?

[1] - https://www.postgresql.org/docs/devel/contrib.html

The chapter about extensions is probably better:
https://www.postgresql.org/docs/devel/extend-extensions.html

Thanks. Attaching v1 patch specifying the notes there. Please review.

Regards,
Bharath Rupireddy.

Attachments:

v1-0001-Document-creating-an-extension-in-replication-set.patchapplication/octet-stream; name=v1-0001-Document-creating-an-extension-in-replication-set.patchDownload
From 70ed66b128e0babbfc87f81817530befd0cf8c3f Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Thu, 9 Dec 2021 02:47:01 +0000
Subject: [PATCH v1] Document creating an extension in replication setup

This patch adds the steps to create an extension in a typical
postgres replication setup with primary and one or more standbys.
---
 doc/src/sgml/extend.sgml | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml
index e928894726..550668e78d 100644
--- a/doc/src/sgml/extend.sgml
+++ b/doc/src/sgml/extend.sgml
@@ -1408,6 +1408,43 @@ include $(PGXS)
      any particular database.
     </para>
    </sect2>
+
+   <sect2 id="create-extension-in-replication-setup">
+    <title>Creating An Extension In Replication Setup</title>
+
+    <para>
+     It is important to note few things before creating an extension in a
+     typical postgres replication setup with a primary and one or more standbys.
+     Firstly, <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     commands are replicated to standbys whereas <link linkend="sql-altersystem">ALTER SYSTEM</link>
+     commands or configuration parameters set in <filename>postgresql.conf</filename>
+     file are not.
+    </para>
+
+    <para>
+     If the extension's shared library doesn't need to be specified in
+     <xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>
+     configuration parameter, then create the extension on the primary, there
+     is no need to create it on the standbys as the <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command is replicated.
+    </para>
+
+    <para>
+     If the extension's shared library needs to be specified in
+     <xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>
+     configuration parameter, then set it either via <link linkend="sql-altersystem">ALTER SYSTEM</link>
+     command or <filename>postgresql.conf</filename> file on both primary and
+     standys, reload the <filename>postgresql.conf</filename> file and restart
+     the servers. Create the extension on the primary, there is no need to
+     create it on the standbys as the <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command is replicated.
+    </para>
+
+    <para>
+     Note that the extension functions which perform writes to the database
+     will not work on standbys as they allow read-only transactions.
+    </para>
+   </sect2>
   </sect1>
 
   <sect1 id="extend-pgxs">
-- 
2.25.1

#5Julien Rouhaud
rjuju123@gmail.com
In reply to: Bharath Rupireddy (#4)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

Hi,

On Thu, Dec 09, 2021 at 08:19:06AM +0530, Bharath Rupireddy wrote:

Thanks. Attaching v1 patch specifying the notes there. Please review.

I think that the common terminology is "module", not "extension". That's
especially important here as this information is also relevant for modules that
may come with an SQL level extension. This should be made clear in that new
documentation, same for the CREATE EXTENSION part that may not be relevant.

It also seems that this documentation is only aimed for physical replication.
It should also be explicitly stated as it might not be obvious for the intended
readers.

+     [...] set it either via <link linkend="sql-altersystem">ALTER SYSTEM</link>
+     command or <filename>postgresql.conf</filename> file on both primary and
+     standys, reload the <filename>postgresql.conf</filename> file and restart
+     the servers.

Isn't the reload a terrible advice? By definition changing
shared_preload_libraries isn't compatible with a simple reload and will emit
some error.

+     [...] Create the extension on the primary, there is no need to
+     create it on the standbys as the <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command is replicated.

The "no need" here is quite ambiguous, as it seems to indicate that trying to
create the extension on the standby will work but is unnecessary.

#6Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Julien Rouhaud (#5)
1 attachment(s)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

On Tue, Jan 18, 2022 at 1:26 PM Julien Rouhaud <rjuju123@gmail.com> wrote:

Hi,

On Thu, Dec 09, 2021 at 08:19:06AM +0530, Bharath Rupireddy wrote:

Thanks. Attaching v1 patch specifying the notes there. Please review.

I think that the common terminology is "module", not "extension". That's
especially important here as this information is also relevant for modules that
may come with an SQL level extension. This should be made clear in that new
documentation, same for the CREATE EXTENSION part that may not be relevant.

Thanks for reviewing this. The aim of the patch is to add how one can
create extensions with "CREATE EXTENSION" command in replication
setup, not sure why I should use the term "module". I hope you have
seen the usage of extension in the extend.sgml.

It also seems that this documentation is only aimed for physical replication.
It should also be explicitly stated as it might not be obvious for the intended
readers.

Yeah, I've changed the title and description accordingly.

+     [...] set it either via <link linkend="sql-altersystem">ALTER SYSTEM</link>
+     command or <filename>postgresql.conf</filename> file on both primary and
+     standys, reload the <filename>postgresql.conf</filename> file and restart
+     the servers.

Isn't the reload a terrible advice? By definition changing
shared_preload_libraries isn't compatible with a simple reload and will emit
some error.

Yes, it will emit the following messages. I removed the reload part.

2022-02-11 04:07:53.178 UTC [1206594] LOG: parameter
"shared_preload_libraries" cannot be changed without restarting the
server
2022-02-11 04:07:53.178 UTC [1206594] LOG: configuration file
"/home/bharath/postgres/inst/bin/data/postgresql.auto.conf" contains
errors; unaffected changes were applied

+     [...] Create the extension on the primary, there is no need to
+     create it on the standbys as the <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command is replicated.

The "no need" here is quite ambiguous, as it seems to indicate that trying to
create the extension on the standby will work but is unnecessary.

Modified.

Attaching v2, please have a look.

Regards,
Bharath Rupireddy.

Attachments:

v2-0001-Document-creating-an-extension-in-replication-set.patchapplication/octet-stream; name=v2-0001-Document-creating-an-extension-in-replication-set.patchDownload
From ff283308411fc3f220d9957393c14aa38109f52f Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Fri, 11 Feb 2022 04:44:24 +0000
Subject: [PATCH v2] Document creating an extension in replication setup

This patch adds the steps to create an extension in a typical
postgres replication setup with primary and one or more standbys.
---
 doc/src/sgml/extend.sgml | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml
index e928894726..de25b3a953 100644
--- a/doc/src/sgml/extend.sgml
+++ b/doc/src/sgml/extend.sgml
@@ -1408,6 +1408,42 @@ include $(PGXS)
      any particular database.
     </para>
    </sect2>
+
+   <sect2 id="create-extension-in-replication-setup">
+    <title>Creating An Extension In Physical Replication Setup</title>
+
+    <para>
+     It is important to note few things before creating an extension in a
+     typical postgres physical replication setup with a primary and one or more
+     standbys. Firstly, <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     commands are replicated to standbys whereas <link linkend="sql-altersystem">ALTER SYSTEM</link>
+     commands or configuration parameters set in <filename>postgresql.conf</filename>
+     file are not. Creating an extension directly on standbys with
+     <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command doesn't work as they allow read-only transactions.
+    </para>
+
+    <para>
+     If the extension's shared library doesn't need to be specified in
+     <xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>
+     configuration parameter, then create the extension on the primary, the
+     standbys will receive it via physical replication.
+    </para>
+
+    <para>
+     If the extension's shared library needs to be specified in
+     <xref linkend="guc-shared-preload-libraries"/><indexterm><primary>shared_preload_libraries</primary></indexterm>
+     configuration parameter, then set it either via <link linkend="sql-altersystem">ALTER SYSTEM</link>
+     command or <filename>postgresql.conf</filename> file on both primary and
+     standbys, and restart the servers. Create the extension on the primary,
+     the standbys will receive it via physical replication.
+    </para>
+
+    <para>
+     Note that the extension functions which perform writes to the database
+     will not work on standbys as they allow read-only transactions.
+    </para>
+   </sect2>
   </sect1>
 
   <sect1 id="extend-pgxs">
-- 
2.25.1

#7Julien Rouhaud
rjuju123@gmail.com
In reply to: Bharath Rupireddy (#6)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

On Fri, Feb 11, 2022 at 10:16:27AM +0530, Bharath Rupireddy wrote:

On Tue, Jan 18, 2022 at 1:26 PM Julien Rouhaud <rjuju123@gmail.com> wrote:

I think that the common terminology is "module", not "extension". That's
especially important here as this information is also relevant for modules that
may come with an SQL level extension. This should be made clear in that new
documentation, same for the CREATE EXTENSION part that may not be relevant.

Thanks for reviewing this. The aim of the patch is to add how one can
create extensions with "CREATE EXTENSION" command in replication
setup, not sure why I should use the term "module". I hope you have
seen the usage of extension in the extend.sgml.

The aim of this patch should be to clarify postgres configuration for
additional modules in physical replication, whether those includes an extension
or not. Your patch covers implication of modifying shared_preload_libraries,
are you saying that if there's no extension associated with that library it
shouldn't be covered?

A simple example is auto_explain, which also means that the documentation
should probbaly mention that shared_preload_libraries is only an example and
all configuration changes should be reported (and eventually adapted) on the
standby, like session_preload_libraries among others.

#8Greg Stark
stark@mit.edu
In reply to: Julien Rouhaud (#7)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

This doesn't seem to be getting any further attention. It sounds like
Julien didn't agree with the scope of the text. Bharath do you think
Julien's comments make sense? Will you have a chance to look at this?

#9Bharath Rupireddy
bharath.rupireddyforpostgres@gmail.com
In reply to: Greg Stark (#8)
1 attachment(s)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

On Fri, Mar 25, 2022 at 10:20 AM Greg Stark <stark@mit.edu> wrote:

This doesn't seem to be getting any further attention. It sounds like
Julien didn't agree with the scope of the text. Bharath do you think
Julien's comments make sense? Will you have a chance to look at this?

Thanks Greg. I was busy with other features.

Thanks Julien for the off-list discussion. I tried to address review
comments in the v3 patch attached. Now, I've added the notes in
high-availability.sgml which sort of suits more and closer to physical
replicatioin than contrib.sgml or extend.sgml.

Thoughts?

Regards,
Bharath Rupireddy.

Attachments:

v3-0001-Document-configuring-an-external-module-in-physic.patchapplication/octet-stream; name=v3-0001-Document-configuring-an-external-module-in-physic.patchDownload
From 840b277f86045f1a5f04593b8dae160edca36697 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Sun, 27 Mar 2022 03:29:56 +0000
Subject: [PATCH v3] Document configuring an external module in physical
 replication setup

---
 doc/src/sgml/high-availability.sgml | 41 +++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/doc/src/sgml/high-availability.sgml b/doc/src/sgml/high-availability.sgml
index 81fa26f985..16a46ceb16 100644
--- a/doc/src/sgml/high-availability.sgml
+++ b/doc/src/sgml/high-availability.sgml
@@ -1410,6 +1410,47 @@ synchronous_standby_names = 'ANY 2 (s1, s2, s3)'
      <literal>on</literal> and <literal>always</literal> modes.
    </para>
   </sect2>
+
+  <sect2 id="configure-external-modules-in-physical-replication-setup">
+    <title>Configuring External Modules in Physical Replication Setup</title>
+
+   <indexterm>
+     <primary>Configuring External Modules in Physical Replication Setup</primary>
+   </indexterm>
+
+    <para>
+     It is important to note few things before configuring an external module
+     in a typical postgres physical replication setup with a primary and one or
+     more standbys. Firstly, the module's shared library must be present on
+     both primary and standbys. If the module exposes SQL functions, running
+     <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command on primary is sufficient as standbys will receive it via physical
+     replication. Attempting to run <command>CREATE EXTENSION</command> on
+     standbys will anyways fail as they allow read-only transactions. The
+     module's shared library gets loaded upon first usage of any of its
+     functions on primary and standbys. 
+    </para>
+
+    <para>
+     If the module doesn't expose SQL functions, the shared library has to be
+     loaded separately on primary and standbys, either by
+     <link linkend="sql-load"><command>LOAD</command></link> command or by
+     setting parameter <xref linkend="guc-session-preload-libraries"/> or
+     <xref linkend="guc-shared-preload-libraries"/> or
+     <xref linkend="guc-local-preload-libraries"/>, depending on module's need.
+    </para>
+
+    <para>
+     If the module contains any configuration parameters, they need to be set
+     separately on primary and standbys separately as they aren't streamed via
+     physical replication (for that matter, any configuration parameter set
+     either in <filename>postgresql.conf</filename> file or by
+     <link linkend="sql-altersystem">ALTER SYSTEM</link> command aren't
+     streamed to standbys by the primary. However, the module can have
+     different configuration parameter values set on primary and standbys.
+    </para>
+ </sect2>
+
   </sect1>
 
   <sect1 id="warm-standby-failover">
-- 
2.25.1

#10Julien Rouhaud
rjuju123@gmail.com
In reply to: Bharath Rupireddy (#9)
Re: Is there any documentation on how to correctly create extensions in HA(primary-standby) setup?

Hi,

On Sun, Mar 27, 2022 at 09:07:12AM +0530, Bharath Rupireddy wrote:

On Fri, Mar 25, 2022 at 10:20 AM Greg Stark <stark@mit.edu> wrote:

This doesn't seem to be getting any further attention. It sounds like
Julien didn't agree with the scope of the text. Bharath do you think
Julien's comments make sense? Will you have a chance to look at this?

Thanks Greg. I was busy with other features.

Thanks Julien for the off-list discussion. I tried to address review
comments in the v3 patch attached. Now, I've added the notes in
high-availability.sgml which sort of suits more and closer to physical
replicatioin than contrib.sgml or extend.sgml.

+    [...] Firstly, the module's shared library must be present on
+     both primary and standbys.

I'm a bit confused with it. It looks like it means that the .so must be
physically present on both servers, but I'm assuming that you're talking about
shared_preload_libraries?

If yes, I still think it's worth documenting that it *needs* to be present on
the standbys *if* you want it to be enabled on the standby, including if it can
be promoted to a primary node. And that any related GUC also has to be
properly configured on all nodes (so maybe moving the last paragraph just after
this one?).

If no, maybe just saying that the module has to be installed and configured on
all nodes?

+     [...] If the module exposes SQL functions, running
+     <link linkend="sql-createextension"><command>CREATE EXTENSION</command></link>
+     command on primary is sufficient as standbys will receive it via physical
+     replication.

I think it's better to phrase it with something like "CREATE EXTENSION is
replicated in physical replication similarly to other DDL commands".

+     [...] The
+     module's shared library gets loaded upon first usage of any of its
+     functions on primary and standbys.

Is it worth documenting that? Note that this is only true if the lib isn't in
shared_preload_libraries and if it's a wrapper on top of a C function.

nitpicking: there's a trailing whitespace after "standbys."

+     If the module doesn't expose SQL functions, the shared library has to be
+     loaded separately on primary and standbys, either by
+     <link linkend="sql-load"><command>LOAD</command></link> command or by
+     setting parameter <xref linkend="guc-session-preload-libraries"/> or
+     <xref linkend="guc-shared-preload-libraries"/> or
+     <xref linkend="guc-local-preload-libraries"/>, depending on module's need.

I think this is also confusing. The need for preloading is entirely orthogonal
to SQL functions in the extension, especially since this is implying SQL
function over C-code. This should be reworded to go with the first paragraph I
think.