Skip to content

Instantly share code, notes, and snippets.

View lesovsky's full-sized avatar
😉
Don't stop

Lesovsky Alexey lesovsky

😉
Don't stop
  • Postgres Pro
  • Yekaterinburg, Russian Federation
View GitHub Profile
@lesovsky
lesovsky / ameba.c
Last active November 15, 2018 11:20
ameba
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
char * ameba(char * s1, char * s2);
int main(void) {
time_t rawtime;
@lesovsky
lesovsky / gocui-pager.go
Last active March 6, 2018 07:34
Gocui breaks after pager program.
package main
import (
"fmt"
"log"
"os/exec"
"os"
"strings"
"github.com/jroimartin/gocui"
"time"
@lesovsky
lesovsky / fn_upgrade_partitioning.sql
Created November 13, 2017 13:04
Upgrading old partitioning with ALTER TABLEs and PLPGSQL.
-- Upgrading old partitioning with ALTER TABLEs and PLPGSQL.
-- IN: _orig_table - master table which should be upgraded
-- IN: _partkey - column which used as partition key
-- IN: _seq_col - sequence column
CREATE OR REPLACE FUNCTION fn_upgrade_partitioning(_orig_table text, _partkey text, _seq_col text) RETURNS void AS
$function$
DECLARE
_new_table text = _orig_table ||'_new'; -- parent relation's name
_child_table text; -- child relation's name
_v_from timestamp without time zone;
@lesovsky
lesovsky / gist:8685d95cafe72d2ea9c1092853ccae21
Last active November 10, 2017 07:41
fn_upgrade_partitioning()
-- Function used for upgrading old (inheritance) partitioning to new (declarative).
-- Partitions have names like partition_XX_YY.
create or replace function fn_upgrade_partitioning(_oname text, _partkey text) returns void as
$$
declare
_nname text = _oname ||'_new'; -- name of the new parent relation
_cname text; -- name of the child relation
_min_border bigint;
_max_border bigint;
begin
@lesovsky
lesovsky / postrgesql-autovacuum-queue-detailed.sql
Created April 10, 2017 08:39
postrgesql-autovacuum-queue-detailed
WITH table_opts AS (
SELECT
c.oid, c.relname, c.relfrozenxid, c.relminmxid, n.nspname, array_to_string(c.reloptions, '') AS relopts
FROM pg_class c
INNER JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE c.relkind IN ('r', 't') AND n.nspname NOT IN ('pg_catalog', 'information_schema') AND n.nspname !~ '^pg_temp'
),
vacuum_settings AS (
SELECT
oid, relname, nspname, relfrozenxid, relminmxid,
@lesovsky
lesovsky / bgwr-ckpt-report.sql
Created March 27, 2017 13:45
Report query for PostgreSQL' bgwriter/checkpointer.
SELECT
now()-pg_postmaster_start_time() "Uptime", now()-stats_reset "Since stats reset",
round(100.0*checkpoints_req/total_checkpoints,1) "Forced checkpoint ratio (%)",
round(np.min_since_reset/total_checkpoints,2) "Minutes between checkpoints",
round(checkpoint_write_time::numeric/(total_checkpoints*1000),2) "Average write time per checkpoint (s)",
round(checkpoint_sync_time::numeric/(total_checkpoints*1000),2) "Average sync time per checkpoint (s)",
round(total_buffers/np.mp,1) "Total MB written",
round(buffers_checkpoint/(np.mp*total_checkpoints),2) "MB per checkpoint",
round(buffers_checkpoint/(np.mp*np.min_since_reset*60),2) "Checkpoint MBps",
round(buffers_clean/(np.mp*np.min_since_reset*60),2) "Bgwriter MBps",
@lesovsky
lesovsky / pointer-to-array-of-strings.c
Created January 7, 2017 19:42
Using pointer to strings array.
/* This example used in the following scenario:
* 1. there are several sets of files.
* 2. we need to choose specific set.
* 3. handle files from this set in the loop.
*/
#include<stdio.h>
int main()
{
@lesovsky
lesovsky / sys-proc-functions.sql
Last active December 26, 2016 10:43
plperl functions whic gathers stats from /proc
CREATE SCHEMA IF NOT EXISTS pgcenter;
CREATE OR REPLACE FUNCTION pgcenter.get_netdev_link_settings(INOUT iface VARCHAR, OUT speed INT, OUT duplex INT) AS $$
use Linux::Ethtool::Settings;
if (my $settings = Linux::Ethtool::Settings->new($_[0])) {
my $if_speed = $settings->speed();
my $if_duplex = $settings->duplex() ? 1 : 0;
return {iface => $_[0], speed => $if_speed, duplex => $if_duplex};
} else {
return {iface => $_[0], speed => 0, duplex => -1};
@lesovsky
lesovsky / terminate.cron
Created November 6, 2016 11:01
Terminate unwanted activity with cron.
# Terminate long transactions
*/10 * * * * psql -d postgres -tx -c "SELECT pg_terminate_backend(pid),now(),now()-xact_start as duration,* from pg_stat_activity where (now() - pg_stat_activity.xact_start) > '1 hour'::interval and usename NOT IN ('postgres', 'backuper') and state<>'idle'" | grep -vE '^(|\(No rows\))$'
* * * * * psql -d postgres -tx -c "SELECT pg_terminate_backend(pid),now(),now()-xact_start as duration,* from pg_stat_activity where (now() - pg_stat_activity.xact_start) > '1 min'::interval and usename NOT IN ('postgres', 'backuper', 'cron_user') and state<>'idle'" | grep -vE '^(|\(No rows\))$'
@lesovsky
lesovsky / simple-ncurses.c
Created August 18, 2016 14:20
Simple ncurses program with two windows.
#include <stdio.h>
#include <ncurses.h>
#include <limits.h> /* INT_MAX */
#include <unistd.h> /* sleep */
int main (void)
{
int i = 0;
WINDOW * w1, * w2;
initscr();