CREATE FOREIGN TABLE IF NOT EXISTS oik_rtu (
  name                text      OPTIONS(field_id '1'),
  tma                 int       OPTIONS(field_id '4'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6')

) SERVER oik_server
OPTIONS (table_id '13');


CREATE FOREIGN TABLE IF NOT EXISTS oik_chn (
  name                text      OPTIONS(field_id '1'),
  tma                 int       OPTIONS(field_id '4'),
  ch                  smallint  OPTIONS(field_id '5')

) SERVER oik_server
OPTIONS (table_id '14');

CREATE FOREIGN TABLE IF NOT EXISTS oik_cur_ts (
  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),
  tma_str             varchar(20) OPTIONS(field_id '10001'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),

  flags               int       OPTIONS(field_id '50'),
  update_time         timestamp OPTIONS(field_id '51'),

  change_time         timestamp OPTIONS(field_id '101'),
  tm_time             timestamp OPTIONS(field_id '102'),
  ex_flags            int       OPTIONS(field_id '103'),
  flags2              smallint  OPTIONS(field_id '104'),
  provider            text      OPTIONS(field_id '105'),

  v_code              smallint  OPTIONS(field_id '201'),
  v_s2                smallint  OPTIONS(field_id '202'),
  v_importance        smallint  OPTIONS(field_id '203'),
  v_state_text        text      OPTIONS(field_id '204'),

  cl_name             text      OPTIONS(field_id '250'),
  cl_gen_ack          boolean   OPTIONS(field_id '251'),
  cl_unack0           boolean   OPTIONS(field_id '252'),
  cl_unack1           boolean   OPTIONS(field_id '253'),
  cl_text0            text      OPTIONS(field_id '254'),
  cl_text1            text      OPTIONS(field_id '255'),
  cl_break_text       text      OPTIONS(field_id '256'),
  cl_malfun_text      text      OPTIONS(field_id '257'),
  cl_fla_name         text      OPTIONS(field_id '258'),
  cl_flb_name         text      OPTIONS(field_id '259'),
  cl_flc_name         text      OPTIONS(field_id '260'),
  cl_fld_name         text      OPTIONS(field_id '261'),
  cl_fla_text0        text      OPTIONS(field_id '262'),
  cl_fla_text1        text      OPTIONS(field_id '263'),
  cl_flb_text0        text      OPTIONS(field_id '264'),
  cl_flb_text1        text      OPTIONS(field_id '265'),
  cl_flc_text0        text      OPTIONS(field_id '266'),
  cl_flc_text1        text      OPTIONS(field_id '267'),
  cl_fld_text0        text      OPTIONS(field_id '268'),
  cl_fld_text1        text      OPTIONS(field_id '269')

) SERVER oik_server
OPTIONS (table_id '1');

CREATE FOREIGN TABLE IF NOT EXISTS oik_cur_tt (
  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),
  tma_str             varchar(20) OPTIONS(field_id '10001'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),

  flags               int       OPTIONS(field_id '50'),
  update_time         timestamp OPTIONS(field_id '51'),

  change_time         timestamp OPTIONS(field_id '101'),
  tm_time             timestamp OPTIONS(field_id '102'),
  ex_flags            int       OPTIONS(field_id '103'),
  flags2              smallint  OPTIONS(field_id '104'),
  provider            text      OPTIONS(field_id '105'),
  
  v_val               real      OPTIONS(field_id '301'),
  v_code              smallint  OPTIONS(field_id '302'),
  v_unit              char(10)  OPTIONS(field_id '303'),
  v_format            text      OPTIONS(field_id '304'),
  v_mult              real      OPTIONS(field_id '305'),
  v_shift             real      OPTIONS(field_id '306'),
  
  cl_name             text      OPTIONS(field_id '350'),
  cl_val_min          real      OPTIONS(field_id '351'),
  cl_val_max          real      OPTIONS(field_id '352'),
  cl_aperture         real      OPTIONS(field_id '353'),
  
  flags_b1 boolean OPTIONS(field_id '50', bit_num '1')

) SERVER oik_server
OPTIONS (table_id '2');

