1919#include "catalog/namespace.h"
2020#include "catalog/pg_type.h"
2121#include "catalog/heap.h"
22+ #include "commands/tablecmds.h"
2223#include "executor/spi.h"
2324#include "parser/parse_relation.h"
2425#include "parser/parse_expr.h"
@@ -44,6 +45,7 @@ static void recreate_range_constraint(Oid partition,
4445const Bound * lower ,
4546const Bound * upper );
4647static char * get_qualified_rel_name (Oid relid );
48+ static void drop_table (Oid relid );
4749
4850/* Function declarations */
4951
@@ -565,7 +567,9 @@ merge_range_partitions_internal(Oid parent, Oid *partitions, uint32 npart)
565567if (SPI_connect ()!= SPI_OK_CONNECT )
566568elog (ERROR ,"could not connect using SPI" );
567569
568- /* Migrate the data from all partition to the first one */
570+ /*
571+ * Migrate the data from all partition to the first one
572+ */
569573for (i = 1 ;i < npart ;i ++ )
570574{
571575char * query = psprintf ("WITH part_data AS (DELETE FROM %s RETURNING *) "
@@ -576,20 +580,14 @@ merge_range_partitions_internal(Oid parent, Oid *partitions, uint32 npart)
576580SPI_exec (query ,0 );
577581}
578582
583+ SPI_finish ();
584+
579585/*
580586 * Drop old partitions
581- *
582- * XXX Rewrite this in C
583587 */
584588for (i = 1 ;i < npart ;i ++ )
585- {
586- char * query = psprintf ("DROP TABLE %s" ,
587- get_qualified_rel_name (partitions [i ]));
588-
589- SPI_exec (query ,0 );
590- }
589+ drop_table (partitions [i ]);
591590
592- SPI_finish ();
593591}
594592
595593/*
@@ -676,3 +674,19 @@ get_qualified_rel_name(Oid relid)
676674quote_identifier (get_namespace_name (namespace )),
677675quote_identifier (get_rel_name (relid )));
678676}
677+
678+ static void
679+ drop_table (Oid relid )
680+ {
681+ DropStmt * n = makeNode (DropStmt );
682+ const char * relname = get_qualified_rel_name (relid );
683+
684+ n -> removeType = OBJECT_TABLE ;
685+ n -> missing_ok = false;
686+ n -> objects = list_make1 (stringToQualifiedNameList (relname ));
687+ n -> arguments = NIL ;
688+ n -> behavior = DROP_RESTRICT ;// default behaviour
689+ n -> concurrent = false;
690+
691+ RemoveRelations (n );
692+ }