From b36f6fdd326e20c7203ce27b16001945da999fe3 Mon Sep 17 00:00:00 2001 From: Phillip Haydon Date: Fri, 25 Nov 2016 01:30:37 +0800 Subject: [PATCH 1/4] added new structure for sql-server to postgres --- .../images/postgres-table-definition.png | Bin 0 -> 3594 bytes documentation/documentation/order.txt | 1 + .../postgres/backup-restore/index.md | 4 ++ .../postgres/backup-restore/local.md | 4 ++ .../postgres/backup-restore/remote.md | 4 ++ .../postgres/casing/case-insensitive-data.md | 4 ++ .../documentation/postgres/casing/index.md | 4 ++ .../documentation/postgres/casing/order.txt | 4 ++ .../documentation/postgres/casing/queries.md | 4 ++ .../postgres/casing/unique-values.md | 4 ++ .../postgres/casing/using-duplicate-fields.md | 4 ++ .../documentation/postgres/casting.md | 4 ++ documentation/documentation/postgres/dates.md | 4 ++ .../documentation/postgres/escaping.md | 10 ++++ .../documentation/postgres/if_statements.md | 4 ++ documentation/documentation/postgres/index.md | 11 ++++ .../documentation/postgres/indexing.md | 4 ++ .../documentation/postgres/indexing_jsonb.md | 4 ++ .../documentation/postgres/naming.md | 53 ++++++++++++++++++ .../documentation/postgres/order.txt | 11 ++++ .../documentation/postgres/slow_queries.md | 4 ++ documentation/documentation/postgres/types.md | 4 ++ 22 files changed, 150 insertions(+) create mode 100644 documentation/content/images/postgres-table-definition.png create mode 100644 documentation/documentation/postgres/backup-restore/index.md create mode 100644 documentation/documentation/postgres/backup-restore/local.md create mode 100644 documentation/documentation/postgres/backup-restore/remote.md create mode 100644 documentation/documentation/postgres/casing/case-insensitive-data.md create mode 100644 documentation/documentation/postgres/casing/index.md create mode 100644 documentation/documentation/postgres/casing/order.txt create mode 100644 documentation/documentation/postgres/casing/queries.md create mode 100644 documentation/documentation/postgres/casing/unique-values.md create mode 100644 documentation/documentation/postgres/casing/using-duplicate-fields.md create mode 100644 documentation/documentation/postgres/casting.md create mode 100644 documentation/documentation/postgres/dates.md create mode 100644 documentation/documentation/postgres/escaping.md create mode 100644 documentation/documentation/postgres/if_statements.md create mode 100644 documentation/documentation/postgres/index.md create mode 100644 documentation/documentation/postgres/indexing.md create mode 100644 documentation/documentation/postgres/indexing_jsonb.md create mode 100644 documentation/documentation/postgres/naming.md create mode 100644 documentation/documentation/postgres/order.txt create mode 100644 documentation/documentation/postgres/slow_queries.md create mode 100644 documentation/documentation/postgres/types.md diff --git a/documentation/content/images/postgres-table-definition.png b/documentation/content/images/postgres-table-definition.png new file mode 100644 index 0000000000000000000000000000000000000000..044c4ae13134fe605a2681ea4c91702dadfcce70 GIT binary patch literal 3594 zcmai1XH-+!+6DwcBSk^NMY;qglmw&>MTP(qh)9!2F@OjJP)bM;L<9!#P(~#I1caa% zdXpw-5(Fi9rS~E*K}32-nsmPC%)M*wx9lB+~EjT?5FB`J4RT4q_u3W7337n|| zfsKu?@%vzJ@-K8}ManWWHnhFtyp-ASySSEc%z@t0sLv_=V~w0uPta0wB5IGouNqXe z6Tn|PV^lf)-+ay2?bZz)aQoms6T;C&=txYM?}yHr((?vi6o;-HS?8h^&}K6q;RBcF zNxBg`3uCj}Q)w>L3Zh#%e^US_5+}_krYM1vPT1t93N-CDJvCsj&xR(v*HMO zZsTHaNySN66~S??p#O?XFg=5#ozag5;Rj#!GDF*2CvqA9`E2uZ5O6A>dm4P$Cuni} zGXs0_E(6dcO6czrV^30AkAYuV5BA|Gc1wS}J=!zS@w>Qhu0b>~w}>w73f~*m+B?{^ zbj$m=yWfPHn_k$QShqjuUfa|#NcNX+t$Pz1blA9HkbSLHT!V0Qct9qT;Svhn-c%BG zb$`FW#Q<>a<8kUx$*sg%VP&js)A`lZO(%ONM|_NXi=U}&A&NEtf32+Wy~yAFx&`Am z>t?#jt_(1iO50v8{Nz}h=4mmeqS&ZBkg(NY3-L4@g%cfyRm2VVk$d}cF6X757C_4@ijSEiHrBPch+YZBOd!~`Rn>G z;!{*}DingfpSN6eKbLfMv$WRg@#ZB1-VaOM@gRAEU?TBt_oX@EZ{45t!gi}ybz{u9 z1qMnN7?;f)2MOHZ-XPyM_Z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q92F2Wa+==~tJa%5fBUiy(t2gWnY{6gU>ZpWHJ-5Y zN=b+gly^>IaXjkto#TWL-#IRS4*VB|wo$Iia10z(Lks + + +//TODO diff --git a/documentation/documentation/postgres/backup-restore/local.md b/documentation/documentation/postgres/backup-restore/local.md new file mode 100644 index 0000000000..2acd41469e --- /dev/null +++ b/documentation/documentation/postgres/backup-restore/local.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/backup-restore/remote.md b/documentation/documentation/postgres/backup-restore/remote.md new file mode 100644 index 0000000000..febd990fd2 --- /dev/null +++ b/documentation/documentation/postgres/backup-restore/remote.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/casing/case-insensitive-data.md b/documentation/documentation/postgres/casing/case-insensitive-data.md new file mode 100644 index 0000000000..f6766e17e7 --- /dev/null +++ b/documentation/documentation/postgres/casing/case-insensitive-data.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/casing/index.md b/documentation/documentation/postgres/casing/index.md new file mode 100644 index 0000000000..347167bda2 --- /dev/null +++ b/documentation/documentation/postgres/casing/index.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/casing/order.txt b/documentation/documentation/postgres/casing/order.txt new file mode 100644 index 0000000000..6412f5cf62 --- /dev/null +++ b/documentation/documentation/postgres/casing/order.txt @@ -0,0 +1,4 @@ +unique-values +case-insensitive-data +queries +using-duplicate-fields diff --git a/documentation/documentation/postgres/casing/queries.md b/documentation/documentation/postgres/casing/queries.md new file mode 100644 index 0000000000..0ac63fc189 --- /dev/null +++ b/documentation/documentation/postgres/casing/queries.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/casing/unique-values.md b/documentation/documentation/postgres/casing/unique-values.md new file mode 100644 index 0000000000..60c0b56ec3 --- /dev/null +++ b/documentation/documentation/postgres/casing/unique-values.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/casing/using-duplicate-fields.md b/documentation/documentation/postgres/casing/using-duplicate-fields.md new file mode 100644 index 0000000000..cf2fdaf14f --- /dev/null +++ b/documentation/documentation/postgres/casing/using-duplicate-fields.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/casting.md b/documentation/documentation/postgres/casting.md new file mode 100644 index 0000000000..befd5c1cc7 --- /dev/null +++ b/documentation/documentation/postgres/casting.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/dates.md b/documentation/documentation/postgres/dates.md new file mode 100644 index 0000000000..b15e6df75f --- /dev/null +++ b/documentation/documentation/postgres/dates.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/escaping.md b/documentation/documentation/postgres/escaping.md new file mode 100644 index 0000000000..46f4211daf --- /dev/null +++ b/documentation/documentation/postgres/escaping.md @@ -0,0 +1,10 @@ + + + +In SQL Server, escaping is done by wrapping with square brackets. + + select [Id] from [MyTable]; + +In Postgres this is done using double quotes. + + select "id" from "my_table"; diff --git a/documentation/documentation/postgres/if_statements.md b/documentation/documentation/postgres/if_statements.md new file mode 100644 index 0000000000..0fdc006709 --- /dev/null +++ b/documentation/documentation/postgres/if_statements.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/index.md b/documentation/documentation/postgres/index.md new file mode 100644 index 0000000000..ba31b8d840 --- /dev/null +++ b/documentation/documentation/postgres/index.md @@ -0,0 +1,11 @@ + + + +PostgreSQL has some of the best documentation available, and it's pretty easy to lookup any most anything to do with Postgres, however coming from SQL Server can sometimes be a bit of a barrier to entry. + +This documentation exists to help the transition for some of the basic usage patterns between the two databases and how help you better understand PostgreSQL so you can get the best out of what Marten has to offer. + + +## SQL Server to PostgreSQL Documentation + +<[TableOfContents]> diff --git a/documentation/documentation/postgres/indexing.md b/documentation/documentation/postgres/indexing.md new file mode 100644 index 0000000000..d0b864a4f8 --- /dev/null +++ b/documentation/documentation/postgres/indexing.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/indexing_jsonb.md b/documentation/documentation/postgres/indexing_jsonb.md new file mode 100644 index 0000000000..a26e28bce3 --- /dev/null +++ b/documentation/documentation/postgres/indexing_jsonb.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/naming.md b/documentation/documentation/postgres/naming.md new file mode 100644 index 0000000000..e15bef40ef --- /dev/null +++ b/documentation/documentation/postgres/naming.md @@ -0,0 +1,53 @@ + + + +When you design your database schema in SQL Server, it's common to name your tables and columns using pascal casing. For example, in SQL Server we may have a table like so: + + | Table Name | Product | + |------------|------------------| + | Columns | Id | + | | Name | + | | Price | + | | IsDeleted | + | | CategoryId | + | | CreatedByUser | + | | ModifiedByUser | + + +PostgreSQL stores all table and columns (that are not in double quotes) in lowercase, so the above would be stored as `product` rather than `Product`, if you run a select with uppercase against Postgres, the query will fail saying the column doesn’t exist. Thus, the Postgres convention for tables and columns, is to name everything lowercase with under scores. The above would become: + + + | Table Name | product | + |------------|------------------| + | Columns | id | + | | name | + | | price | + | | is_deleted | + | | category_id | + | | created_by_user | + | | modified_by_user | + + +While it is possible to use the convention from SQL Server, if you're looking at the table and column information from the database you will find it is stored in lowercase, this often makes it harder to read later. + +For example, if we created a table in Postgres, the same as we would in SQL Server. + + create table if not exists Product ( + Id serial, + Name text, + Price money, + IsDeleted bool, + CategoryId int, + CreatedByUser int, + ModifiedByUser int + ); + +When we query the table definition: + + select * + from INFORMATION_SCHEMA.COLUMNS + where table_name = 'product'; + +You can see from the screen grab that the table and columns are stored lowercase. + +![](/content/images/postgres-table-definition.png) diff --git a/documentation/documentation/postgres/order.txt b/documentation/documentation/postgres/order.txt new file mode 100644 index 0000000000..a21e07aebb --- /dev/null +++ b/documentation/documentation/postgres/order.txt @@ -0,0 +1,11 @@ +naming +escaping +types +casting +casing +slow_queries +indexing +indexing_jsonb +if_statements +dates +backup-restore diff --git a/documentation/documentation/postgres/slow_queries.md b/documentation/documentation/postgres/slow_queries.md new file mode 100644 index 0000000000..06e00bb75c --- /dev/null +++ b/documentation/documentation/postgres/slow_queries.md @@ -0,0 +1,4 @@ + + + +//TODO diff --git a/documentation/documentation/postgres/types.md b/documentation/documentation/postgres/types.md new file mode 100644 index 0000000000..654848b5b2 --- /dev/null +++ b/documentation/documentation/postgres/types.md @@ -0,0 +1,4 @@ + + + +//TODO From 0468c7b8c994c863eeee0725e306e45f4854b486 Mon Sep 17 00:00:00 2001 From: Phillip Haydon Date: Mon, 28 Nov 2016 00:44:52 +0800 Subject: [PATCH 2/4] added doco for types --- .../content/images/postgres-sequence.png | Bin 0 -> 2131 bytes documentation/documentation/postgres/types.md | 89 +++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 documentation/content/images/postgres-sequence.png diff --git a/documentation/content/images/postgres-sequence.png b/documentation/content/images/postgres-sequence.png new file mode 100644 index 0000000000000000000000000000000000000000..549752448b0162baa1f20f162bdcd7b2b923ffd7 GIT binary patch literal 2131 zcmV-Z2(0&sP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D2k1#eK~#8N?VC+& z97hm_^?TO0TuhWeNWh0&Vz4pBAbhY4f}qGg*f;55FuLdvT~-F&)|YHdjBm;>P<~gc zySi(tKWApOJ2R_&ADXzgr~0d=>Ybhu+57RQpR?u7n>SI*8Vd$TK|w*$rKrUyC@3f> zgB55o3JMC!_yt;wf`WoFe%bpD006+)?ERp4`0$~tfkA-70jRSJIl_B{7LN%W4nQ4f zF&F|k9Dq8|VlV`7H~@8^#b5~FZ~*E+i@^}U;Q-Wu7T**0_y0J*`S$L~zWY5fpv3^x zDdot-Q@aly|R8_iKy%mT08_VU%vduLKW9!fffV6^v#iN z)Z+Rc_ntm|%D)M{?fv~{?>^uD{qKKN&X3GO_n&n@#M^Z1`mpOKC2(mtssV;{D-iH>BQexmGpycu=Yeygh*3>8g=;D*bAbulDXRWuRl*I7dQ*drK9WR=a0XA zf2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yq<0E`c`7ytmq2U-jO0OJEK1^|HZfffS*z}P%^@IUKWTGII4X_Wu~002ov JPDHLkV1nS-A@=|P literal 0 HcmV?d00001 diff --git a/documentation/documentation/postgres/types.md b/documentation/documentation/postgres/types.md index 654848b5b2..5d021aaaa9 100644 --- a/documentation/documentation/postgres/types.md +++ b/documentation/documentation/postgres/types.md @@ -1,4 +1,89 @@ - + -//TODO +PostgreSQL offers a lot more range when it comes to data types, and also allow you to easily create your own. However if you find yourself trying to translate your SQL Server knowledge to Postgres, you may run into some gotcha’s. + +The following table is a list of common data types from SQL Server and the Postgres equivalent. + + + | SQL Server | PostgreSQL | Notes | + |--------------|---------------|-----------------------------------------------------------------------------------------------------------------------------| + | int | int / integer | | + | int IDENTITY | serial | serial is the equivalent of SQL Servers auto generated number and is stored as an integer. Your C# code will still use int. | + | bit | boolean | Postgres has an actual boolean data type. | + +### serial + +Serial is interesting, because it can actually be used on multiple tables at the same time. + +If you define a table like so: + + create table if not exists serial_one ( + id serial, + name text + ); + + insert into serial_one (name) values ('phill'); + insert into serial_one (name) values ('james'); + + select * from serial_one; + +You will get a result with two values. And the `id` column will be incremented as you would expect. + +You can then query for sequences in Postgres by selecting from the `information_schema.sequences` table. + + select * from information_schema.sequences; + +There will be an entry for the sequence created by the table definition above. + +![](/content/images/postgres-sequence.png) + +The naming is: table_column_seq + +In this instance, the table is `serial_one`, the column is `id`, and the suffix is `seq` + +If you look at the table schema you can see the column is created with a default value of `nextval('serial_one_id_seq'::regclass)` + +So you can create your own table using the same sequence name, by defining the column as `int` with a default value. + + create table if not exists serial_two ( + id int not null default(nextval('serial_one_id_seq')), + name text + ); + + insert into serial_two (name) values ('demi'); + insert into serial_two (name) values ('nigel'); + + select * from serial_two; + +If you want to name the sequence yourself you can create the sequence first like: + + create sequence my_own_named_sequence + +Look at the Postgres Sequence docs for more info. + +https://www.postgresql.org/docs/9.5/static/sql-createsequence.html + +### boolean + +The boolean type is great to work with, you can use it in many ways. For example, assuming we had a user table with a boolean column `active` + +We could use various different queries like so: + + select * from users where active; + select * from users where not active; + +We can also use true/false instead of implicit checks. + + select * from users where active is true; + select * from users where active is false; + + -- or + + select * from users where active = true; + select * from users where active = false;; + + -- or + + select * from users where active is not true; + select * from users where active is not false; From 8b771f91b237cf23d40cd1ab1dd7b75219e6e307 Mon Sep 17 00:00:00 2001 From: Phillip Haydon Date: Sun, 4 Dec 2016 16:24:02 +0800 Subject: [PATCH 3/4] added date stuff --- documentation/documentation/postgres/dates.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/documentation/documentation/postgres/dates.md b/documentation/documentation/postgres/dates.md index b15e6df75f..3e45d7350d 100644 --- a/documentation/documentation/postgres/dates.md +++ b/documentation/documentation/postgres/dates.md @@ -1,4 +1,14 @@ - + -//TODO +SQL Server has functions for modifying a date, such as `DATEADD(…)` + +Postgres has an interval type which allows you to add/remove in a much more semantic manner. + +For example, in SQL Server to add 5 days to the current date we might do: + + SELECT DATEADD(day, 5, getutcdate()) + +In Postgres we would achieve the same like so: + + SELECT now() + ‘5 days’::interval; From 0d655e63508a421093917c8a3f9e647ed599f8f7 Mon Sep 17 00:00:00 2001 From: Phillip Haydon Date: Sun, 4 Dec 2016 16:43:21 +0800 Subject: [PATCH 4/4] I am groot --- .../documentation/postgres/if_statements.md | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/documentation/documentation/postgres/if_statements.md b/documentation/documentation/postgres/if_statements.md index 0fdc006709..6382800b39 100644 --- a/documentation/documentation/postgres/if_statements.md +++ b/documentation/documentation/postgres/if_statements.md @@ -1,4 +1,66 @@ - + -//TODO +When ever you write SQL in SQL Server Management Studio, you're actually writing TSQL PostgreSQL supports SQL by default only and has key words for breaking out into other supported sub languages, the most common being PL/pgSQL (PostgresSQL) and plv8 (JavaScript run on the v8 engine) + +If statement's are not actually part of the SQL Spec *(The supported version of PostgreSQL 9.5 onwards supports most of the 2011 spec)*, and so in order to write if statements you need to use an anonymous code block with the language PL/pgSQL. + +For example: + + DO %% + BEGIN + + // your sql here… + + END + %%; + +Everything inside the begin/end will use PL/pgSQL by default as an assumed language. + +You can be explicit about the language by defining the language at the end of the block like so: + +DO %% +BEGIN + + // your sql here… + +END +%% LANGUAGE plpgsql; + +Note: `DO` is not part of the SQL Standard and is unique to PostgreSQL. + +An actual example, lets say we wanted to create a new schema called `inventory` if it didn’t exist already. + + DO $$ + BEGIN + IF NOT EXISTS( + SELECT schema_name + FROM information_schema.schemata + WHERE schema_name = 'inventory' + ) + THEN + EXECUTE 'CREATE SCHEMA inventory'; + END IF; + END + $$; + +It is worth noting that Postgres offers helpful syntax for scenarios you would usually use IF statements in SQL Server for. + +For example in SQL Server we might do the following: + + DROP PROCEDURE CreateProduct… + CREATE PROCEDURE CreateProduct… + +or + + IF NOT EXISTS (….) + BEGIN + CREATE TABLE Product (… + +These would be written in Postgres like so: + + CREATE OR REPLACE FUNCTION create_product + + CREATE TABLE IF NOT EXISTS Product (…. + +These don't require you to drop down to PL/pgSQ in order to execute.