forked from denishpatel/pg-clone-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampledb.sql
1342 lines (1013 loc) · 38.9 KB
/
sampledb.sql
1
2
3
4
5
6
7
8
9
10
11
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Sample database used for clone_schema() testing
-- psql -b postgres < ./sampledb.sql
-- psql -v ON_ERROR_STOP=1 -e -b postgres < ~/clone_schema/sampledb.sql
-- psql clone_testing < /var/lib/pgsql/clone_schema/clone_schema.sql
-- psql clone_testing; select clone_schema('sample', 'sample_clone1', false, false);
-- add access control after creating the clone_testing database:
-- psql -v ON_ERROR_STOP=1 -e -b postgres < ./sampledb_access_control.sql
-- drop/create clone schema database
drop database if exists clone_testing;
create database clone_testing;
\connect clone_testing;
COMMENT ON DATABASE clone_testing IS 'just a comment on my sample database';
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
-- drop roles related to this database
-- DROP ROLE IF EXISTS sysdba;
-- set global stuff:
-- don't worry if roles already exist, since they are cluster-wide, not just database-wide
-- CREATE ROLE sysdba;
-- ALTER ROLE sysdba WITH SUPERUSER INHERIT CREATEROLE CREATEDB LOGIN REPLICATION NOBYPASSRLS PASSWORD 'md54f2192d28c96b5e38d4553264cb2f448';
-- Role memberships
-- end of global stuff
CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA public;
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
CREATE EXTENSION IF NOT EXISTS postgres_fdw WITH SCHEMA public;
COMMENT ON EXTENSION postgres_fdw IS 'foreign-data wrapper for remote PostgreSQL servers';
CREATE SERVER my_foreign_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (
dbname 'testing2',
host 'localhost',
port '5443'
);
ALTER SERVER my_foreign_server OWNER TO postgres;
CREATE USER MAPPING IF NOT EXISTS FOR postgres SERVER my_foreign_server OPTIONS (
password 'pgpass',
"user" 'postgres'
);
--
-- Name: sample; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA sample;
SET search_path = sample, public;
ALTER SCHEMA sample OWNER TO postgres;
--
-- Name: SCHEMA sample; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA sample IS 'standard sample schema';
--
-- Name: de-u-co-phonebk-x-icu; Type: COLLATION; Schema: sample; Owner: postgres
--
CREATE COLLATION "de-u-co-phonebk-x-icu" (provider = icu, locale = 'de-u-co-phonebk');
ALTER COLLATION "de-u-co-phonebk-x-icu" OWNER TO postgres;
--
-- Name: french; Type: COLLATION; Schema: sample; Owner: postgres
--
CREATE COLLATION french (provider = icu, locale = 'fr');
COMMENT ON COLLATION french IS 'my comments on french collation';
ALTER COLLATION french OWNER TO postgres;
--
-- Name: german_phonebook; Type: COLLATION; Schema: sample; Owner: postgres
--
CREATE COLLATION german_phonebook (provider = icu, locale = 'de-u-co-phonebk');
ALTER COLLATION german_phonebook OWNER TO postgres;
--
-- Name: und-u-co-emoji-x-icu; Type: COLLATION; Schema: sample; Owner: postgres
--
CREATE COLLATION "und-u-co-emoji-x-icu" (provider = icu, locale = 'und-u-co-emoji');
ALTER COLLATION "und-u-co-emoji-x-icu" OWNER TO postgres;
--
-- Name: addr; Type: DOMAIN; Schema: sample; Owner: postgres
--
CREATE TYPE udt_myint AS (myint INTEGER);
CREATE DOMAIN addr AS character varying(90) NOT NULL;
COMMENT ON DOMAIN addr IS 'my domain comments on addr';
ALTER DOMAIN addr OWNER TO postgres;
--
-- Name: addr2; Type: DOMAIN; Schema: sample; Owner: postgres
--
CREATE DOMAIN addr2 AS character varying(90) NOT NULL DEFAULT 'N/A'::character varying;
ALTER DOMAIN addr2 OWNER TO postgres;
--
-- Name: addr3; Type: DOMAIN; Schema: sample; Owner: postgres
--
CREATE DOMAIN addr3 AS character varying(90) NOT NULL DEFAULT 'N/A'::character varying
CONSTRAINT addr3_check CHECK (((VALUE)::text > ''::text));
ALTER DOMAIN addr3 OWNER TO postgres;
--
-- Name: compfoo; Type: TYPE; Schema: sample; Owner: postgres
--
CREATE TYPE compfoo AS (
f1 integer,
f2 text
);
COMMENT ON TYPE compfoo IS 'just a comment on compfoo type';
ALTER TYPE compfoo OWNER TO postgres;
--
-- Name: compfoo2; Type: TYPE; Schema: sample; Owner: postgres
--
CREATE TYPE compfoo2 AS (
x1 integer,
x2 text
);
ALTER TYPE compfoo2 OWNER TO postgres;
--
-- Name: cycle_frequency; Type: TYPE; Schema: sample; Owner: postgres
--
CREATE TYPE cycle_frequency AS ENUM (
'WEEKLY',
'MONTHLY',
'QUARTERLY',
'ANNUALLY'
);
ALTER TYPE cycle_frequency OWNER TO postgres;
CREATE TYPE banner_color AS ENUM ('green','blue','lightblue','purple','red','yellow','orange','grey','pink');
ALTER TYPE banner_color OWNER TO postgres;
--
-- Name: idx; Type: DOMAIN; Schema: sample; Owner: postgres
--
CREATE DOMAIN idx AS integer NOT NULL
CONSTRAINT idx_check CHECK (((VALUE > 100) AND (VALUE < 999)));
ALTER DOMAIN idx OWNER TO postgres;
--
-- Name: obj_type; Type: TYPE; Schema: sample; Owner: postgres
--
CREATE TYPE obj_type AS ENUM (
'TABLE',
'VIEW',
'COLUMN',
'SEQUENCE',
'FUNCTION',
'SCHEMA',
'DATABASE'
);
ALTER TYPE obj_type OWNER TO postgres;
--
-- Name: perm_type; Type: TYPE; Schema: sample; Owner: postgres
--
CREATE TYPE perm_type AS ENUM (
'SELECT',
'INSERT',
'UPDATE',
'DELETE',
'TRUNCATE',
'REFERENCES',
'TRIGGER',
'USAGE',
'CREATE',
'EXECUTE',
'CONNECT',
'TEMPORARY'
);
ALTER TYPE perm_type OWNER TO postgres;
--
-- Name: us_postal_code; Type: DOMAIN; Schema: sample; Owner: postgres
--
CREATE DOMAIN us_postal_code AS text NOT NULL
CONSTRAINT us_postal_code_check CHECK (((VALUE ~ '^\d{5}$'::text) OR (VALUE ~ '^\d{5}-\d{4}$'::text)));
ALTER DOMAIN us_postal_code OWNER TO postgres;
CREATE AGGREGATE avg (float8)
(
sfunc = float8_accum,
stype = float8[],
finalfunc = float8_avg,
initcond = '{0,0,0}'
);
-- CREATE AGGREGATE array_accum (anyelement)
-- (
-- sfunc = array_append,
-- stype = anyarray,
-- initcond = '{}'
-- );
create function greaterint (int, int)
returns int language sql
as $$
select case when $1 < $2 then $2 else $1 end
$$;
create function intplus10 (int)
returns int language sql
as $$
select $1+ 10;
$$;
create aggregate incremented_max (int) (
sfunc = greaterint,
finalfunc = intplus10,
stype = integer,
initcond = 0
);
CREATE OR REPLACE FUNCTION database_principal_id()
RETURNS INTEGER
AS
$BODY$
DECLARE
ownerid integer;
myint udt_myint;
BEGIN
SELECT U.oid into ownerid FROM pg_roles AS U JOIN pg_database AS D ON (D.datdba = U.oid) WHERE D.datname = current_database();
RETURN ownerid;
END;
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION aaa() RETURNS void
LANGUAGE plpgsql
AS $_$
DECLARE
stmt text;
BEGIN
stmt =
$xxx$
select n.nspname AS schemaname, t.typname AS typename, t.typcategory AS typcategory, t.typinput AS typinput, t.typstorage AS typstorage, CASE WHEN t.typcategory='C' THEN ''
WHEN t.typcategory='E' THEN 'CREATE TYPE quote_ident(dest_schema).' || t.typname || ' AS ENUM (' || REPLACE(quote_literal(array_to_string(array_agg(e.enumlabel ORDER BY e.enumsortorder),',')), ',', ''',''') || ');'
ELSE 'type category: ' || t.typcategory || ' not implemented yet' END AS enum_ddl FROM pg_type t JOIN pg_namespace n ON (n.oid = t.typnamespace)
LEFT JOIN pg_enum e ON (t.oid = e.enumtypid) where n.nspname = quote_ident(source_schema) group by 1,2,3,4,5
$xxx$;
RAISE NOTICE '%', stmt;
RETURN;
END;
$_$;
ALTER FUNCTION aaa() OWNER TO postgres;
COMMENT ON FUNCTION aaa() IS 'comment on my aaa() function';
--
-- Name: emp_stamp(); Type: FUNCTION; Schema: sample; Owner: postgres
--
CREATE FUNCTION emp_stamp() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;
-- Who works for us when she must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;
-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$$;
ALTER FUNCTION emp_stamp() OWNER TO postgres;
CREATE OR REPLACE FUNCTION fnsplitstring(IN par_string VARCHAR, IN par_delimiter CHAR)
RETURNS TABLE (splitdata VARCHAR)
AS
$BODY$
# variable_conflict use_column
DECLARE
var_start INTEGER;
var_end INTEGER;
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS fnsplitstring$tmptbl (splitdata VARCHAR) ON COMMIT DELETE ROWS;
SELECT 1, STRPOS(par_string, par_delimiter) INTO var_start, var_end;
WHILE var_start < LENGTH(par_string) + 1 LOOP
IF var_end = 0 THEN
var_end := (LENGTH(par_string) + 1)::INT;
END IF;
INSERT INTO fnsplitstring$tmptbl (splitdata)
VALUES (SUBSTR(par_string, var_start, var_end - var_start));
var_start := (var_end + 1)::INT;
var_end := aws_sqlserver_ext.STRPOS3(par_delimiter, par_string, var_start);
END LOOP;
RETURN QUERY (SELECT * FROM fnsplitstring$tmptbl);
END;
$BODY$
LANGUAGE plpgsql;
CREATE PROCEDURE get_userscans(IN aschema text, IN atable text, INOUT scans INTEGER) AS
$BODY$
BEGIN
-- Select seq_scan into scans FROM pg_stat_user_tables where schemaname = aschema and relname = atable;
Select seq_scan FROM pg_stat_user_tables where schemaname = aschema and relname = atable INTO scans;
RETURN;
END;
$BODY$
LANGUAGE plpgsql;
COMMENT ON PROCEDURE get_userscans(text, text, integer) IS 'my comments on get_userscans procedure';
CREATE PROCEDURE get_userscans(IN aschema text, IN atable text, INOUT scans INTEGER, INOUT ok boolean) AS
$BODY$
BEGIN
-- Select seq_scan into scans FROM pg_stat_user_tables where schemaname = aschema and relname = atable;
Select seq_scan FROM pg_stat_user_tables where schemaname = aschema and relname = atable INTO scans;
ok := True;
RETURN;
END;
$BODY$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION aaa(IN akey integer default 0)
RETURNS integer
AS
$BODY$
DECLARE
var_start INTEGER;
var_end INTEGER;
BEGIN
Return 1;
END;
$BODY$
LANGUAGE plpgsql;
COMMENT ON FUNCTION aaa(integer) IS 'comment on my aaa(int) function';
SET default_tablespace = '';
SET default_with_oids = false;
CREATE UNLOGGED TABLE myunloggedtbl (id integer PRIMARY KEY, val text NOT NULL) WITH (autovacuum_enabled = off);
CREATE table timestamptbl (akey int not null, color sample.banner_color, avalue text, tmstmp timestamptz NOT NULL, tmstmp_null timestamptz NULL, tmstmp_null2 timestamp(0) with time zone);
INSERT INTO timestamptbl (akey, color, avalue, tmstmp, tmstmp_null, tmstmp_null2) VALUES (1, 'green', 'aaa', now(), now(), now());
INSERT INTO timestamptbl (akey, color, avalue, tmstmp ) VALUES (2, 'red', 'bbb', now());
CREATE TABLE numerics (
id integer,
anumeric numeric,
anumeric2 numeric(1,0),
anumeric3 numeric(1,1),
anumeric4 numeric(25,0),
anumeric5 numeric(25,0)
);
ALTER TABLE numerics OWNER TO postgres;
CREATE TABLE arrays (
name text,
aarray1 integer[],
aarray2 text[][],
aarray3 text[3][3],
aarray4 integer ARRAY[4],
aarray5 integer ARRAY,
aarray6 varchar(8) [],
email public.citext,
aarray7 character varying(200)[] NOT NULL
);
ALTER TABLE numerics OWNER TO postgres;
--
-- Name: address; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE address (
id bigint NOT NULL,
id2 udt_myint,
id3 udt_myint,
addr text
);
COMMENT ON TABLE address IS 'This table is where I keep address info.';
INSERT INTO address OVERRIDING SYSTEM VALUE SELECT 1, '(1)', '(1)', 'text1';
INSERT INTO address OVERRIDING SYSTEM VALUE SELECT 2, '(2)', '(2)', 'text2';
ALTER TABLE address OWNER TO postgres;
--
-- Name: address_id_seq; Type: SEQUENCE; Schema: sample; Owner: postgres
--
ALTER TABLE address ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME address_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TYPE status AS ENUM ('Notconfirmed','Coming', 'Notcoming', 'Maycome');
CREATE TABLE statuses (id serial PRIMARY KEY, s status default 'Notconfirmed');
COMMENT ON TABLE statuses IS 'This table is where I keep status info.';
INSERT INTO statuses Select 1, 'Coming';
INSERT INTO statuses Select 2, 'Notcoming';
INSERT INTO statuses Select 3, 'Maycome';
--
-- Name: emp; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE emp (
empname text,
salary integer,
last_date timestamp without time zone,
last_user text
);
COMMENT ON TABLE emp IS 'Employee info';
COMMENT ON COLUMN emp.salary IS 'Employee Salary info';
ALTER TABLE emp OWNER TO postgres;
INSERT INTO emp select 'michael', 100, current_timestamp, 'john';
--
-- Name: foo; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo (
foo_id integer NOT NULL,
foo_name character varying(10)
);
CREATE TABLE foo2 (
foo_id integer NOT NULL,
foo_name character varying(10)
);
ALTER TABLE foo OWNER TO postgres;
ALTER TABLE foo2 OWNER TO postgres;
-- PG 16 throws error for following rule creation
-- CREATE RULE "_RETURN" AS ON SELECT TO foo DO INSTEAD SELECT * FROM foo2;
-- ERROR: relation "foo" cannot have ON SELECT rules
-- DETAIL: This operation is not supported for tables.
CREATE RULE notify_me AS ON UPDATE TO foo DO ALSO NOTIFY foo;
INSERT INTO foo (foo_id, foo_name) VALUES(1,'haha');
INSERT INTO foo2 (foo_id, foo_name) VALUES(1,'hoho');
-- -----------------------------------------------
-- Create partitions the old way using inheritance
-- -----------------------------------------------
CREATE TABLE measurement (
city_id int not null,
logdate date not null,
peaktemp int,
unitsales int
);
CREATE TABLE measurement_y2006m02 (
CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE '2006-03-01' )
) INHERITS (measurement);
CREATE TABLE measurement_y2006m03 (
CHECK ( logdate >= DATE '2006-03-01' AND logdate < DATE '2006-04-01' )
) INHERITS (measurement);
CREATE TABLE measurement_y2006mRest (
CHECK ( logdate >= DATE '2006-04-01' AND logdate < DATE '2022-12-31' )
) INHERITS (measurement);
CREATE TABLE measurement_y2007 (
CHECK ( logdate >= DATE '2007-01-01' AND logdate < DATE '2008-01-01' )
) INHERITS (measurement);
CREATE TABLE measurement_y2008_2022 (
CHECK ( logdate >= DATE '2008-01-01' AND logdate < DATE '2023-01-01' )
) INHERITS (measurement);
CREATE TABLE measurement_y2023 (
CHECK ( logdate >= DATE '2023-01-01' AND logdate < DATE '2024-01-01' )
) INHERITS (measurement);
CREATE TABLE measurement_y2024 (
CHECK ( logdate >= DATE '2024-01-01' AND logdate < DATE '2025-01-01' )
) INHERITS (measurement);
CREATE INDEX measurement_y2006m02_logdate_ix ON measurement_y2006m02 (logdate);
CREATE INDEX measurement_y2006m03_logdate_ix ON measurement_y2006m03 (logdate);
CREATE INDEX measurement_y2026mRest_ix ON measurement_y2006mRest (logdate);
CREATE INDEX measurement_y2007_ix ON measurement_y2007 (logdate);
CREATE INDEX measurement_y2008_2022_ix ON measurement_y2008_2022 (logdate);
CREATE INDEX measurement_y2023_ix ON measurement_y2023 (logdate);
CREATE INDEX measurement_y2024_ix ON measurement_y2024 (logdate);
CREATE OR REPLACE FUNCTION measurement_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.logdate >= DATE '2006-02-01' AND
NEW.logdate < DATE '2006-03-01' ) THEN
INSERT INTO measurement_y2006m02 VALUES (NEW.*);
ELSIF ( NEW.logdate >= DATE '2006-03-01' AND
NEW.logdate < DATE '2006-04-01' ) THEN
INSERT INTO measurement_y2006m03 VALUES (NEW.*);
ELSIF ( NEW.logdate >= DATE '2006-04-01' AND
NEW.logdate < DATE '2007-01-01' ) THEN
INSERT INTO measurement_y2006mRest VALUES (NEW.*);
ELSIF ( NEW.logdate >= DATE '2007-01-01' AND
NEW.logdate < DATE '2008-01-01' ) THEN
INSERT INTO measurement_y2007 VALUES (NEW.*);
ELSIF ( NEW.logdate >= DATE '2008-01-01' AND
NEW.logdate < DATE '2023-01-01' ) THEN
INSERT INTO measurement_y2008_2022 VALUES (NEW.*);
ELSIF ( NEW.logdate >= DATE '2023-01-01' AND
NEW.logdate < DATE '2024-01-01' ) THEN
INSERT INTO measurement_y2023 VALUES (NEW.*);
ELSIF ( NEW.logdate >= DATE '2024-01-01' AND
NEW.logdate < DATE '2025-01-01' ) THEN
INSERT INTO measurement_y2024 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the measurement_insert_trigger() function!';
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER insert_measurement_trigger
BEFORE INSERT ON measurement
FOR EACH ROW EXECUTE PROCEDURE measurement_insert_trigger();
INSERT INTO measurement SELECT 1, now(), 70, 100;
INSERT INTO measurement SELECT 2, now() - interval '1 year', 80, 120;
INSERT INTO measurement SELECT 3, now() - interval '10 year', 90, 125;
--
-- Name: foo_bar_baz; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
)
PARTITION BY RANGE (foo_id);
ALTER TABLE foo_bar_baz OWNER TO postgres;
COMMENT ON TABLE foo_bar_baz IS 'just a comment on a partitioned table';
--
-- Name: foo_bar_baz_0; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz_0 (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
);
ALTER TABLE ONLY foo_bar_baz ATTACH PARTITION foo_bar_baz_0 FOR VALUES FROM (0) TO (1);
ALTER TABLE foo_bar_baz_0 OWNER TO postgres;
--
-- Name: foo_bar_baz_1; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz_1 (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
);
ALTER TABLE ONLY foo_bar_baz ATTACH PARTITION foo_bar_baz_1 FOR VALUES FROM (1) TO (2);
ALTER TABLE foo_bar_baz_1 OWNER TO postgres;
--
-- Name: foo_bar_baz_2; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz_2 (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
);
ALTER TABLE ONLY foo_bar_baz ATTACH PARTITION foo_bar_baz_2 FOR VALUES FROM (2) TO (3);
ALTER TABLE foo_bar_baz_2 OWNER TO postgres;
--
-- Name: foo_bar_baz_3; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz_3 (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
);
ALTER TABLE ONLY foo_bar_baz ATTACH PARTITION foo_bar_baz_3 FOR VALUES FROM (3) TO (4);
ALTER TABLE foo_bar_baz_3 OWNER TO postgres;
--
-- Name: foo_bar_baz_4; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz_4 (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
);
ALTER TABLE ONLY foo_bar_baz ATTACH PARTITION foo_bar_baz_4 FOR VALUES FROM (4) TO (5);
ALTER TABLE foo_bar_baz_4 OWNER TO postgres;
--
-- Name: foo_bar_baz_5; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE foo_bar_baz_5 (
foo_id integer NOT NULL,
bar_id integer NOT NULL,
baz integer NOT NULL,
akey text not null DEFAULT 'my default value'
);
ALTER TABLE ONLY foo_bar_baz ATTACH PARTITION foo_bar_baz_5 FOR VALUES FROM (5) TO (6);
ALTER TABLE foo_bar_baz_5 OWNER TO postgres;
INSERT INTO foo_bar_baz SELECT 1, 1, 1;
INSERT INTO foo_bar_baz SELECT 2, 2, 2;
INSERT INTO foo_bar_baz SELECT 3, 3, 3;
--
-- Name: haha; Type: FOREIGN TABLE; Schema: sample; Owner: postgres
--
CREATE FOREIGN TABLE haha (
id integer NOT NULL,
adate timestamp with time zone NOT NULL
)
SERVER my_foreign_server
OPTIONS (
schema_name 'sample',
table_name 'haha'
);
COMMENT ON FOREIGN TABLE haha IS 'just a comment on a foreign table';
ALTER FOREIGN TABLE haha OWNER TO postgres;
CREATE MATERIALIZED VIEW activity AS SELECT pid, datname, state, query FROM pg_stat_activity where state in ('active','idle in transaction') WITH NO DATA;
ALTER TABLE activity OWNER TO postgres;
COMMENT ON MATERIALIZED VIEW activity IS 'just a comment on the activity materialized view';
CREATE INDEX activity_pid_idx ON activity USING btree (pid);
CREATE MATERIALIZED VIEW mv_foo_bar_baz AS
SELECT count(*) as count
FROM foo_bar_baz;
ALTER TABLE mv_foo_bar_baz OWNER TO postgres;
--
-- Name: hoho2; Type: VIEW; Schema: sample; Owner: postgres
--
CREATE VIEW hoho2 AS
SELECT count(*) AS count
FROM pg_stat_activity;
ALTER TABLE hoho2 OWNER TO postgres;
-- THIS WONT WORK UNTIL WE FIX dependency ORDERINGS
-- CREATE VIEW hoho3 AS
-- SELECT count(*) as count
-- FROM hoho2;
-- ALTER TABLE hoho3 OWNER TO postgres;
--
-- Name: person; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE person (
id bigint NOT NULL,
firstname text NOT NULL,
lastname text NOT NULL
);
ALTER TABLE person OWNER TO postgres;
ALTER TABLE person ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME person_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
INSERT into person OVERRIDING SYSTEM VALUE select 1, 'joe','shmoe';
INSERT into person OVERRIDING SYSTEM VALUE select 2, 'james','bond';
--
-- Name: sampletable; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE sampletable (x numeric);
ALTER TABLE sampletable OWNER TO postgres;
CREATE VIEW v_sampletable AS Select * from sampletable;
COMMENT ON VIEW v_sampletable IS 'just a view on the sample table';
INSERT INTO sampletable SELECT 1.00;
INSERT INTO sampletable SELECT 2.00;
--
-- Name: seq111; Type: SEQUENCE; Schema: sample; Owner: postgres
--
CREATE SEQUENCE seq111
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
COMMENT ON SEQUENCE seq111 IS 'just a comment on seq111 sequence';
ALTER TABLE seq111 OWNER TO postgres;
--
-- Name: test; Type: TABLE; Schema: sample; Owner: postgres
--
CREATE TABLE test (
major integer DEFAULT 2 NOT NULL,
minor integer
);
ALTER TABLE test OWNER TO postgres;
INSERT INTO test SELECT 1,1;
INSERT INTO test SELECT 2,2;
--
-- Name: address address_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY address
ADD CONSTRAINT address_pkey PRIMARY KEY (id);
--
-- Name: foo_bar_baz foo_bar_baz_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz
ADD CONSTRAINT foo_bar_baz_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: foo_bar_baz_0 foo_bar_baz_0_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz_0
ADD CONSTRAINT foo_bar_baz_0_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: foo_bar_baz_1 foo_bar_baz_1_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz_1
ADD CONSTRAINT foo_bar_baz_1_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: foo_bar_baz_2 foo_bar_baz_2_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz_2
ADD CONSTRAINT foo_bar_baz_2_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: foo_bar_baz_3 foo_bar_baz_3_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz_3
ADD CONSTRAINT foo_bar_baz_3_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: foo_bar_baz_4 foo_bar_baz_4_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz_4
ADD CONSTRAINT foo_bar_baz_4_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: foo_bar_baz_5 foo_bar_baz_5_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY foo_bar_baz_5
ADD CONSTRAINT foo_bar_baz_5_pkey PRIMARY KEY (foo_id, bar_id, baz);
--
-- Name: person person_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY person
ADD CONSTRAINT person_pkey PRIMARY KEY (id);
--
-- Name: test test_pkey; Type: CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY test
ADD CONSTRAINT test_pkey PRIMARY KEY (major);
--
-- Name: idx_x; Type: INDEX; Schema: sample; Owner: postgres
--
CREATE INDEX idx_x ON sampletable USING btree (x);
COMMENT ON INDEX idx_x IS 'just another btree index';
CREATE TABLE tablewithindexes(akey int, anum int, avalue text,
CONSTRAINT pk_akey_anum PRIMARY KEY (akey,anum),
CONSTRAINT uix_akey_anum UNIQUE (akey,anum));
CREATE TABLE t_site (
site_key integer NOT NULL,
initial_trip character varying(7) NOT NULL,
stn_code character varying(6) NULL,
area character varying(4) NULL,
stratum character varying(4) NULL,
lat integer NULL,
nors character varying(1) NULL,
long integer NULL,
eorw character varying(1) NULL,
site_type character varying(8) NULL,
dlat numeric(7,5) NULL,
dlon numeric(8,5) NULL,
position geometry NULL,
CONSTRAINT pk_t_site PRIMARY KEY (site_key),
CONSTRAINT ui_t_site UNIQUE (initial_trip, stn_code),
CONSTRAINT enforce_dims_geom CHECK ((st_ndims("position") = 2)),
CONSTRAINT enforce_geotype_geom CHECK (((geometrytype("position") = 'POINT'::text) OR ("position" IS NULL))),
CONSTRAINT enforce_srid_geom CHECK ((st_srid("position") = 4326))
) TABLESPACE pg_default;
--
-- Name: minor_idx; Type: INDEX; Schema: sample; Owner: postgres
--
CREATE INDEX minor_idx ON test USING btree (major, minor);
COMMENT ON INDEX minor_idx IS 'just another btree index';
--
-- Name: foo_bar_baz_0_pkey; Type: INDEX ATTACH; Schema: sample; Owner:
--
ALTER INDEX foo_bar_baz_pkey ATTACH PARTITION foo_bar_baz_0_pkey;
--
-- Name: foo_bar_baz_1_pkey; Type: INDEX ATTACH; Schema: sample; Owner:
--
ALTER INDEX foo_bar_baz_pkey ATTACH PARTITION foo_bar_baz_1_pkey;
--
-- Name: foo_bar_baz_2_pkey; Type: INDEX ATTACH; Schema: sample; Owner:
--
ALTER INDEX foo_bar_baz_pkey ATTACH PARTITION foo_bar_baz_2_pkey;
--
-- Name: foo_bar_baz_3_pkey; Type: INDEX ATTACH; Schema: sample; Owner:
--
ALTER INDEX foo_bar_baz_pkey ATTACH PARTITION foo_bar_baz_3_pkey;
--
-- Name: foo_bar_baz_4_pkey; Type: INDEX ATTACH; Schema: sample; Owner:
--
ALTER INDEX foo_bar_baz_pkey ATTACH PARTITION foo_bar_baz_4_pkey;
--
-- Name: foo_bar_baz_5_pkey; Type: INDEX ATTACH; Schema: sample; Owner:
--
ALTER INDEX foo_bar_baz_pkey ATTACH PARTITION foo_bar_baz_5_pkey;
--
-- Name: emp emp_stamp; Type: TRIGGER; Schema: sample; Owner: postgres
--
CREATE TRIGGER emp_stamp BEFORE INSERT OR UPDATE ON emp FOR EACH ROW EXECUTE PROCEDURE emp_stamp();
--
-- Name: address address_id_fkey; Type: FK CONSTRAINT; Schema: sample; Owner: postgres
--
ALTER TABLE ONLY address
ADD CONSTRAINT address_id_fkey FOREIGN KEY (id) REFERENCES person(id);
--
-- Name: SCHEMA sample; Type: ACL; Schema: -; Owner: postgres
--
--
-- Name: FUNCTION aaa(); Type: ACL; Schema: sample; Owner: postgres
--