CREATE FOREIGN TABLE IF NOT EXISTS oik_retro (
  typ                 smallint  OPTIONS(field_id '801'),
  num                 int       OPTIONS(field_id '807'),
  name                text      OPTIONS(field_id '802'),
  descr               text      OPTIONS(field_id '803'),
  avrg                boolean   OPTIONS(field_id '804'),
  period              int       OPTIONS(field_id '805'),
  maxrec              int       OPTIONS(field_id '806'),
  start_time          timestamp OPTIONS(field_id '808'),
  end_time            timestamp OPTIONS(field_id '809'),
  megabytes           int       OPTIONS(field_id '810')
) SERVER oik_server
OPTIONS (table_id '10');

CREATE FOREIGN TABLE IF NOT EXISTS oik_retro_ts (
  retro_num           smallint  OPTIONS(field_id '501'),

  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),

  tma_str             varchar(20) OPTIONS(field_id '10001'),
  
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),

  flags               int       OPTIONS(field_id '50'),
  update_time         timestamp OPTIONS(field_id '51'),

  change_time         timestamp OPTIONS(field_id '101'),
  tm_time             timestamp OPTIONS(field_id '102'),
  ex_flags            int       OPTIONS(field_id '103'),
  flags2              smallint  OPTIONS(field_id '104'),
  provider            text      OPTIONS(field_id '105'),
  
  v_code              smallint  OPTIONS(field_id '201'),
  v_s2                smallint  OPTIONS(field_id '202'),
  v_importance        smallint  OPTIONS(field_id '203'),
  v_state_text        text      OPTIONS(field_id '204'),

  cl_name             text      OPTIONS(field_id '250'),
  cl_gen_ack          boolean   OPTIONS(field_id '251'),
  cl_unack0           boolean   OPTIONS(field_id '252'),
  cl_unack1           boolean   OPTIONS(field_id '253'),
  cl_text0            text      OPTIONS(field_id '254'),
  cl_text1            text      OPTIONS(field_id '255'),
  cl_break_text       text      OPTIONS(field_id '256'),
  cl_malfun_text      text      OPTIONS(field_id '257'),
  cl_fla_name         text      OPTIONS(field_id '258'),
  cl_flb_name         text      OPTIONS(field_id '259'),
  cl_flc_name         text      OPTIONS(field_id '260'),
  cl_fld_name         text      OPTIONS(field_id '261'),
  cl_fla_text0        text      OPTIONS(field_id '262'),
  cl_fla_text1        text      OPTIONS(field_id '263'),
  cl_flb_text0        text      OPTIONS(field_id '264'),
  cl_flb_text1        text      OPTIONS(field_id '265'),
  cl_flc_text0        text      OPTIONS(field_id '266'),
  cl_flc_text1        text      OPTIONS(field_id '267'),
  cl_fld_text0        text      OPTIONS(field_id '268'),
  cl_fld_text1        text      OPTIONS(field_id '269')

) SERVER oik_server
OPTIONS (table_id '4');

CREATE FOREIGN TABLE IF NOT EXISTS oik_retro_tt (
  retro_num           smallint  OPTIONS(field_id '501'),

  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),

  tma_str             varchar(20) OPTIONS(field_id '10001'),
  
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),

  flags               int       OPTIONS(field_id '50'),
  update_time         timestamp OPTIONS(field_id '51'),

  change_time         timestamp OPTIONS(field_id '101'),
  tm_time             timestamp OPTIONS(field_id '102'),
  ex_flags            int       OPTIONS(field_id '103'),
  flags2              smallint  OPTIONS(field_id '104'),
  provider            text      OPTIONS(field_id '105'),
  
  v_val               real      OPTIONS(field_id '301'),
  v_code              smallint  OPTIONS(field_id '302'),
  v_unit              char(10)  OPTIONS(field_id '303'),
  v_format            text      OPTIONS(field_id '304'),
  v_mult              real      OPTIONS(field_id '305'),
  v_shift             real      OPTIONS(field_id '306'),
  
  cl_name             text      OPTIONS(field_id '350'),
  cl_val_min          real      OPTIONS(field_id '351'),
  cl_val_max          real      OPTIONS(field_id '352'),
  cl_aperture         real      OPTIONS(field_id '353')

) SERVER oik_server
OPTIONS (table_id '5');

