Skip to content

Commit 8ff44b7

Browse files
songjinzhoumarcocitus
authored andcommitted
fix the problem citusdata#5763 DROP OWNED BY fails to drop the schemas on the workers
1 parent 3b24c47 commit 8ff44b7

File tree

9 files changed

+230
-1
lines changed

9 files changed

+230
-1
lines changed

src/backend/distributed/commands/distribute_object_ops.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ static DistributeObjectOps Any_CreateRole = {
254254
.address = CreateRoleStmtObjectAddress,
255255
.markDistributed = true,
256256
};
257+
static DistributeObjectOps Any_DropOwned = {
258+
.deparse = DeparseDropOwnedStmt,
259+
.qualify = NULL,
260+
.preprocess = PreprocessDropOwnedStmt,
261+
.postprocess = NULL,
262+
.operationType = DIST_OPS_DROP,
263+
.address = NULL,
264+
.markDistributed = false,
265+
};
257266
static DistributeObjectOps Any_DropRole = {
258267
.deparse = DeparseDropRoleStmt,
259268
.qualify = NULL,
@@ -1658,6 +1667,11 @@ GetDistributeObjectOps(Node *node)
16581667
return &Any_DropRole;
16591668
}
16601669

1670+
case T_DropOwnedStmt:
1671+
{
1672+
return &Any_DropOwned;
1673+
}
1674+
16611675
case T_DropStmt:
16621676
{
16631677
DropStmt *stmt = castNode(DropStmt, node);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* owned.c
4+
* Commands for DROP OWNED statements.
5+
*
6+
* Copyright (c) Citus Data, Inc.
7+
*
8+
*-------------------------------------------------------------------------
9+
*/
10+
11+
#include "postgres.h"
12+
13+
#include "access/heapam.h"
14+
#include "access/htup_details.h"
15+
#include "access/genam.h"
16+
#include "access/table.h"
17+
#include "access/xact.h"
18+
#include "catalog/catalog.h"
19+
#include "catalog/pg_auth_members.h"
20+
#include "catalog/pg_authid.h"
21+
#include "catalog/pg_db_role_setting.h"
22+
#include "catalog/pg_type.h"
23+
#include "catalog/objectaddress.h"
24+
#include "commands/dbcommands.h"
25+
#include "distributed/citus_ruleutils.h"
26+
#include "distributed/citus_safe_lib.h"
27+
#include "distributed/commands.h"
28+
#include "distributed/commands/utility_hook.h"
29+
#include "distributed/deparser.h"
30+
#include "distributed/listutils.h"
31+
#include "distributed/coordinator_protocol.h"
32+
#include "distributed/metadata/distobject.h"
33+
#include "distributed/metadata_sync.h"
34+
#include "distributed/metadata/distobject.h"
35+
#include "distributed/multi_executor.h"
36+
#include "distributed/relation_access_tracking.h"
37+
#include "distributed/version_compat.h"
38+
#include "distributed/worker_transaction.h"
39+
#include "miscadmin.h"
40+
#include "nodes/makefuncs.h"
41+
#include "nodes/parsenodes.h"
42+
#include "nodes/pg_list.h"
43+
#include "parser/scansup.h"
44+
#include "utils/builtins.h"
45+
#include "utils/fmgroids.h"
46+
#include "utils/rel.h"
47+
#include "utils/varlena.h"
48+
#include "utils/syscache.h"
49+
50+
/*
51+
* PreprocessDropOwnedStmt finds the distributed role out of the ones
52+
* being dropped and unmarks them distributed and creates the drop statements
53+
* for the workers.
54+
*/
55+
List *
56+
PreprocessDropOwnedStmt(Node *node, const char *queryString,
57+
ProcessUtilityContext processUtilityContext)
58+
{
59+
DropOwnedStmt *stmt = castNode(DropOwnedStmt, node);
60+
List *allDropRoles = stmt->roles;
61+
62+
List *distributedDropRoles = FilterDistributedRoles(allDropRoles);
63+
if (list_length(distributedDropRoles) <= 0)
64+
{
65+
return NIL;
66+
}
67+
68+
if (!ShouldPropagate())
69+
{
70+
return NIL;
71+
}
72+
73+
EnsureCoordinator();
74+
75+
stmt->roles = distributedDropRoles;
76+
char *sql = DeparseTreeNode((Node *) stmt);
77+
stmt->roles = allDropRoles;
78+
79+
List *commands = list_make3(DISABLE_DDL_PROPAGATION,
80+
sql,
81+
ENABLE_DDL_PROPAGATION);
82+
83+
return NodeDDLTaskList(NON_COORDINATOR_NODES, commands);
84+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* deparse_owned_stmts.c
4+
* Functions to turn all Statement structures related to owned back
5+
* into sql.
6+
*
7+
* Copyright (c), Citus Data, Inc.
8+
*
9+
*-------------------------------------------------------------------------
10+
*/
11+
12+
#include "postgres.h"
13+
14+
#include "pg_version_compat.h"
15+
16+
#include "distributed/citus_ruleutils.h"
17+
#include "distributed/deparser.h"
18+
#include "lib/stringinfo.h"
19+
#include "nodes/parsenodes.h"
20+
#include "utils/builtins.h"
21+
22+
static void AppendDropOwnedStmt(StringInfo buf, DropOwnedStmt *stmt);
23+
static void AppendRoleList(StringInfo buf, List *roleList);
24+
25+
/*
26+
* DeparseDropOwnedStmt builds and returns a string representing of the
27+
* DropOwnedStmt for application on a remote server.
28+
*/
29+
char *
30+
DeparseDropOwnedStmt(Node *node)
31+
{
32+
DropOwnedStmt *stmt = castNode(DropOwnedStmt, node);
33+
34+
StringInfoData buf = { 0 };
35+
initStringInfo(&buf);
36+
37+
AppendDropOwnedStmt(&buf, stmt);
38+
39+
return buf.data;
40+
}
41+
42+
43+
/*
44+
* AppendDropOwnedStmt generates the string representation of the
45+
* DropOwnedStmt and appends it to the buffer.
46+
*/
47+
static void
48+
AppendDropOwnedStmt(StringInfo buf, DropOwnedStmt *stmt)
49+
{
50+
appendStringInfo(buf, "DROP OWNED BY ");
51+
52+
AppendRoleList(buf, stmt->roles);
53+
54+
if (stmt->behavior == DROP_RESTRICT)
55+
{
56+
appendStringInfo(buf, " RESTRICT");
57+
}
58+
else if (stmt->behavior == DROP_CASCADE)
59+
{
60+
appendStringInfo(buf, " CASCADE");
61+
}
62+
}
63+
64+
static void
65+
AppendRoleList(StringInfo buf, List *roleList)
66+
{
67+
ListCell *cell = NULL;
68+
foreach(cell, roleList)
69+
{
70+
Node *roleNode = (Node *) lfirst(cell);
71+
Assert(IsA(roleNode, RoleSpec) || IsA(roleNode, AccessPriv));
72+
char const *rolename = NULL;
73+
if (IsA(roleNode, RoleSpec))
74+
{
75+
rolename = RoleSpecString((RoleSpec *) roleNode, true);
76+
}
77+
if (IsA(roleNode, AccessPriv))
78+
{
79+
rolename = quote_identifier(((AccessPriv *) roleNode)->priv_name);
80+
}
81+
appendStringInfoString(buf, rolename);
82+
if (cell != list_tail(roleList))
83+
{
84+
appendStringInfo(buf, ", ");
85+
}
86+
}
87+
}
88+

src/include/distributed/commands.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ extern bool IsReindexWithParam_compat(ReindexStmt *stmt, char *paramName);
385385
extern List * CreateExtensionStmtObjectAddress(Node *stmt, bool missing_ok, bool
386386
isPostprocess);
387387

388+
/* owned.c - forward declarations */
389+
extern List *PreprocessDropOwnedStmt(Node *node, const char *queryString, ProcessUtilityContext processUtilityContext);
388390

389391
/* policy.c - forward declarations */
390392
extern List * CreatePolicyCommands(Oid relationId);

src/include/distributed/deparser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ extern char * DeparseCreateRoleStmt(Node *stmt);
196196
extern char * DeparseDropRoleStmt(Node *stmt);
197197
extern char * DeparseGrantRoleStmt(Node *stmt);
198198

199+
/* forward declarations for deparse_owned_stmts.c */
200+
extern char *DeparseDropOwnedStmt(Node *node);
201+
199202
/* forward declarations for deparse_extension_stmts.c */
200203
extern DefElem * GetExtensionOption(List *extensionOptions,
201204
const char *defname);

src/test/regress/expected/issue_5763.out

Whitespace-only changes.

src/test/regress/multi_schedule

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ test: multi_dropped_column_aliases foreign_key_restriction_enforcement
9595
test: binary_protocol
9696
test: alter_table_set_access_method
9797
test: alter_distributed_table
98-
test: issue_5248 issue_5099
98+
test: issue_5248 issue_5099 issue_5763
9999
test: object_propagation_debug
100100
test: undistribute_table
101101
test: run_command_on_all_nodes
@@ -113,3 +113,4 @@ test: ensure_no_intermediate_data_leak
113113
# --------
114114
test: ensure_no_shared_connection_leak
115115
test: check_mx
116+

src/test/regress/sql/issue_5763.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--
2+
-- ISSUE_5763
3+
--
4+
-- Issue: DROP OWNED BY fails to drop the schemas on the workers
5+
-- Link: https://github.com/citusdata/citus/issues/5763
6+
--
7+
CREATE USER issue_5763 WITH SUPERUSER;
8+
\c - issue_5763 - :master_port
9+
CREATE SCHEMA issue_5763_sc;
10+
\c - postgres - :master_port
11+
DROP OWNED BY issue_5763;
12+
\c - issue_5763 - :master_port
13+
CREATE SCHEMA issue_5763_sc;
14+
\c - postgres - :master_port
15+
DROP SCHEMA issue_5763_sc;
16+
DROP USER issue_5763;

src/test/regress/sql/issue_5763.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--
2+
-- ISSUE_5763
3+
--
4+
-- Issue: DROP OWNED BY fails to drop the schemas on the workers
5+
-- Link: https://github.com/citusdata/citus/issues/5763
6+
--
7+
8+
CREATE USER issue_5763 WITH SUPERUSER;
9+
10+
\c - issue_5763 - :master_port
11+
CREATE SCHEMA issue_5763_sc;
12+
13+
\c - postgres - :master_port
14+
DROP OWNED BY issue_5763;
15+
16+
\c - issue_5763 - :master_port
17+
CREATE SCHEMA issue_5763_sc;
18+
19+
\c - postgres - :master_port
20+
DROP SCHEMA issue_5763_sc;
21+
DROP USER issue_5763;

0 commit comments

Comments
 (0)