Skip to content

Chore/postgres 17 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions postgresql-17/json_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE IF NOT EXISTS books (
id integer NOT NULL,
name varchar NOT NULL,
data jsonb
);

INSERT INTO books (id, name, data)
VALUES (
1,
'High Performance PostgreSQL for Rails',
jsonb_build_object(
'publisher', 'Pragmatic Bookshelf; 1st edition (July 23, 2024)',
'isbn', '979-8888650387',
'author','Andrew Atkinson'));

SELECT
isbn,
publisher,
author
FROM
books,
JSON_TABLE (data, '$' COLUMNS (
publisher text PATH '$.publisher',
isbn text PATH '$.isbn',
author text PATH '$.author'
)) AS jt;

22 changes: 22 additions & 0 deletions postgresql-17/merge.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- MERGE in Postgres 15

create table people (id int, name text);
create table employees (id int, name text);

insert into people (id, name) VALUES (1, 'Andy');

MERGE INTO employees e
USING people p
ON e.id = p.id
WHEN MATCHED THEN
UPDATE SET name = p.name
WHEN NOT MATCHED THEN
INSERT (id, name)
VALUES (p.id, p.name)
;
-- RETURNING *;

-- id | name | id | name
-- ----+------+----+------
-- 1 | Andy | 1 | Andy
-- (1 row)
5 changes: 5 additions & 0 deletions postgresql-17/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
docker pull postgres:17

docker run --name my-postgres-container -e POSTGRES_PASSWORD=mysecretpassword -d postgres:17

docker exec -it my-postgres-container psql -U postgres
34 changes: 34 additions & 0 deletions postgresql-17/updatable_views.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
create table employees (id int, name text, is_admin boolean);

insert into employees (id, name, is_admin) VALUES (1, 'Andy', true);
insert into employees (id, name, is_admin) VALUES (2, 'Jane', false);
insert into employees (id, name, is_admin) VALUES (3, 'Jared', false);

CREATE VIEW non_admins AS
SELECT * FROM employees
WHERE is_admin = false;

SELECT * FROM non_admins;

-- Updatable automatically by INSERT, UPDATE, DELETE

-- With 17, added MERGE support
-- Can be updated with trigger-updatable

CREATE OR REPLACE FUNCTION update_employee()
RETURNS TRIGGER AS $$
BEGIN
UPDATE employees
SET is_admin = NEW.is_admin
WHERE id = OLD.id;

RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigger_update_non_admins
INSTEAD OF UPDATE ON non_admins
FOR EACH ROW
EXECUTE FUNCTION update_employee();

UPDATE non_admins SET is_admin = true where id = 2 RETURNING *;