CREATE FOREIGN TABLE IF NOT EXISTS oik_aan (
  el_type             smallint  OPTIONS(field_id '601'),
  aa_flags            bit(8)    OPTIONS(field_id '602'),
  step                int       OPTIONS(field_id '603'),

  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),

  tma_str             varchar(20) OPTIONS(field_id '10001'),
  
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),

  flags               int       OPTIONS(field_id '50'),
  update_time         timestamp OPTIONS(field_id '51'),
  
  v_val               real      OPTIONS(field_id '301'),
  v_unit              char(10)  OPTIONS(field_id '303'),
  v_format            text      OPTIONS(field_id '304'),
  v_mult              real      OPTIONS(field_id '305'),
  v_shift             real      OPTIONS(field_id '306')

) SERVER oik_server
OPTIONS (table_id '7');

CREATE FOREIGN TABLE IF NOT EXISTS oik_event_log (
  update_time         timestamp OPTIONS(field_id '51'),
  rec_type            smallint  OPTIONS(field_id '701'),
  rec_type_name       text      OPTIONS(field_id '702'),
  rec_state           smallint  OPTIONS(field_id '703'),
  cmd_result          smallint  OPTIONS(field_id '704'),
  rec_state_text      text      OPTIONS(field_id '705'),
  fix_time            timestamp OPTIONS(field_id '707'),
  rec_text            text      OPTIONS(field_id '708'),
  user_name           text      OPTIONS(field_id '709'),
  ext_kind            smallint  OPTIONS(field_id '710'),
  ext_source          smallint  OPTIONS(field_id '711'),
  ext_binary          bytea     OPTIONS(field_id '712'),
  ts_add_flags        bit(8)    OPTIONS(field_id '713'),
  ack_time            timestamp OPTIONS(field_id '714'),
  ack_user            text      OPTIONS(field_id '715'),
  flags               int       OPTIONS(field_id '50'),
  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),
  tma_str             varchar(20) OPTIONS(field_id '10001'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),
  v_code              smallint  OPTIONS(field_id '201'),
  v_s2                smallint  OPTIONS(field_id '202'),
  v_val               real      OPTIONS(field_id '301'),
  alarm_id            smallint  OPTIONS(field_id '902'),
  alarm_active        boolean   OPTIONS(field_id '909')
  
) SERVER oik_server
OPTIONS (table_id '8');

CREATE FOREIGN TABLE IF NOT EXISTS oik_event_log_elix (
  elix                bytea     OPTIONS(field_id '706'),
  
  update_time         timestamp OPTIONS(field_id '51'),
  rec_type            smallint  OPTIONS(field_id '701'),
  rec_type_name       text      OPTIONS(field_id '702'),
  rec_state           smallint  OPTIONS(field_id '703'),
  cmd_result          smallint  OPTIONS(field_id '704'),
  rec_state_text      text      OPTIONS(field_id '705'),
  fix_time            timestamp OPTIONS(field_id '707'),
  rec_text            text      OPTIONS(field_id '708'),
  user_name           text      OPTIONS(field_id '709'),
  ext_kind            smallint  OPTIONS(field_id '710'),
  ext_source          smallint  OPTIONS(field_id '711'),
  ext_binary          bytea     OPTIONS(field_id '712'),
  ts_add_flags        bit(8)    OPTIONS(field_id '713'),
  ack_time            timestamp OPTIONS(field_id '714'),
  ack_user            text      OPTIONS(field_id '715'),
  flags               int       OPTIONS(field_id '50'),
  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),
  tma_str             varchar(20) OPTIONS(field_id '10001'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),
  v_code              smallint  OPTIONS(field_id '201'),
  v_s2                smallint  OPTIONS(field_id '202'),
  v_val               real      OPTIONS(field_id '301'),
  alarm_id            smallint  OPTIONS(field_id '902'),
  alarm_active        boolean   OPTIONS(field_id '909')
  
) SERVER oik_server
OPTIONS (table_id '9');

