Skip to content

Instantly share code, notes, and snippets.

@garethflowers
garethflowers / array_intersect.sql
Last active December 10, 2016 08:43
Returns an array of elements which exist in both argument arrays.
CREATE OR REPLACE FUNCTION array_intersect(anyarray, anyarray)
RETURNS anyarray
LANGUAGE sql IMMUTABLE STRICT
AS $$
SELECT ARRAY(
SELECT $1[i]
FROM GENERATE_SERIES( ARRAY_LOWER( $1, 1 ), ARRAY_UPPER( $1, 1 ) ) i
WHERE ARRAY[$1[i]] && $2
);
@garethflowers
garethflowers / unnest.sql
Last active May 3, 2021 22:07
Expand an array to a set of rows.Mimics the UNNEST() function available in PostgreSQL 8.4+.
CREATE OR REPLACE FUNCTION unnest(
ANYARRAY
)
RETURNS SETOF ANYELEMENT
LANGUAGE sql
IMMUTABLE
STRICT
AS $$
SELECT $1[i]
@garethflowers
garethflowers / is_numeric.sql
Last active May 3, 2021 22:05
Checks whether a value is a valid Numeric
CREATE OR REPLACE FUNCTION is_numeric(
ANYELEMENT
)
RETURNS BOOLEAN
LANGUAGE sql
IMMUTABLE
STRICT
AS $$
SELECT $1::TEXT ~ '^[-+]?[0-9]*[.]?[0-9]+$';
Public Shared Function pathAsUnc(ByVal fileName As String) As String
Dim file As New System.IO.FileInfo(filename.ToLowerInvariant)
If Not file.Exists Then
Return String.Empty
End If
Dim oldDrive As String = file.Directory.Root.FullName
Dim uncPath As Object = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Network\" & oldDrive.Remove(1), "RemotePath", Nothing)
Dim uri As Uri
@garethflowers
garethflowers / is_int.sql
Created April 9, 2013 09:55
Checks whether a value is a valid Integer
CREATE OR REPLACE FUNCTION is_int(anyelement)
RETURNS boolean AS
$$
SELECT $1::TEXT ~ '^[-+]?[0-9]+$';
$$
LANGUAGE sql IMMUTABLE STRICT;
@garethflowers
garethflowers / ucWords.js
Created April 9, 2013 16:54
Uppercase the first character of each word in a string
if (typeof String.prototype.ucWords !== 'function') {
String.prototype.ucWords = function() {
return this.replace(/^([a-z])|\s+([a-z])/g, function($1) {
return $1.toUpperCase();
});
};
}
@garethflowers
garethflowers / startsWith.js
Created April 9, 2013 16:55
Checks with the string instance starts with a specified value
if (typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function(str) {
return this.slice(0, str.length) === str;
};
}
@garethflowers
garethflowers / array_length.sql
Last active May 3, 2021 22:03
Polyfill. Returns the length of the requested array dimension.
CREATE OR REPLACE FUNCTION array_length(
ANYARRAY,
INT
)
RETURNS SETOF INT
LANGUAGE sql
IMMUTABLE
STRICT
AS $$
@garethflowers
garethflowers / is_email.sql
Last active May 3, 2021 21:41
Validates if a value is an email string.
CREATE OR REPLACE FUNCTION is_email(
anyelement
)
RETURNS boolean
LANGUAGE sql
IMMUTABLE
STRICT
AS $$
SELECT $1::TEXT ~ '^\S+@\S+$';
@garethflowers
garethflowers / postgres-performance-analysis.sql
Last active May 3, 2021 21:37
Useful queries to analyse statistics created by PostgreSQL.
/*
Tables using Sequential Scans
Identifies tables that are not using indexes very often
*/
SELECT
relname AS "Table Name",
n_live_tup "Row Count",
100 * idx_scan / (seq_scan + idx_scan) "% Times Used"
FROM pg_stat_user_tables
WHERE seq_scan + idx_scan > 0