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

Commit eba355c

Browse files
committed
More optimal way of deleting partitions
1 parent 740a194 commit eba355c

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

src/pg_shardman.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -761,16 +761,6 @@ node_in_cluster(int id)
761761
return res;
762762
}
763763

764-
static void
765-
rm_partition(int node_id, char const* part_name)
766-
{
767-
char* sql;
768-
sql = psprintf("delete from shardman.partitions where owner=%d and part_name='%s'",
769-
node_id, part_name);
770-
void_spi(sql);
771-
pfree(sql);
772-
}
773-
774764
/*
775765
* Remove node, losing all data on it. We
776766
* - ensure that there is active node with given id in the cluster
@@ -787,7 +777,7 @@ rm_node(Cmd *cmd)
787777
char *sql;
788778
char **opts;
789779
bool force = false;
790-
int i, e;
780+
int e;
791781

792782
for (opts = cmd->opts; *opts; opts++)
793783
{
@@ -799,28 +789,26 @@ rm_node(Cmd *cmd)
799789
}
800790

801791
SPI_PROLOG;
802-
sql = psprintf("select part_name from shardman.partitions where owner=%d", node_id);
803-
e = SPI_execute(sql, true, 0);
804-
if (e < 0)
805-
shmn_elog(FATAL, "Stmt failed: %s", sql);
806-
pfree(sql);
807-
if (SPI_processed > 0)
792+
if (force)
808793
{
809-
TupleDesc rowdesc = SPI_tuptable->tupdesc;
810-
if (!force)
794+
sql = psprintf("delete from shardman.partitions where owner=%d", node_id);
795+
void_spi(sql);
796+
}
797+
else
798+
{
799+
bool isnull;
800+
sql = psprintf("select count(*) from shardman.partitions where owner=%d", node_id);
801+
e = SPI_execute(sql, true, 0);
802+
if (e < 0)
803+
shmn_elog(FATAL, "Stmt failed: %s", sql);
804+
Assert(SPI_processed == 1);
805+
if (DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)) != 0)
811806
{
812-
813807
ereport(ERROR, (errmsg("Can not remove node with existed partitions"),
814808
errhint("Add \"force\" option to remove node with existed partitions.")));
815809
}
816-
/* Remove partitions belonging to this node */
817-
for (i = 0; i < SPI_processed; i++)
818-
{
819-
HeapTuple tuple = SPI_tuptable->vals[i];
820-
char const* partition = SPI_getvalue(tuple, rowdesc, 1);
821-
rm_partition(node_id, partition);
822-
}
823810
}
811+
pfree(sql);
824812
SPI_EPILOG;
825813

826814
elog(INFO, "Removing node %d ", node_id);

0 commit comments

Comments
 (0)