CREATE FOREIGN TABLE IF NOT EXISTS oik_event_log_rb (
  update_time         timestamp OPTIONS(field_id '51'),
  rec_type            smallint  OPTIONS(field_id '701'),
  rec_type_name       text      OPTIONS(field_id '702'),
  rec_state           smallint  OPTIONS(field_id '703'),
  cmd_result          smallint  OPTIONS(field_id '704'),
  rec_state_text      text      OPTIONS(field_id '705'),
  fix_time            timestamp OPTIONS(field_id '707'),
  rec_text            text      OPTIONS(field_id '708'),
  user_name           text      OPTIONS(field_id '709'),
  ext_kind            smallint  OPTIONS(field_id '710'),
  ext_source          smallint  OPTIONS(field_id '711'),
  ext_binary          bytea     OPTIONS(field_id '712'),
  ts_add_flags        bit(8)    OPTIONS(field_id '713'),
  ack_time            timestamp OPTIONS(field_id '714'),
  ack_user            text      OPTIONS(field_id '715'),
  flags               int       OPTIONS(field_id '50'),
  name                text      OPTIONS(field_id '1'),
  tm_type             smallint  OPTIONS(field_id '2'),
  tm_type_name        text      OPTIONS(field_id '3'),
  tma                 int       OPTIONS(field_id '4'),
  tma_str             varchar(20) OPTIONS(field_id '10001'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  class_id            smallint  OPTIONS(field_id '8'),
  group_id            smallint  OPTIONS(field_id '9'),
  v_code              smallint  OPTIONS(field_id '201'),
  v_s2                smallint  OPTIONS(field_id '202'),
  v_val               real      OPTIONS(field_id '301'),
  alarm_id            smallint  OPTIONS(field_id '902'),
  alarm_active        boolean   OPTIONS(field_id '909')
  
) SERVER oik_server
OPTIONS (table_id '12');

CREATE FOREIGN TABLE IF NOT EXISTS oik_alarms (
  tma                 int       OPTIONS(field_id '4'),
  tma_str             varchar(20) OPTIONS(field_id '10001'),
  ch                  smallint  OPTIONS(field_id '5'),
  rtu                 smallint  OPTIONS(field_id '6'),
  point               smallint  OPTIONS(field_id '7'),
  
  alarm_name          text      OPTIONS(field_id '901'),
  alarm_id            smallint  OPTIONS(field_id '902'),
  group_id            smallint  OPTIONS(field_id '903'),
  val                 real      OPTIONS(field_id '904'),
  expr                text      OPTIONS(field_id '905'),
  cmp_sign            smallint  OPTIONS(field_id '906'),
  numseconds          smallint  OPTIONS(field_id '907'),
  fill_type           smallint  OPTIONS(field_id '908'),
  active              boolean   OPTIONS(field_id '909'),
  in_use              smallint  OPTIONS(field_id '910'),
  importance          smallint  OPTIONS(field_id '911'),
  period              int       OPTIONS(field_id '912'),
  day_map             bytea     OPTIONS(field_id '913'),
  week_map            bit(8)    OPTIONS(field_id '914'),
  year_map            bytea     OPTIONS(field_id '915'),
  indirect            smallint  OPTIONS(field_id '916'),
  countdown           int       OPTIONS(field_id '917'),
  cur_val             real      OPTIONS(field_id '918'),
  cmp_val             real      OPTIONS(field_id '919'),
  param1              real      OPTIONS(field_id '920'),
  param2              real      OPTIONS(field_id '921'),
  param1_name         text      OPTIONS(field_id '922'),
  param2_name         text      OPTIONS(field_id '923')
  
) SERVER oik_server
OPTIONS (table_id '11');
