Skip to content

Instantly share code, notes, and snippets.

@cabecada
cabecada / gist:f188aa5d2d406861c4ffab6446c30945
Last active March 15, 2024 07:22
citus backup using pgbackrest and pitr restore using citus_create_restore_point
using citus_create_restore_point() for pitr
postgres@pg:~/citusdb/demo$ cat setup.sh
#!/bin/bash
export PATH=/opt/15/usr/local/bin:$PATH
port=5432
for i in db1 db2 db3
@cabecada
cabecada / gist:8024d98024559e9fc97ccfcb5324c09f
Last active March 12, 2024 19:20
corruption demo for blogs.
postgres@pg:~/udemy/16$ psql
psql (16.1 (Ubuntu 16.1-1.pgdg22.04+1))
Type "help" for help.
postgres=# drop table t;
DROP TABLE
postgres=# create table t(col1 text, col2 text);
CREATE TABLE
postgres=# insert into t select x::text, x::text from generate_series(1, 10) x;
INSERT 0 10
https://manishrjain.com/zfs-raid-ubuntu
@cabecada
cabecada / gist:7b7d2c306c41f35a9cfa96a0cafd6256
Created February 12, 2024 10:52
citus update view on distributed table
-- Create Employees table
CREATE TABLE Employees (
EmployeeID SERIAL PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Department VARCHAR(50),
Salary NUMERIC(10, 2)
);
CREATE TABLE Employees2 (
@cabecada
cabecada / gist:7d8d2f5a46802ab5b3fa4bd2f4a0e787
Last active January 30, 2024 20:53
postgres failover, convert existing primary to replica controldata REDO
postgres@pg:~/udemy/15/project$ initdb -D db1 >/dev/null 2>/dev/null
postgres@pg:~/udemy/15/project$ cp postgresql.auto.conf_db1 db1/postgresql.auto.conf
postgres@pg:~/udemy/15/project$ vim db1/postgresql.auto.conf
postgres@pg:~/udemy/15/project$ pg_ctl -D db1 -l db1.log start
waiting for server to start.... done
server started
postgres@pg:~/udemy/15/project$ psql -c 'create table t(col1 int); insert into t select generate_series(1, 10000) x;'
CREATE TABLE
INSERT 0 10000
postgres@pg:~/udemy/15/project$ psql -c 'checkpoint'
@cabecada
cabecada / gist:dd47f1549c5aac46ab20667c44139d23
Created January 3, 2024 14:04
npgsql example postgres
using System;
using System.Collections;
using Npgsql;
namespace Program
{
class Program
{
static async Task Main(string[] args) // without async task main, cannot use await
@cabecada
cabecada / parsel.sql
Created December 23, 2023 16:05 — forked from molind/parsel.sql
Parallel select function for PostgreSQL.
--
-- Befor using it you should enable dblink extension in database and allow user to run dblink_connect_u
-- You may need to change 'dbname=osm' to your db connection options in line 34.
-- CREATE EXTENSION dblink;
-- GRANT EXECUTE ON FUNCTION dblink_connect_u(text) TO user;
-- GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO user;
--
DROP FUNCTION IF EXISTS public.g_parsel(query text, table_to_chunk text, num_chunks integer);
CREATE OR REPLACE FUNCTION public.g_parsel(query text, table_to_chunk text, num_chunks integer default 2)
@cabecada
cabecada / gist:2492a2f8f8bc98330f63c30a46a1b363
Last active December 3, 2023 14:59
stored procedure exception handling error
https://itecnote.com/tecnote/postgresql-can-a-postgres-commit-exist-in-procedure-that-has-an-exception-block/
https://github.com/postgres/postgres/blob/REL_14_STABLE/src/pl/plpgsql/src/expected/plpgsql_transaction.out
There are parts of the procedure where I might want to commit my work so-far so that it won't be rolled back if an exceptions ensues.
I want to have an exception handling block at the end of the procedure where I catch the exception
and insert the information from the exception into a logging table.
@cabecada
cabecada / gist:5fedcbe35322d56334718fd8b82f90b0
Created November 26, 2023 19:18
consistent hashing using fnv and murmur3
consistent hashing using fnv and murmur3
https://github.com/flier/pyfasthash
https://github.com/amutu/fnvhash
@cabecada
cabecada / gist:94fc1e1354dba8920e8bc5a96a7f6b10
Last active November 20, 2023 10:10
timezone lateral current time in all timezones
citusdb=# select * from pg_timezone_names limit 1;
name | abbrev | utc_offset | is_dst
------------+--------+------------+--------
Etc/GMT-12 | +12 | 12:00:00 | f
(1 row)
citusdb=# select current_timestamp at time zone 'UTC';
timezone
---------------------------
2023-11-20 09:59:05.33791