Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 61d1396

Browse files
knizhnikkelvich
authored andcommitted
Fix problem with process utulity statement context filter
1 parent feaa891 commit 61d1396

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

multimaster--1.0.sql

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,23 @@ BEGIN
9999
FOR seq_class IN
100100
SELECT * FROM pg_class WHERE pg_class.relkind = 'S'
101101
LOOP
102-
EXECUTE 'select * from ' || seq_class.relname || ';' INTO seq_tuple;
103-
IF seq_tuple.increment_by != max_nodes THEN
104-
altered := true;
105-
RAISE NOTICE 'Altering step for sequence % to %.', seq_tuple.sequence_name, max_nodes;
106-
EXECUTE 'ALTER SEQUENCE ' || seq_class.relname || ' INCREMENT BY ' || max_nodes || ';';
107-
END IF;
108-
IF (seq_tuple.last_value % max_nodes) != node_id THEN
109-
altered := true;
110-
new_start := (seq_tuple.last_value / max_nodes + 1)*max_nodes + node_id;
111-
RAISE NOTICE 'Altering start for sequence % to %.', seq_tuple.sequence_name, new_start;
112-
EXECUTE 'ALTER SEQUENCE ' || seq_class.relname || ' RESTART WITH ' || new_start || ';';
113-
END IF;
102+
BEGIN
103+
EXECUTE 'select * from ' || seq_class.relname || ';' INTO seq_tuple;
104+
IF seq_tuple.increment_by != max_nodes THEN
105+
altered := true;
106+
RAISE NOTICE 'Altering step for sequence % to %.', seq_tuple.sequence_name, max_nodes;
107+
EXECUTE 'ALTER SEQUENCE ' || seq_class.relname || ' INCREMENT BY ' || max_nodes || ';';
108+
END IF;
109+
IF (seq_tuple.last_value % max_nodes) != node_id THEN
110+
altered := true;
111+
new_start := (seq_tuple.last_value / max_nodes + 1)*max_nodes + node_id;
112+
RAISE NOTICE 'Altering start for sequence % to %.', seq_tuple.sequence_name, new_start;
113+
EXECUTE 'ALTER SEQUENCE ' || seq_class.relname || ' RESTART WITH ' || new_start || ';';
114+
END IF;
115+
EXCEPTION
116+
WHEN OTHERS THEN
117+
RAISE NOTICE 'Failed to alter sequence %s', seq_class.relname;
118+
END;
114119
END LOOP;
115120
IF altered = false THEN
116121
RAISE NOTICE 'All found sequnces have proper params.';

multimaster.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "utils/builtins.h"
4747
#include "utils/memutils.h"
4848
#include "commands/dbcommands.h"
49+
#include "commands/extension.h"
4950
#include "postmaster/autovacuum.h"
5051
#include "storage/pmsignal.h"
5152
#include "storage/proc.h"
@@ -4879,7 +4880,8 @@ static void MtmProcessUtility(Node *parsetree, const char *queryString,
48794880
bool skipCommand = false;
48804881
bool executed = false;
48814882

4882-
MTM_LOG3("%d: Process utility statement %s", MyProcPid, queryString);
4883+
MTM_LOG3("%d: Process utility statement tag=%d, context=%d, issubtrans=%d, query=%s",
4884+
MyProcPid, nodeTag(parsetree), context, IsSubTransaction(), queryString);
48834885
switch (nodeTag(parsetree))
48844886
{
48854887
case T_TransactionStmt:
@@ -5133,7 +5135,13 @@ static void MtmProcessUtility(Node *parsetree, const char *queryString,
51335135
}
51345136

51355137
/* XXX: dirty. Clear on new tx */
5136-
if (!skipCommand && (context != PROCESS_UTILITY_SUBCOMMAND || MtmUtilityProcessedInXid != GetCurrentTransactionId()))
5138+
/* Some "black magic here":( We want to avoid redundant execution of utility statement by ProcessUtilitySlow (which is done with PROCESS_UTILITY_SUBCOMMAND).
5139+
* But if we allow only PROCESS_UTILITY_TOPLEVEL context, then we will not replicated DDL inside dynamic queries in plpgsql functions (see https://jira.postgrespro.ru/browse/CORE-526).
5140+
* If we disable only PROCESS_UTILITY_SUBCOMMAND, then we will get problems with "create extension" which is executed also in PROCESS_UTILITY_QUERY context.
5141+
* So workaround at this moment is to treat extension as special case.
5142+
* TODO: try to find right solution and rewrite this dummy check.
5143+
*/
5144+
if (!skipCommand && (context == PROCESS_UTILITY_TOPLEVEL || (context == PROCESS_UTILITY_QUERY && !creating_extension) || MtmUtilityProcessedInXid != GetCurrentTransactionId()))
51375145
MtmUtilityProcessedInXid = InvalidTransactionId;
51385146

51395147
if (!skipCommand && !MtmTx.isReplicated && (MtmUtilityProcessedInXid == InvalidTransactionId)) {

0 commit comments

Comments
 (0)