func (db DB) SetRetention(p *Partition, opts ...map[string]interface{}) {
	if p.Retention == "" {
		l.Info("No retention period configured.")
		return
	}
	var count int
	err := db.Get(&count, "SELECT COUNT(*) FROM partman.part_config WHERE parent_table = $1", p.Table)
	if err != nil {
		l.Error(err)
	}
	if count > 0 {
		m := map[string]interface{}{"table": p.Table, "retention": p.Retention, "retentionSchema": p.Options.RetentionSchema, "retentionKeepTable": p.Options.RetentionKeepTable}
		if len(opts) > 0 {
			if err := mergo.Merge(&m, opts[0]); err != nil {
				l.Error(err)
			}
		}
		if err := mergo.Merge(&m, p.Options.Functions.SetRetention); err != nil {
			l.Error(err)
		}
		pc := PartConfig{}
		err := db.Select(&pc, "SELECT * FROM partman.part_config WHERE parent_table = $1", p.Table)
		if err != nil {
			l.Error(err)
		}
		if err := mergo.Merge(&m, map[string]interface{}{"retention_schema": pc.RetentionSchema, "retention_keep_table": pc.RetentionKeepTable}); err != nil {
			l.Error(err)
		}
		_, err = db.NamedExec(`UPDATE partman.part_config SET retention = :retention, retention_schema = :retentionSchema, retention_keep_table = :retentionKeepTable WHERE parent_table = :table;`, m)
		if err != nil {
			l.Error(err)
		} else {
			l.Info("A retention period has been set for " + p.Table + ". Maintenance will remove old child partition tables.")
		}
	}
}
Sets a retention period on a partition
package main
import (
	"github.com/imdario/mergo"
	"gopkg.in/guregu/null.v2"
	"regexp"
	"strconv"
)
func (db DB) CreateParent(p *Partition) {
	var count int
	err := db.Get(&count, "SELECT COUNT(*) FROM partman.part_config WHERE parent_table = $1", p.Table)
	if err != nil {
		l.Error(err)
	}
	if count > 0 {
		l.Info("Partition already exists for " + p.Table + " you must first run `undo` on it.")
		return
	}
	_, err = db.NamedExec(`SELECT partman.create_parent(:table, :column, :type, :interval);`, p)
	if err != nil {
		l.Error(err)
	}
	db.SetRetention(p)
}
func (db DB) CreateParents() {
	if len(db.Partitions) == 0 {
		l.Info("There are no configured partitions to be created.")
	} else {
		for _, p := range db.Partitions {
			db.CreateParent(&p)
		}
	}
}
func (db DB) RunMaintenance(p *Partition, opts ...map[string]interface{}) {
	m := map[string]interface{}{"table": p.Table}
	if len(opts) > 0 {
		if err := mergo.Merge(&m, opts[0]); err != nil {
			l.Error(err)
		}
	}
	if err := mergo.Merge(&m, p.Options.Functions.RunMaintenance); err != nil {
		l.Error(err)
	}
	if err := mergo.Merge(&m, map[string]interface{}{"analyze": true, "jobmon": true}); err != nil {
		l.Error(err)
	}
	_, err := db.NamedExec(`SELECT partman.run_maintenance(:table, :analyze, :jobmon);`, m)
	if err != nil {
		l.Error(err)
	}
}
func (db DB) UndoPartition(p *Partition, opts ...map[string]interface{}) {
	m := map[string]interface{}{"table": p.Table}
	if len(opts) > 0 {
		if err := mergo.Merge(&m, opts[0]); err != nil {
			l.Error(err)
		}
	}
	if err := mergo.Merge(&m, p.Options.Functions.UndoPartition); err != nil {
		l.Error(err)
	}
	if err := mergo.Merge(&m, map[string]interface{}{"batchCount": 1, "keepTable": true, "jobmon": true, "lockWait": 0}); err != nil {
		l.Error(err)
	}
	_, err := db.NamedExec(`SELECT partman.undo_partition(:table, :batchCount, :keepTable, :jobmon, :lockWait);`, m)
	if err != nil {
		l.Error(err)
	}
	_, err = db.NamedExec(`DELETE FROM partman.part_config WHERE parent_table = :table;`, m)
	if err != nil {
		l.Error(err)
	}
}
func (db DB) PartitionInfo(p *Partition) PartConfig {
	pc := PartConfig{}
	err := db.Get(&pc, "SELECT parent_table,control,type,part_interval,premake FROM partman.part_config WHERE parent_table = $1 LIMIT 1", p.Table)
	if err != nil {
		l.Error(err)
	}
	return pc
}
func (db DB) GetChildPartitions(p *Partition) []ChildInfo {
	c := []ChildInfo{}
	err := db.Select(&c, "SELECT partman.show_partitions($1) AS table", p.Table)
	if err != nil {
		l.Error(err)
	} else {
		for i, child := range c {
			err := db.Get(&c[i].Records, "SELECT COUNT(*) FROM "+child.Table)
			if err != nil {
				l.Error(err)
			}
			err = db.Get(&c[i].BytesOnDisk, "SELECT pg_total_relation_size('"+child.Table+"');")
			if err != nil {
				l.Error(err)
			}
		}
	}
	return c
}
func (db DB) CheckParent() []ParentInfo {
	ps := []ParentInfo{}
	res := []struct {
		value string `db:"value"`
	}{}
	err := db.Select(&res, "SELECT partman.check_parent() AS value")
	if err != nil {
		l.Error(err)
		return ps
	}
	r, _ := regexp.Compile(`\((.*)\,([0-9]*)\)`)
	for _, record := range res {
		pInfo := r.FindStringSubmatch(record.value)
		if len(pInfo) == 3 {
			recordCount, err := strconv.Atoi(pInfo[2])
			if err == nil {
				ps = append(ps, ParentInfo{Table: pInfo[1], Records: recordCount})
			}
		}
	}
	return ps
}
func (db DB) ReapplyPrivileges(p *Partition) {
	m := map[string]interface{}{"table": p.Table}
	_, err := db.NamedExec(`SELECT partman.reapply_privileges(:table);`, m)
	if err != nil {
		l.Error(err)
	}
}
func (db DB) ApplyForeignKeys(p *Partition, opts ...map[string]interface{}) {
	m := map[string]interface{}{"table": p.Table}
	if len(opts) > 0 {
		if err := mergo.Merge(&m, opts[0]); err != nil {
			l.Error(err)
		}
	}
	if err := mergo.Merge(&m, map[string]interface{}{"childTable": null.String{}, "debug": false}); err != nil {
		l.Error(err)
	}
	_, err := db.NamedExec(`SELECT partman.apply_foreign_keys(:table, :childTable, :debug);`, m)
	if err != nil {
		l.Error(err)
	}
}