summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-12-30 17:36:57 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2016-12-30 17:36:57 +0000
commit066d7e30a1cd6e491b9c56568f6e4520fb6a8b9f (patch)
tree6a1500f55c71ac67e9c5a7749f351b6d7f59ed0d
parentSet tables in test database to be unlogged, faster (less disk IO) and we don'... (diff)
downloadlibdbpp-postgresql-066d7e30a1cd6e491b9c56568f6e4520fb6a8b9f.tar.bz2
libdbpp-postgresql-066d7e30a1cd6e491b9c56568f6e4520fb6a8b9f.tar.xz
libdbpp-postgresql-066d7e30a1cd6e491b9c56568f6e4520fb6a8b9f.zip
More complex approach to setting tables to unlogged to handle foreign key dependencies between tables
-rw-r--r--libpqpp/pq-mock.cpp27
1 files changed, 23 insertions, 4 deletions
diff --git a/libpqpp/pq-mock.cpp b/libpqpp/pq-mock.cpp
index 30f14aa..c90f84c 100644
--- a/libpqpp/pq-mock.cpp
+++ b/libpqpp/pq-mock.cpp
@@ -24,12 +24,31 @@ void
Mock::SetTablesToUnlogged() const
{
auto c = DB::ConnectionPtr(openConnection());
- auto s = c->select("SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE schemaname NOT IN (?, ?)");
+ auto s = c->select(R"SQL(
+SELECT n.nspname, c.relname
+FROM pg_class c, pg_namespace n
+WHERE c.relkind = 'r'
+AND n.nspname not in (?, ?)
+AND c.relpersistence = 'p'
+AND NOT EXISTS (
+ SELECT from pg_constraint fk, pg_class ck
+ WHERE fk.contype = 'f'
+ AND fk.confrelid = c.oid
+ AND fk.conrelid = ck.oid
+ AND ck.oid != c.oid
+ AND ck.relpersistence = 'p')
+AND n.oid = c.relnamespace
+ORDER BY 1, 2)SQL");
s->bindParamS(0, "pg_catalog");
s->bindParamS(1, "information_schema");
- for (const auto & t : s->as<std::string, std::string>()) {
- c->execute("ALTER TABLE " + t.value<0>() + "." + t.value<1>() + " SET UNLOGGED");
- }
+ unsigned int n = 0;
+ do {
+ n = 0;
+ for (const auto & t : s->as<std::string, std::string>()) {
+ c->execute("ALTER TABLE " + t.value<0>() + "." + t.value<1>() + " SET UNLOGGED");
+ n += 1;
+ }
+ } while(n);
}
Mock::~Mock()