Class: Rake::DataTask::Greenplum

Inherits:
Postgres show all
Defined in:
lib/data_task/adapters/greenplum.rb

Constant Summary collapse

TABLE_TRACKER_HELPER_NAME =
"operations"

Constants inherited from Db

Db::LOG, Db::TABLE_TRACKER_COLUMNS, Db::TABLE_TRACKER_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Postgres

#[], #create_table, #create_view, #drop_view, #execute, #initialize, #operations_supported, #reset_tracking, #set_up_tracking, #table_exists?, #table_mtime, #table_tracker_columns, #tear_down_tracking, #truncate_table, #view_exists?, #with_role, #with_search_path

Methods included from ConnectionPersistence

#persist_connection, #persisted_connection

Methods inherited from Db

#operation_values, #relation_type_values, #table_tracker_columns

Constructor Details

This class inherits a constructor from Rake::DataTask::Postgres

Class Method Details

.set_up_tracking(options) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/data_task/adapters/greenplum.rb', line 12

def self.set_up_tracking options
  tear_down_tracking options
  super

  execute "alter table #{TABLE_TRACKER_NAME} rename to #{TABLE_TRACKER_HELPER_NAME}"

  # Greenplum tracks CREATE and TRUNCATE operations in its pg_stat_operations system view.
  # Join this view with the tracking table so that we can track CREATE and TRUNCATE from within
  # the database instead of from application code.

  execute "    create view fixed_pg_stat_operations as\n    -- GP's pg_stat_operations enum values like 'TABLE' are inconsistent so fix them here\n    select\n      pso.classname, \n      pso.objname,\n      pso.objid,\n      pso.schemaname,\n      pso.usestatus,\n      pso.usename,\n      pso.actionname,\n      case \n        when pso.actionname = 'TRUNCATE' then '\#{relation_type_values[:table]}'\n        when pso.subtype = 'TABLE' then '\#{relation_type_values[:table]}'\n        else pso.subtype\n      end as subtype,\n      pso.statime\n    from pg_stat_operations pso\n  EOSQL\n\n  execute <<-EOSQL\n    create view \#{TABLE_TRACKER_NAME} as \n    select\n      relation_name,\n      relation_type,\n      operation,\n      time\n    from (\n\n      select\n        a.*,\n        rank() over (partition by relation_name, relation_type order by time desc)\n      from (\n\n        -- select all CREATE and TRUNCATE operations tracked by Greenplum\n        select\n          pso.objname as relation_name,\n          pso.subtype as relation_type,\n          pso.actionname as operation,\n          pso.statime as time\n        from fixed_pg_stat_operations pso\n        where pso.actionname not in ('ANALYZE', 'VACUUM')\n\n        union all\n\n        -- select all operations tracked by Greenplum (PostgreSQL) table rules \n        select\n          ttb.relation_name,\n          ttb.relation_type,\n          ttb.operation,\n          ttb.time\n        from \n          \#{TABLE_TRACKER_HELPER_NAME} ttb\n          -- return only operations for tables that exist in system tables\n          join fixed_pg_stat_operations pso on (\n            ttb.relation_name = pso.objname and\n            ttb.relation_type = pso.subtype and\n            pso.actionname = 'CREATE'\n          )\n\n        ) a\n      ) b \n    -- take only the latest operation per table\n    where rank = 1\n  EOSQL\n\n  # make sure we do deletes and inserts on the helper table, not the view\n  execute <<-EOSQL\n    create rule delete_operation_record as on delete to \#{TABLE_TRACKER_NAME} \n      do instead\n      delete from \#{TABLE_TRACKER_HELPER_NAME} \n      where\n        relation_name = OLD.relation_name and\n        relation_type = OLD.relation_type and\n        operation = OLD.operation\n    ;\n\n    create rule insert_operation_record as on insert to \#{TABLE_TRACKER_NAME} \n      do instead\n      insert into \#{TABLE_TRACKER_HELPER_NAME} values (\n        NEW.relation_name,\n        NEW.relation_type,\n        NEW.operation,\n        NEW.time\n      )\n    ;\n  EOSQL\nend\n"

.tear_down_tracking(options) ⇒ Object



111
112
113
114
115
# File 'lib/data_task/adapters/greenplum.rb', line 111

def self.tear_down_tracking options
  drop_view "fixed_pg_stat_operations"
  drop_view TABLE_TRACKER_NAME
  drop_table TABLE_TRACKER_HELPER_NAME
end

Instance Method Details

#drop_table(table_name) ⇒ Object



121
122
123
124
125
126
# File 'lib/data_task/adapters/greenplum.rb', line 121

def drop_table table_name
  execute "drop table if exists #{table_name} cascade"
  return if table_name.casecmp(TABLE_TRACKER_HELPER_NAME) == 0 || 
    table_name.casecmp(TABLE_TRACKER_NAME) == 0
  track_drop table_name
end

#track_drop(table_name) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/data_task/adapters/greenplum.rb', line 128

def track_drop table_name
  execute "    delete from \#{TABLE_TRACKER_HELPER_NAME} \n    where \n      relation_name = '\#{table_name}' and \n      relation_type = '\#{relation_type_values[:table]}'\n  EOSQL\nend\n"

#tracking_tables?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/data_task/adapters/greenplum.rb', line 117

def tracking_tables?
  view_exists?(TABLE_TRACKER_NAME)
end