diff --git a/README.md b/README.md index 8ca565d..99ea367 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,75 @@ -This a repository for scripts and sql querys ------------------------- +# postgresql-useful-scripts -1) postgresql +This a repository for useful PostgreSQL scripts and sql querys -* execute_all.sh: master file to execute all the sql files. +## Use -Files are contained inside of the dev_postgres_database folder, with the below structure: +Give permissions to execute_all.sh file: -a) schema -> useful_queries: a several useful querys related with structures of postgres (schemas, sequences, tables, functions, triggers), dba monitoring querys and more. + chmod +x execute_all.sh -b) schema -> functions: a several PL/pgSQL functions with useful code. +Login as a postgres user: -c) schema -> tables: examples tables to use in the plpgsql_scripts. + sudo -i -u postgres -d) schema -> data: mock data to use in the plpgsql_scripts. +And execute the execute_all.sh file: + + ./execute_all.sh + +## Structure: + +Files are contained inside of the dev_postgres_database folder, with the follow structure: dev_postgres_database -> schema -> functions. + +1. dba_scripts.sql: + +- Display actives connections +- Display the activity of your database +- Display connections by client_addr +- Display usaged size on disk by database +- Display usaged size of a specific table +- Display usaged size of tables by schemas + +2. udf_check_value.sql, udf_adjust_sequence.sql: scripts to adjust sequences. + +3. udf_kill_connections_pg3.sql, udf_kill_connections_pg4.sql: scripts used to controll the excessive number of queries windows opened in pgadmin 3 and 4. + +4. useful_structure_queries.sql: several useful querys related with schemas, sequences, tables, columns, triggers, constraints, functions and pg_notify channels. + +5. udf_generate_entity_database_commments: is a simple script to generate the comment template for tables, columns and functions related with a schema list that you supply. + +- List schemas names +- List all sequences information +- Display all the information about an specific sequence +- List sequences comments +- List all the tables names of an schema +- List all the tables of your database with their primary keys +- List all the tables indexs +- List all columns for a specified table +- List all columns for a specified table (another option) +- Search an specific column excluding postgres pg_catalogs +- List all the triggers +- List all the triggers of a schema +- List all the foreign keys +- List all the functions including postgres +- List all the functions of a schema +- Search (functions, tables, colums...) in all the functions (Great function!) +- List function comments of a schema +- List current channels listening +- List information for data dictionary + +5. useful_postgis_queries.sql: several useful querys related with geolocation using postgis extension. + +- Distance between 2 points (longitude latitude) based on WGS 84 +- Get X,Y coordinates from geometry point +- Get geometry value from text (longitude latitude) +- Find intersection based on WGS 84 +- Find near by places within X km from a current position(longitude,latitude) + +6. useful_fts_queries.sql: examples of how to do full text search with different case of searches, given stop words, upper or lower cases, accents, white spaces, multiple strings, incomplete strings or multiple incomplete strings. Contributions ----------------------- All work to improve performance is good - - Enjoy it! diff --git a/execute_all.sh b/execute_all.sh index 2e41c71..36a2e04 100755 --- a/execute_all.sh +++ b/execute_all.sh @@ -1,22 +1,7 @@ -# Execute from postgres user: -# chmod +x execute_all.sh -# sudo -i -u postgres psql -U postgres -a -f ./postgresql/main_database.sql -psql -U postgres -d dev_postgres_database -a -c "CREATE SCHEMA temporal_schema;" -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/functions/udt_general_tables_insert.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_categories.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_categories.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_endangered_animals.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_endangered_animals.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_reasons.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_reasons.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_relation_endangered_reasons.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_relation_endangered_reasons.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/data/tbl_categories.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/data/tbl_endangered_animals.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/data/tbl_reasons.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/data/tbl_relation_endangered_reasons.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/functions/udf_kill_connections_pg3.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/functions/udf_kill_connections_pg4.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_get.sql -psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_reasons_tree_get.sql +psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/public/functions/udf_kill_connections_pg3.sql +psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/public/functions/udf_kill_connections_pg4.sql +psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/public/functions/udf_check_value.sql +psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/public/functions/udf_adjust_sequence.sql +psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/public/tables/places.sql +psql -U postgres -d dev_postgres_database -a -f ./postgresql/dev_postgres_database/public/data/places.sql \ No newline at end of file diff --git a/postgresql/dev_postgres_database/public/data/places.sql b/postgresql/dev_postgres_database/public/data/places.sql new file mode 100644 index 0000000..25736ed --- /dev/null +++ b/postgresql/dev_postgres_database/public/data/places.sql @@ -0,0 +1,3 @@ +INSERT INTO public.places (id, name, created_at, updated_at, is_active, is_deleted) VALUES (1, 'El Salto Ángel', now(), now(), true, false); +INSERT INTO public.places (id, name, created_at, updated_at, is_active, is_deleted) VALUES (2, 'The Wizarding World of Harry Potter', now(), now(), true, false); +INSERT INTO public.places (id, name, created_at, updated_at, is_active, is_deleted) VALUES (3, 'Triángulo de las Bermudas', now(), now(), true, false); diff --git a/postgresql/dev_postgres_database/temporal_schema/useful_queries/dba_scripts.sql b/postgresql/dev_postgres_database/public/functions/dba_scripts.sql similarity index 100% rename from postgresql/dev_postgres_database/temporal_schema/useful_queries/dba_scripts.sql rename to postgresql/dev_postgres_database/public/functions/dba_scripts.sql diff --git a/postgresql/dev_postgres_database/public/functions/udf_adjust_sequence.sql b/postgresql/dev_postgres_database/public/functions/udf_adjust_sequence.sql new file mode 100644 index 0000000..ed29c85 --- /dev/null +++ b/postgresql/dev_postgres_database/public/functions/udf_adjust_sequence.sql @@ -0,0 +1,60 @@ +-- DROP FUNCTION udf_adjust_sequence); +CREATE OR REPLACE FUNCTION udf_adjust_sequence() +RETURNS INT AS +$BODY$ +DECLARE + d_seq_list record; + +BEGIN + + -- If the column value is greater than the sequence + FOR d_seq_list IN ( + + WITH tmp_sequence_list AS ( + + SELECT + c.table_schema || '.'|| c.table_name table_name, + c.column_name, + substring(c.column_default FROM position('(' in c.column_default) + 2 FOR (position('::' in c.column_default) - (position('(' in c.column_default) + 3)) ) column_default + FROM + information_schema.columns c + WHERE + c.data_type = 'integer' + AND table_schema NOT IN ('pg_catalog', 'pg_toast','pgagent','information_schema') + AND c.column_default ~~ 'nextval%' + AND c.column_default ~~ '%_seq%' + ORDER BY c.table_schema, c.table_name + + ), tmp_values AS ( + + SELECT + column_default, + ( + SELECT + udf_check_value('SELECT last_value FROM '|| column_default, TRUE)) sequence_value, + coalesce((SELECT udf_check_value('SELECT max(' || column_name || ') FROM '|| table_name, TRUE)), 0) column_value + FROM tmp_sequence_list + ) + SELECT * + FROM + tmp_values + WHERE + (column_value > sequence_value) or (sequence_value > (column_value+1)) + ) + LOOP + + PERFORM udf_check_value('ALTER SEQUENCE ' || d_seq_list.column_default || ' RESTART WITH ' || (d_seq_list.column_value + 1), FALSE); + + END LOOP; + + RETURN 1; + +END; + +$BODY$ +LANGUAGE plpgsql VOLATILE +COST 100; + +ALTER FUNCTION udf_adjust_sequence() + OWNER TO postgres; +COMMENT ON FUNCTION udf_adjust_sequence() IS 'Adjust all the Sequence which name ends with "_seq" in all the tables in all the schemas (excluding postgres)'; diff --git a/postgresql/dev_postgres_database/public/functions/udf_check_value.sql b/postgresql/dev_postgres_database/public/functions/udf_check_value.sql new file mode 100644 index 0000000..402ae85 --- /dev/null +++ b/postgresql/dev_postgres_database/public/functions/udf_check_value.sql @@ -0,0 +1,33 @@ +-- DROP FUNCTION udf_check_value(TEXT, BOOLEAN); +CREATE OR REPLACE FUNCTION udf_check_value( + i_query TEXT, + i_result BOOLEAN + ) +RETURNS INT AS +$BODY$ +DECLARE + d_value INT := 0; +BEGIN + /* + To test: + SELECT udf_check_value('SELECT last_value FROM seqname_id_seq', TRUE); + */ + + IF i_result THEN + EXECUTE i_query INTO d_value; + ELSE + EXECUTE i_query; + END IF; + + RETURN d_value; + +END; +$BODY$ + LANGUAGE plpgsql VOLATILE + COST 100; +ALTER FUNCTION udf_check_value(TEXT, BOOLEAN) + OWNER TO postgres; +COMMENT ON FUNCTION udf_check_value(TEXT, BOOLEAN) IS 'Returns the last value of a sequence'; + + + diff --git a/postgresql/dev_postgres_database/public/functions/udf_generate_entity_database_commments.sql b/postgresql/dev_postgres_database/public/functions/udf_generate_entity_database_commments.sql new file mode 100644 index 0000000..221af87 --- /dev/null +++ b/postgresql/dev_postgres_database/public/functions/udf_generate_entity_database_commments.sql @@ -0,0 +1,87 @@ +-- DROP FUNCTION IF EXISTS public.udf_generate_entity_database_commments(character varying[]); +CREATE OR REPLACE FUNCTION public.udf_generate_entity_database_commments(param_schema_list character varying[]) + RETURNS void + LANGUAGE 'plpgsql' + COST 100 + VOLATILE +AS $BODY$ + +DECLARE + + /* + To test: + SELECT public.udf_generate_entity_database_commments(array['public']); + */ + + i integer; + j RECORD; + k RECORD; + w RECORD; + + BEGIN + + -- Iterate on each schema + FOR i IN 1 .. array_upper(param_schema_list, 1) + LOOP + + -- Iterate on each table of the current schema + FOR j IN ( + SELECT + tablename + FROM + pg_tables + WHERE + schemaname = param_schema_list[i] + ) + LOOP + + -- Generate table comment + RAISE NOTICE 'COMMENT ON TABLE %.% is '''';',param_schema_list[i],j.tablename; + + -- Get all the columns name of the current table + FOR k IN ( + SELECT + column_name + FROM + information_schema.columns + WHERE + table_schema = 'public' + AND + table_name = j.tablename + ORDER BY + column_name + ) + LOOP + + -- Generate column comment + RAISE NOTICE 'COMMENT ON COLUMN %.%.% is '''';',param_schema_list[i],j.tablename,k.column_name; + + END LOOP; + + END LOOP; + + -- Iterate on each function of the current schema + FOR w IN ( + SELECT + proname + FROM + pg_catalog.pg_proc f + INNER JOIN + pg_catalog.pg_namespace n ON (f.pronamespace = n.oid) + WHERE + n.nspname = param_schema_list[i] + ) + LOOP + + -- Generate function comment + RAISE NOTICE 'COMMENT ON FUNCTION %.% is '''';',param_schema_list[i],w.proname; + + END LOOP; + + END LOOP; + +END; + +$BODY$; + +ALTER FUNCTION public.udf_generate_entity_database_commments(character varying[]) OWNER TO postgres; \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/functions/udf_kill_connections_pg3.sql b/postgresql/dev_postgres_database/public/functions/udf_kill_connections_pg3.sql similarity index 100% rename from postgresql/dev_postgres_database/temporal_schema/functions/udf_kill_connections_pg3.sql rename to postgresql/dev_postgres_database/public/functions/udf_kill_connections_pg3.sql diff --git a/postgresql/dev_postgres_database/temporal_schema/functions/udf_kill_connections_pg4.sql b/postgresql/dev_postgres_database/public/functions/udf_kill_connections_pg4.sql similarity index 100% rename from postgresql/dev_postgres_database/temporal_schema/functions/udf_kill_connections_pg4.sql rename to postgresql/dev_postgres_database/public/functions/udf_kill_connections_pg4.sql diff --git a/postgresql/dev_postgres_database/temporal_schema/functions/udt_general_tables_insert.sql b/postgresql/dev_postgres_database/public/functions/udt_general_tables_insert.sql similarity index 100% rename from postgresql/dev_postgres_database/temporal_schema/functions/udt_general_tables_insert.sql rename to postgresql/dev_postgres_database/public/functions/udt_general_tables_insert.sql diff --git a/postgresql/dev_postgres_database/public/functions/useful_fts_queries.sql b/postgresql/dev_postgres_database/public/functions/useful_fts_queries.sql new file mode 100644 index 0000000..3e64e9f --- /dev/null +++ b/postgresql/dev_postgres_database/public/functions/useful_fts_queries.sql @@ -0,0 +1,75 @@ +-- To search a string in a column +SELECT + * +FROM + +WHERE + (to_tsvector('spanish',UNACCENT()) @@ to_tsquery('spanish',UNACCENT(replace(trim() || ':*',' ','&')))) + +/* +# Steps: +1. Remove white spaces at the begining or at the end with trim() + +2. Append :* at the end of the string to allow to search incomplete string + +3. Replace space between multiple words with & to allow to search incomplete string with multiple words + +4. Remove accents with UNACCENT(), previously installed with: +CREATE EXTENSION unaccent; + +5. Remove (spanish) stop words with to_tsquery() + +6. Match string with the column selected + +* Important Note: consider that the column should have some index (GIN, GiST or B-tree, see more information here: +https://www.postgresql.org/docs/current/textsearch-indexes.html) +To make improvements in fast searches + +* To check efficient of this query, use: +EXPLAIN ANALYZE query + +See important documentation below (according with your postgresql version): +- https://www.postgresql.org/docs/current/textsearch.html +- https://www.postgresql.org/docs/11/pgtrgm.html +- https://stackoverflow.com/questions/2513501/postgresql-full-text-search-how-to-search-partial-words +- http://rachbelaid.com/postgres-full-text-search-is-good-enough/ + +# Examples of Search: + +SELECT + * +FROM + places +WHERE + (to_tsvector('spanish',UNACCENT(name)) @@ to_tsquery('spanish',UNACCENT(replace(trim('ÁNGEL') || ':*',' ','&')))) + +- First place: "El Salto Ángel" +- Searches: +ángel +angel +ÁNGEL +El salto angel +El Salto Ángel +El Sal +salto a +s +sa + sa + +- Second place (with to_tsvector('english'): "The Wizarding World of Harry Potter" +- Searches: +The Wizarding World of Harry Potter +Harry Potter +Potter +Pot +Potter Harry +w +wi + +- Third place: "Triángulo de las Bermudas" +- Searches: +triángulo +el triángulo +bermudas + +*/ \ No newline at end of file diff --git a/postgresql/dev_postgres_database/public/functions/useful_postgis_queries.sql b/postgresql/dev_postgres_database/public/functions/useful_postgis_queries.sql new file mode 100644 index 0000000..69aa8cb --- /dev/null +++ b/postgresql/dev_postgres_database/public/functions/useful_postgis_queries.sql @@ -0,0 +1,151 @@ +-- PostGIS tests + +----------------------------------------------- +-- ABOUT INSTALLATION +----------------------------------------------- + +-- Default installation +CREATE EXTENSION postgis; + +/* +-- See more about the installation and aditional extensions here: + +http://postgis.net/install/ + +https://postgis.net/docs/postgis_installation.html#idm559 + +*/ + +----------------------------------------------- +-- THINGS YOU SHOULD KNOW RELATED WITH GEOLOCALIZATION AND BEFORE TO WORK WITH POSTGIS +----------------------------------------------- + +/* +-- WGS 84 (World Geodetic System 1984) +https://es.wikipedia.org/wiki/WGS84 + +-- What are EPSG / SRID codes and their link with PostGIS +https://mappinggis.com/2016/04/los-codigos-epsg-srid-vinculacion-postgis/ + +-- Spatial reference system +https://en.wikipedia.org/wiki/Spatial_reference_system + +-- Introduction to Spatial References (See 4326 - GPS section) +https://developers.arcgis.com/documentation/core-concepts/spatial-references/ + +-- Reverse Geocoding +https://en.wikipedia.org/wiki/Reverse_geocoding + +-- Admin level reference (useful for reverse geocoding queries, not only for PostGIS) +https://wiki.openstreetmap.org/wiki/Nominatim/Development_overview#Country_to_street_level + +-- Nearest-Neighbour Searching +https://postgis.net/workshops/postgis-intro/knn.html + +*/ + +----------------------------------------------- +-- ABOUT DOCUMENTATION +----------------------------------------------- + +/* + +-- Official Documentation +http://postgis.net/docs/ + +-- PostGIS 2.5.3dev Manual +http://postgis.net/stuff/postgis-2.5.pdf + +-- PostGIS reference for functions description +http://postgis.net/docs/reference.html + +-- FAQ: +https://postgis.net/docs/PostGIS_FAQ.html + +http://postgis.net/2013/08/30/tip_ST_Set_or_Transform/ + +-- PostGIS official repository: +https://trac.osgeo.org/postgis/ + +https://github.com/postgis/postgis + +-- pgRouting official repository +https://github.com/pgRouting/pgrouting + +*/ + +----------------------------------------------- +-- USEFUL QUERIES +----------------------------------------------- + +-- Adding a geometry columns: +ALTER TABLE + ADD COLUMN geom geometry(Geometry,4326); + +ALTER TABLE + ADD COLUMN position geometry(Point,4326); + +-- Traduce your lat and long columns in a geometry point based on WGS 84 +UPDATE + SET = ST_SetSRID(ST_MakePoint(,),4326); + +-- Distance between 2 points (longitude latitude) based on WGS 84 +SELECT ST_Distance( + ST_GeometryFromText('POINT(-118.4079 33.9434)', 4326), -- Los Angeles (LAX) + ST_GeometryFromText('POINT(2.5559 49.0083)', 4326) -- Paris (CDG) + ); + +-- Get X,Y coordinates from geometry point +SELECT + ST_X() latitude, + ST_Y() longitude, + ST_AsText() +FROM + ; + +-- Get geometry value from text (longitude latitude) +SELECT + ST_GeogFromText('SRID=4267;POINT(-77.0092 38.889588)'); + +-- Get geometry value from text (longitude latitude) +SELECT + ST_Transform(.,4269) +FROM + ; + +-- Find intersection based on WGS 84 +SELECT + +FROM + +WHERE + ST_intersects(ST_GeographyFromText('SRID=4326;POINT(' || . || ' ' || . || ')'), .) + +-- Find near by places within 5 km from a current position(longitude,latitude) +SELECT + * +FROM + +WHERE + ST_DistanceSphere(""."", ST_GeomFromEWKB(ST_MakePoint(,)::bytea)) <= 5000.0 + +-- Get the first 5 nearest records disregard (<->) how far the are away from the current location (latitude and longitude as a parameters) +SELECT + * +FROM + +ORDER BY + . <-> ST_SetSRID(ST_MakePoint(param_lng,param_lat),4326) LIMIT 5; + + +-- Basic grid clustering with SnapToGrid +SELECT + array_agg(id) AS ids, + COUNT( position ) AS count, + ST_AsText( ST_Centroid(ST_Collect( position )) ) AS center +FROM + +GROUP BY + ST_SnapToGrid( ST_SetSRID(position, 4326), 22.25, 11.125) +ORDER BY + count DESC; \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/useful_queries/useful_structure_queries.sql b/postgresql/dev_postgres_database/public/functions/useful_structure_queries.sql similarity index 92% rename from postgresql/dev_postgres_database/temporal_schema/useful_queries/useful_structure_queries.sql rename to postgresql/dev_postgres_database/public/functions/useful_structure_queries.sql index 8db3f74..00c77f7 100644 --- a/postgresql/dev_postgres_database/temporal_schema/useful_queries/useful_structure_queries.sql +++ b/postgresql/dev_postgres_database/public/functions/useful_structure_queries.sql @@ -353,3 +353,27 @@ WHERE LIKE '%LISTEN%' ORDER BY backend_start; +----------------------------------------------- +-- RELATED WITH DATA DICTIONARY +----------------------------------------------- + +-- To list information for data dictionary +SELECT + t1.TABLE_SCHEMA, + t1.TABLE_NAME, + t1.COLUMN_NAME, + t1.COLUMN_DEFAULT, + t1.IS_NULLABLE, + t1.DATA_TYPE, + COALESCE(t1.NUMERIC_PRECISION, + t1.CHARACTER_MAXIMUM_LENGTH) AS COLUMN_LENGHT, + PG_CATALOG.COL_DESCRIPTION(t2.OID, + t1.DTD_IDENTIFIER::int) AS COLUMN_DESCRIPTION, + t1.DOMAIN_NAME AS COLUMN_DOMAIN +FROM + INFORMATION_SCHEMA.COLUMNS t1 + INNER JOIN PG_CLASS t2 ON (t2.RELNAME = t1.TABLE_NAME) +WHERE + t1.TABLE_SCHEMA = 'your_schema_name' +ORDER BY + t1.TABLE_NAME; diff --git a/postgresql/dev_postgres_database/public/tables/places.sql b/postgresql/dev_postgres_database/public/tables/places.sql new file mode 100644 index 0000000..6fe91a6 --- /dev/null +++ b/postgresql/dev_postgres_database/public/tables/places.sql @@ -0,0 +1,8 @@ +CREATE TABLE public.places( + id serial primary key, + name character varying(100) NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + is_active boolean DEFAULT true, + is_deleted boolean DEFAULT false +); \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/data/tbl_categories.sql b/postgresql/dev_postgres_database/temporal_schema/data/tbl_categories.sql deleted file mode 100644 index 039eb4d..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/data/tbl_categories.sql +++ /dev/null @@ -1,2 +0,0 @@ -INSERT INTO temporal_schema.tbl_categories (name,last_modified_date) VALUES ('Mammals',NOW()); -INSERT INTO temporal_schema.tbl_categories (name,last_modified_date) VALUES ('Reptile',NOW()); \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/data/tbl_endangered_animals.sql b/postgresql/dev_postgres_database/temporal_schema/data/tbl_endangered_animals.sql deleted file mode 100644 index 98f1fc5..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/data/tbl_endangered_animals.sql +++ /dev/null @@ -1,4 +0,0 @@ -INSERT INTO temporal_schema.tbl_endangered_animals (name,id_categories,last_modified_date) VALUES ('Bengal tiger',1,NOW()); -INSERT INTO temporal_schema.tbl_endangered_animals (name,id_categories,last_modified_date) VALUES ('Giant panda',1,NOW()); -INSERT INTO temporal_schema.tbl_endangered_animals (name,id_categories,last_modified_date) VALUES ('Green Turtle',2,NOW()); -INSERT INTO temporal_schema.tbl_endangered_animals (name,id_categories,last_modified_date) VALUES ('Hippopotamus',1,NOW()); diff --git a/postgresql/dev_postgres_database/temporal_schema/data/tbl_reasons.sql b/postgresql/dev_postgres_database/temporal_schema/data/tbl_reasons.sql deleted file mode 100644 index f1042b3..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/data/tbl_reasons.sql +++ /dev/null @@ -1,4 +0,0 @@ -INSERT INTO temporal_schema.tbl_reasons (description,last_modified_date) VALUES ('Poaching',NOW()); -INSERT INTO temporal_schema.tbl_reasons (description,last_modified_date) VALUES ('Sales',NOW()); -INSERT INTO temporal_schema.tbl_reasons (description,last_modified_date) VALUES ('Sea contamination',NOW()); -INSERT INTO temporal_schema.tbl_reasons (description,last_modified_date) VALUES ('Destruction of territory',NOW()); diff --git a/postgresql/dev_postgres_database/temporal_schema/data/tbl_relation_endangered_reasons.sql b/postgresql/dev_postgres_database/temporal_schema/data/tbl_relation_endangered_reasons.sql deleted file mode 100644 index 357e00a..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/data/tbl_relation_endangered_reasons.sql +++ /dev/null @@ -1,6 +0,0 @@ -INSERT INTO temporal_schema.tbl_relation_endangered_reasons (id_endangered_animals,id_categories,id_reasons,last_modified_date) VALUES (1,1,1,NOW()); -INSERT INTO temporal_schema.tbl_relation_endangered_reasons (id_endangered_animals,id_categories,id_reasons,last_modified_date) VALUES (1,1,2,NOW()); -INSERT INTO temporal_schema.tbl_relation_endangered_reasons (id_endangered_animals,id_categories,id_reasons,last_modified_date) VALUES (2,1,1,NOW()); -INSERT INTO temporal_schema.tbl_relation_endangered_reasons (id_endangered_animals,id_categories,id_reasons,last_modified_date) VALUES (3,2,3,NOW()); -INSERT INTO temporal_schema.tbl_relation_endangered_reasons (id_endangered_animals,id_categories,id_reasons,last_modified_date) VALUES (4,1,1,NOW()); -INSERT INTO temporal_schema.tbl_relation_endangered_reasons (id_endangered_animals,id_categories,id_reasons,last_modified_date) VALUES (4,1,4,NOW()); diff --git a/postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_get.sql b/postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_get.sql deleted file mode 100644 index 57d778e..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_get.sql +++ /dev/null @@ -1,65 +0,0 @@ --- DROP FUNCTION temporal_schema.udf_endangered_animals_get(INTEGER); - -CREATE OR REPLACE FUNCTION temporal_schema.udf_endangered_animals_get(param_id_endangered_animals INTEGER) - RETURNS character varying - LANGUAGE plpgsql -AS $function$ - -DECLARE - -- This is a comment: in this section you can declare your variables - - local_returning VARCHAR(200) = ''; - - BEGIN - - /* It is a suggest of a good header - Author: - Creation Date: - Review: - - -- To Test: you can use the next line, to test this function - SELECT temporal_schema.udf_endangered_animals_get(1); - */ - - -- This is a validation to check if the param_id_endangered_animals exist - IF EXISTS ( - SELECT - name - FROM - temporal_schema.tbl_endangered_animals - WHERE - id_endangered_animals = param_id_endangered_animals - AND - is_active - AND - not is_deleted - ) THEN - - -- You can use 'INTO STRICT' statement, to store data returned into a variable. Be carefull if your query return more than one values - SELECT - name INTO STRICT local_returning - FROM - temporal_schema.tbl_endangered_animals - WHERE - id_endangered_animals = param_id_endangered_animals - AND - is_active - AND - not is_deleted; - - -- You can use 'RAISE NOTICE' statement to display messages and debug your code - RAISE NOTICE 'Data found'; - - ELSE - - RAISE NOTICE 'Not data found'; - - END IF; - - RETURN local_returning; - -END; - -$function$; - -COMMENT ON FUNCTION temporal_schema.udf_endangered_animals_get(INTEGER) IS 'This function is used to get data from temporal_schema.udf_endangered_animals_get'; diff --git a/postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_reasons_tree_get.sql b/postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_reasons_tree_get.sql deleted file mode 100644 index 1fa9206..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/functions/udf_endangered_animals_reasons_tree_get.sql +++ /dev/null @@ -1,120 +0,0 @@ --- DROP FUNCTION temporal_schema.udf_endangered_animals_reasons_tree_get(INTEGER); - -CREATE OR REPLACE FUNCTION temporal_schema.udf_endangered_animals_reasons_tree_get() - RETURNS json - LANGUAGE plpgsql -AS $function$ - -DECLARE - - json_returning json = '{}'; - - BEGIN - - /* - -- To Test: - SELECT temporal_schema.udf_endangered_animals_reasons_tree_get(); - */ - - WITH temporal_category_table AS ( - SELECT - id_categories, - name, - is_active, - is_deleted - FROM - temporal_schema.tbl_categories - WHERE - is_active - AND - not is_deleted - ), temporal_relation_endangered_reasons AS ( - SELECT - id_endangered_animals, - id_categories, - id_reasons, - is_active, - is_deleted - FROM - temporal_schema.tbl_relation_endangered_reasons - WHERE - is_active - AND - not is_deleted - ) - - SELECT JSON_AGG(a.*) INTO STRICT json_returning - FROM ( - SELECT - tct.id_categories "categoryId", - tct.name "categoryName", - true as "isActive", - ( - SELECT ARRAY_AGG(b.*) "animalList" - FROM ( - SELECT - -- DISTINCT ON (ea.id_endangered_animals) -- it also could be, distinct on rather than group by - ea.id_endangered_animals "animalId", - ea.name "animalName", - true as "isActive", - ( - SELECT JSON_AGG(c.*) "endangeredReasonsList" - FROM ( - SELECT - r.id_reasons "reasonId", - r.description "reason", - true as "isActive" - FROM temporal_schema.tbl_reasons r - INNER JOIN temporal_relation_endangered_reasons trer - ON r.id_reasons = trer.id_reasons - AND - trer.id_endangered_animals = ea.id_endangered_animals - AND - trer.id_categories = tct.id_categories - AND - r.is_active - AND - not r.is_deleted - AND - trer.is_active - AND - not trer.is_deleted - )c - ) - FROM temporal_schema.tbl_endangered_animals ea - INNER JOIN temporal_schema.tbl_relation_endangered_reasons rer - ON ea.id_endangered_animals = rer.id_endangered_animals - AND - ea.id_categories = rer.id_categories - AND - ea.id_categories = tct.id_categories - AND - ea.is_active - AND - not ea.is_deleted - AND - rer.is_active - AND - not rer.is_deleted - GROUP BY - ea.id_endangered_animals, - ea.name - ORDER BY - ea.id_endangered_animals - )b - ) - FROM - temporal_category_table tct - WHERE - tct.is_active - AND - not tct.is_deleted - )a; - - RETURN json_returning; - -END; - -$function$; - -COMMENT ON FUNCTION temporal_schema.udf_endangered_animals_reasons_tree_get() IS 'This function is used to get a tree of endangered animals with endangered reasons.'; diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_categories.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_categories.sql deleted file mode 100644 index c3aac0d..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_categories.sql +++ /dev/null @@ -1,11 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_categories; -CREATE TABLE temporal_schema.tbl_categories ( - id_categories SERIAL NOT NULL PRIMARY KEY, - name VARCHAR(255), - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL -); -CREATE TRIGGER trigger_categories_insert BEFORE UPDATE OR DELETE ON temporal_schema.tbl_categories - FOR EACH ROW - EXECUTE PROCEDURE udt_general_tables_insert(); diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_endangered_animals.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_endangered_animals.sql deleted file mode 100644 index d845261..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_endangered_animals.sql +++ /dev/null @@ -1,14 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_endangered_animals; -CREATE TABLE temporal_schema.tbl_endangered_animals ( - id_endangered_animals SERIAL NOT NULL, - name VARCHAR(255), - id_categories INTEGER, - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL, - created_date timestamp with time zone DEFAULT NOW(), - PRIMARY KEY (id_endangered_animals,id_categories) -); -CREATE TRIGGER trigger_endangered_animals_insert BEFORE UPDATE OR DELETE ON temporal_schema.tbl_endangered_animals - FOR EACH ROW - EXECUTE PROCEDURE udt_general_tables_insert(); \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_categories.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_categories.sql deleted file mode 100644 index 5608645..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_categories.sql +++ /dev/null @@ -1,11 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_log_categories; -CREATE TABLE temporal_schema.tbl_log_categories ( - id_categories INTEGER, - name VARCHAR(255), - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL, - last_modified_by CHARACTER VARYING(100), - ip_user CHARACTER VARYING(100), - update_or_deleted_date timestamp with time zone NOT NULL -); \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_endangered_animals.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_endangered_animals.sql deleted file mode 100644 index d45c940..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_endangered_animals.sql +++ /dev/null @@ -1,14 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_log_endangered_animals; -CREATE TABLE temporal_schema.tbl_log_endangered_animals ( - id_endangered_animals INTEGER, - name VARCHAR(255), - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL, - last_modified_by CHARACTER VARYING(100), - ip_user CHARACTER VARYING(100), - update_or_deleted_date timestamp with time zone NOT NULL -); -ALTER TABLE temporal_schema.tbl_endangered_animals ADD CONSTRAINT tbll_endangered_animals_tb_categories - FOREIGN KEY (id_categories) REFERENCES temporal_schema.tbl_categories (id_categories) -ON UPDATE CASCADE; \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_reasons.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_reasons.sql deleted file mode 100644 index 536073c..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_reasons.sql +++ /dev/null @@ -1,11 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_log_reasons; -CREATE TABLE temporal_schema.tbl_log_reasons ( - id_reasons INTEGER, - name VARCHAR(255), - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL, - last_modified_by CHARACTER VARYING(100), - ip_user CHARACTER VARYING(100), - update_or_deleted_date timestamp with time zone NOT NULL -); \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_relation_endangered_reasons.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_relation_endangered_reasons.sql deleted file mode 100644 index af5ff22..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_log_relation_endangered_reasons.sql +++ /dev/null @@ -1,11 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_log_relation_endangered_reasons; -CREATE TABLE temporal_schema.tbl_log_relation_endangered_reasons ( - id_relation_endangered_reasons INTEGER, - name VARCHAR(255), - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL, - last_modified_by CHARACTER VARYING(100), - ip_user CHARACTER VARYING(100), - update_or_deleted_date timestamp with time zone NOT NULL -); \ No newline at end of file diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_reasons.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_reasons.sql deleted file mode 100644 index e45d52f..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_reasons.sql +++ /dev/null @@ -1,11 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_reasons; -CREATE TABLE temporal_schema.tbl_reasons ( - id_reasons SERIAL NOT NULL PRIMARY KEY, - description VARCHAR(255), - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL -); -CREATE TRIGGER trigger_reasons_insert BEFORE UPDATE OR DELETE ON temporal_schema.tbl_reasons - FOR EACH ROW - EXECUTE PROCEDURE udt_general_tables_insert(); diff --git a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_relation_endangered_reasons.sql b/postgresql/dev_postgres_database/temporal_schema/tables/tbl_relation_endangered_reasons.sql deleted file mode 100644 index f16a7fe..0000000 --- a/postgresql/dev_postgres_database/temporal_schema/tables/tbl_relation_endangered_reasons.sql +++ /dev/null @@ -1,21 +0,0 @@ ---DROP TABLE IF EXISTS temporal_schema.tbl_relation_endangered_reasons; -CREATE TABLE temporal_schema.tbl_relation_endangered_reasons ( - id_endangered_reasons_relation SERIAL, - id_endangered_animals INTEGER NOT NULL, - id_categories INTEGER NOT NULL, - id_reasons INTEGER NOT NULL, - is_active BOOLEAN DEFAULT true, - is_deleted BOOLEAN DEFAULT false, - last_modified_date timestamp with time zone NOT NULL, - created_date timestamp with time zone DEFAULT NOW(), - PRIMARY KEY (id_endangered_reasons_relation,id_categories,id_endangered_animals,id_reasons) -); -ALTER TABLE temporal_schema.tbl_relation_endangered_reasons ADD CONSTRAINT tbl_relation_endangered_reasons_tbl_reasons - FOREIGN KEY (id_reasons) REFERENCES temporal_schema.tbl_reasons (id_reasons) -ON UPDATE CASCADE; -ALTER TABLE temporal_schema.tbl_relation_endangered_reasons ADD CONSTRAINT tbl_relation_endangered_reasons_tbl_endangered_animals - FOREIGN KEY (id_endangered_animals,id_categories) REFERENCES temporal_schema.tbl_endangered_animals (id_endangered_animals,id_categories) -ON UPDATE CASCADE; -CREATE TRIGGER trigger_relation_endangered_reasons_insert BEFORE UPDATE OR DELETE ON temporal_schema.tbl_relation_endangered_reasons - FOR EACH ROW - EXECUTE PROCEDURE udt_general_tables_insert();