Guest User

Untitled

a guest
Jul 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. Pager usage is off.
  2. Timing is on.
  3. SET
  4. Time: 78.776 ms
  5. SET
  6. Time: 81.274 ms
  7. DO $$
  8. DECLARE
  9. tbl text := quote_ident('entity_associations');
  10. target_col text := quote_ident('target_entity_id');
  11. new_col text := quote_ident('tmp_promote_target_entity_id');
  12.  
  13. BEGIN
  14. -- If the target column does not exist, fail.
  15.  
  16. IF NOT EXISTS (
  17. SELECT 1 FROM information_schema.columns
  18. WHERE table_name = tbl
  19. AND column_name = target_col
  20. ) THEN
  21. RAISE EXCEPTION 'target column % does not exist', target_col;
  22. END IF;
  23.  
  24. -- If the target column exists, but is not of type integer, fail.
  25.  
  26. IF NOT EXISTS (
  27. SELECT 1 FROM information_schema.columns
  28. WHERE table_name = tbl
  29. AND column_name = target_col
  30. AND data_type = 'integer'
  31. ) THEN
  32. RAISE EXCEPTION 'target column % must have type integer', target_col;
  33. END IF;
  34.  
  35. -- If the new column already exists, fail.
  36.  
  37. -- NB: It is intentional that a user must deliberately delete a leftover new
  38. -- column from a previous, failed execution of this script. Automatic cleanup
  39. -- could result in catastrophe if the arguments to target_col and new_col were
  40. -- accidentally swapped!
  41.  
  42. IF EXISTS (
  43. SELECT 1 FROM information_schema.columns
  44. WHERE table_name = tbl
  45. AND column_name = new_col
  46. ) THEN
  47. RAISE EXCEPTION
  48. 'Temp column % already exists! To continue, confirm that this is in fact a temp column, and remove it.',
  49. new_col;
  50. END IF;
  51. END
  52. $$;
  53. DO
  54. Time: 94.230 ms
  55. SELECT run_command_on_workers(
  56. $cmd$
  57.  
  58. CREATE OR REPLACE FUNCTION copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations()
  59. RETURNS TRIGGER AS
  60. $copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations$
  61. BEGIN
  62. NEW.tmp_promote_target_entity_id := NEW.target_entity_id;
  63. RETURN NEW;
  64. END;
  65. $copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations$ LANGUAGE plpgsql;
  66. $cmd$
  67. );
  68. run_command_on_workers
  69. -----------------------------------------------------------------------
  70. (ec2-18-210-81-75.compute-1.amazonaws.com,5432,t,"CREATE FUNCTION")
  71. (ec2-18-232-211-178.compute-1.amazonaws.com,5432,t,"CREATE FUNCTION")
  72. (2 rows)
  73.  
  74. Time: 84.638 ms
  75. ALTER TABLE entity_associations ADD COLUMN tmp_promote_target_entity_id bigint;
  76. ALTER TABLE
  77. Time: 90.755 ms
  78. SELECT run_command_on_shards(
  79. 'entity_associations',
  80. $cmd$
  81. DROP TRIGGER IF EXISTS backfill_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations ON %s
  82. $cmd$
  83. );
  84. run_command_on_shards
  85. ---------------------------
  86. (102224,t,"DROP TRIGGER")
  87. (102225,t,"DROP TRIGGER")
  88. (102226,t,"DROP TRIGGER")
  89. (102227,t,"DROP TRIGGER")
  90. (102228,t,"DROP TRIGGER")
  91. (102229,t,"DROP TRIGGER")
  92. (102230,t,"DROP TRIGGER")
  93. (102231,t,"DROP TRIGGER")
  94. (8 rows)
  95.  
  96. Time: 91.372 ms
  97. SELECT run_command_on_shards('entity_associations', $cmd$ CREATE TRIGGER backfill_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations
  98. BEFORE INSERT OR UPDATE ON %s
  99. FOR EACH ROW
  100. EXECUTE PROCEDURE copy_target_entity_id_to_tmp_promote_target_entity_id_on_entity_associations (target_entity_id, tmp_promote_target_entity_id); $cmd$
  101. );
  102. run_command_on_shards
  103. -----------------------------
  104. (102224,t,"CREATE TRIGGER")
  105. (102225,t,"CREATE TRIGGER")
  106. (102226,t,"CREATE TRIGGER")
  107. (102227,t,"CREATE TRIGGER")
  108. (102228,t,"CREATE TRIGGER")
  109. (102229,t,"CREATE TRIGGER")
  110. (102230,t,"CREATE TRIGGER")
  111. (102231,t,"CREATE TRIGGER")
  112. (8 rows)
  113.  
  114. Time: 86.881 ms
  115. Done!
Add Comment
Please, Sign In to add comment