|
| 1 | +-- set connection strings to nodes |
| 2 | +select conninfo as node1 from mtm.nodes() where id = 1 \gset |
| 3 | +select conninfo as node2 from mtm.nodes() where id = 2 \gset |
| 4 | +select conninfo as node3 from mtm.nodes() where id = 3 \gset |
| 5 | +-- check that implicit empty transactions works fine |
| 6 | +create table t (a int, b text); |
| 7 | +create or replace function f1() returns trigger as $$begin raise notice 'b: %', new.b; return NULL; end$$ language plpgsql; |
| 8 | +create trigger tr1 before insert on t for each row execute procedure f1(); |
| 9 | +insert into t values (1, 'asdf'); |
| 10 | +NOTICE: b: asdf |
| 11 | +copy t from stdout; |
| 12 | +NOTICE: b: baz |
| 13 | +-- test mixed temp table and persistent write |
| 14 | +\c :node1 |
| 15 | +CREATE TEMPORARY TABLE box_temp (f1 box); |
| 16 | +CREATE TABLE box_persistent (f1 box); |
| 17 | +insert into box_temp values('(45,55,45,49)'); |
| 18 | +insert into box_persistent values('(45,55,45,49)'); |
| 19 | +begin; |
| 20 | +insert into box_temp values('(45,55,45,49)'); |
| 21 | +insert into box_persistent values('(45,55,45,49)'); |
| 22 | +commit; |
| 23 | +ERROR: cannot PREPARE a transaction that has operated on temporary tables |
| 24 | +table box_temp; |
| 25 | + f1 |
| 26 | +----------------- |
| 27 | + (45,55),(45,49) |
| 28 | +(1 row) |
| 29 | + |
| 30 | +table box_persistent; |
| 31 | + f1 |
| 32 | +----------------- |
| 33 | + (45,55),(45,49) |
| 34 | +(1 row) |
| 35 | + |
| 36 | +begin; |
| 37 | +create temporary table sp_test_t(i serial primary key); |
| 38 | +create table sp_test(i int primary key); |
| 39 | +commit; |
| 40 | +ERROR: cannot PREPARE a transaction that has operated on temporary tables |
| 41 | +create temporary table sp_test_t1(i serial primary key); |
| 42 | +create table sp_test1(i int primary key); |
| 43 | +\c :node2 |
| 44 | +table box_temp; |
| 45 | +ERROR: relation "box_temp" does not exist |
| 46 | +LINE 1: table box_temp; |
| 47 | + ^ |
| 48 | +table box_persistent; |
| 49 | + f1 |
| 50 | +----------------- |
| 51 | + (45,55),(45,49) |
| 52 | +(1 row) |
| 53 | + |
| 54 | +table sp_test1; |
| 55 | + i |
| 56 | +--- |
| 57 | +(0 rows) |
| 58 | + |
| 59 | +\c :node1 |
| 60 | +create table t_tempddl_mix(id int primary key); |
| 61 | +insert into t_tempddl_mix values(1); |
| 62 | +begin; |
| 63 | +insert into t_tempddl_mix values(42); |
| 64 | +create temp table tempddl(id int); |
| 65 | +commit; |
| 66 | +ERROR: cannot PREPARE a transaction that has operated on temporary tables |
| 67 | +table t_tempddl_mix; |
| 68 | + id |
| 69 | +---- |
| 70 | + 1 |
| 71 | +(1 row) |
| 72 | + |
| 73 | +\c :node2 |
| 74 | +table t_tempddl_mix; |
| 75 | + id |
| 76 | +---- |
| 77 | + 1 |
| 78 | +(1 row) |
| 79 | + |
| 80 | +-- test CTA replication inside explain |
| 81 | +\c :node1 |
| 82 | +DO $$ |
| 83 | +BEGIN |
| 84 | + EXECUTE 'EXPLAIN ANALYZE create table explain_cta as select 42 as col;'; |
| 85 | +END$$; |
| 86 | +table explain_cta; |
| 87 | + col |
| 88 | +----- |
| 89 | + 42 |
| 90 | +(1 row) |
| 91 | + |
| 92 | +\c :node3 |
| 93 | +table explain_cta; |
| 94 | + col |
| 95 | +----- |
| 96 | + 42 |
| 97 | +(1 row) |
| 98 | + |
| 99 | +--- test schemas |
| 100 | +\c :node1 |
| 101 | +create user user1; |
| 102 | +create schema user1; |
| 103 | +alter schema user1 owner to user1; |
| 104 | +create table user1.test(i int primary key); |
| 105 | +table test; |
| 106 | +ERROR: relation "test" does not exist |
| 107 | +LINE 1: table test; |
| 108 | + ^ |
| 109 | +table user1.test; |
| 110 | + i |
| 111 | +--- |
| 112 | +(0 rows) |
| 113 | + |
| 114 | +\c :node2 |
| 115 | +table test; |
| 116 | +ERROR: relation "test" does not exist |
| 117 | +LINE 1: table test; |
| 118 | + ^ |
| 119 | +table user1.test; |
| 120 | + i |
| 121 | +--- |
| 122 | +(0 rows) |
| 123 | + |
| 124 | +--- scheduler example with secdefs and triggers |
| 125 | +\c :node1 |
| 126 | +CREATE TABLE aaa ( |
| 127 | + id int primary key, |
| 128 | + text text |
| 129 | +); |
| 130 | +CREATE TABLE aaa_copy (LIKE aaa); |
| 131 | +ALTER TABLE aaa_copy ADD submit_time timestamp NOT NULL DEFAULT now(); |
| 132 | +ALTER TABLE aaa_copy ADD submitter text NOT NULL DEFAULT session_user; |
| 133 | +ALTER TABLE aaa_copy ADD version_id SERIAL NOT NULL; |
| 134 | +ALTER TABLE aaa_copy ADD PRIMARY KEY (id, version_id); |
| 135 | +CREATE FUNCTION add_aaa( |
| 136 | + aid integer |
| 137 | +) RETURNS integer AS |
| 138 | +$BODY$ |
| 139 | +DECLARE |
| 140 | + nid integer; |
| 141 | +BEGIN |
| 142 | + INSERT INTO aaa (id, text) VALUES (aid, 'zzz') RETURNING id INTO nid; |
| 143 | + RETURN nid; |
| 144 | +END |
| 145 | +$BODY$ |
| 146 | +LANGUAGE plpgsql SECURITY DEFINER; |
| 147 | +CREATE FUNCTION drop_aaa( |
| 148 | + aid integer |
| 149 | +) RETURNS integer AS |
| 150 | +$BODY$ |
| 151 | +BEGIN |
| 152 | + DELETE FROM aaa WHERE id = aid; |
| 153 | + RETURN aid; |
| 154 | +END |
| 155 | +$BODY$ |
| 156 | +LANGUAGE plpgsql SECURITY DEFINER; |
| 157 | +CREATE OR REPLACE FUNCTION on_aaa_update() RETURNS TRIGGER |
| 158 | +AS $BODY$ |
| 159 | +DECLARE |
| 160 | + aaa_id integer; |
| 161 | +BEGIN |
| 162 | + aaa_id := NEW.id; |
| 163 | + INSERT INTO aaa_copy VALUES (NEW.*); |
| 164 | + IF TG_OP = 'UPDATE' THEN |
| 165 | + INSERT INTO aaa_copy VALUES (NEW.*); |
| 166 | + END IF; |
| 167 | + RETURN OLD; |
| 168 | +END |
| 169 | +$BODY$ LANGUAGE plpgsql; |
| 170 | +CREATE OR REPLACE FUNCTION on_aaa_delete() RETURNS TRIGGER |
| 171 | +AS $BODY$ |
| 172 | +DECLARE |
| 173 | + aaa_id INTEGER; |
| 174 | +BEGIN |
| 175 | + aaa_id := OLD.id; |
| 176 | + DELETE FROM aaa_copy WHERE id = aaa_id; |
| 177 | + RETURN OLD; |
| 178 | +END |
| 179 | +$BODY$ LANGUAGE plpgsql; |
| 180 | +CREATE TRIGGER aaa_update_trigger |
| 181 | +AFTER UPDATE OR INSERT ON aaa |
| 182 | + FOR EACH ROW EXECUTE PROCEDURE on_aaa_update(); |
| 183 | +CREATE TRIGGER aaa_delete_trigger |
| 184 | +BEFORE DELETE ON aaa |
| 185 | + FOR EACH ROW EXECUTE PROCEDURE on_aaa_delete(); |
| 186 | +select add_aaa(58); |
| 187 | + add_aaa |
| 188 | +--------- |
| 189 | + 58 |
| 190 | +(1 row) |
| 191 | + |
| 192 | +select add_aaa(5833); |
| 193 | + add_aaa |
| 194 | +--------- |
| 195 | + 5833 |
| 196 | +(1 row) |
| 197 | + |
| 198 | +select add_aaa(582); |
| 199 | + add_aaa |
| 200 | +--------- |
| 201 | + 582 |
| 202 | +(1 row) |
| 203 | + |
| 204 | +delete from aaa; |
| 205 | +table aaa; |
| 206 | + id | text |
| 207 | +----+------ |
| 208 | +(0 rows) |
| 209 | + |
| 210 | +table aaa_copy; |
| 211 | + id | text | submit_time | submitter | version_id |
| 212 | +----+------+-------------+-----------+------------ |
| 213 | +(0 rows) |
| 214 | + |
| 215 | +\c :node3 |
| 216 | +table aaa; |
| 217 | + id | text |
| 218 | +----+------ |
| 219 | +(0 rows) |
| 220 | + |
| 221 | +table aaa_copy; |
| 222 | + id | text | submit_time | submitter | version_id |
| 223 | +----+------+-------------+-----------+------------ |
| 224 | +(0 rows) |
| 225 | + |
| 226 | +-- check our custom publications don't interfere with updates/deletes |
| 227 | +\c :node1 |
| 228 | +create table tbl(id int); |
| 229 | +insert into tbl values (42); |
| 230 | +update tbl set id = id * 2; |
| 231 | +table tbl; |
| 232 | + id |
| 233 | +---- |
| 234 | + 84 |
| 235 | +(1 row) |
| 236 | + |
| 237 | +\c :node2 |
| 238 | +table tbl; |
| 239 | + id |
| 240 | +---- |
| 241 | + 84 |
| 242 | +(1 row) |
| 243 | + |
| 244 | +drop table tbl; |
| 245 | +-- search path checks |
| 246 | +\c :node1 |
| 247 | +set search_path to ''; |
| 248 | +create table sp_test(i int primary key); |
| 249 | +ERROR: no schema has been selected to create in |
| 250 | +LINE 1: create table sp_test(i int primary key); |
| 251 | + ^ |
| 252 | +create table public.sp_test(i int primary key); |
| 253 | +reset search_path; |
| 254 | +drop table sp_test; |
| 255 | +create table sp_test(i int primary key); |
| 256 | +-- portals |
| 257 | +BEGIN; |
| 258 | +DECLARE foo1 CURSOR WITH HOLD FOR SELECT 1; |
| 259 | +DECLARE foo2 CURSOR WITHOUT HOLD FOR SELECT 1; |
| 260 | +SELECT name FROM pg_cursors ORDER BY 1; |
| 261 | + name |
| 262 | +------ |
| 263 | + foo1 |
| 264 | + foo2 |
| 265 | +(2 rows) |
| 266 | + |
| 267 | +CLOSE ALL; |
| 268 | +SELECT name FROM pg_cursors ORDER BY 1; |
| 269 | + name |
| 270 | +------ |
| 271 | +(0 rows) |
| 272 | + |
| 273 | +COMMIT; |
0 commit comments