-- admin can view all rows and fields
-postgres=> set role admin;
+postgres=> set role admin;
SET
-postgres=> table passwd;
+postgres=> table passwd;
username | pwhash | uid | gid | real_name | home_phone | extra_info | home_dir | shell
----------+--------+-----+-----+-----------+--------------+------------+-------------+-----------
admin | xxx | 0 | 0 | Admin | 111-222-3333 | | /root | /bin/dash
(3 rows)
-- Test what Alice is able to do
-postgres=> set role alice;
+postgres=> set role alice;
SET
-postgres=> table passwd;
+postgres=> table passwd;
ERROR: permission denied for relation passwd
-postgres=> select username,real_name,home_phone,extra_info,home_dir,shell from passwd;
+postgres=> select username,real_name,home_phone,extra_info,home_dir,shell from passwd;
username | real_name | home_phone | extra_info | home_dir | shell
----------+-----------+--------------+------------+-------------+-----------
admin | Admin | 111-222-3333 | | /root | /bin/dash
alice | Alice | 098-765-4321 | | /home/alice | /bin/zsh
(3 rows)
-postgres=> update passwd set username = 'joe';
+postgres=> update passwd set username = 'joe';
ERROR: permission denied for relation passwd
-- Allowed to change her own real_name, but no others
-postgres=> update passwd set real_name = 'Alice Doe';
+postgres=> update passwd set real_name = 'Alice Doe';
UPDATE 1
-postgres=> update passwd set real_name = 'John Doe' where username = 'admin';
+postgres=> update passwd set real_name = 'John Doe' where username = 'admin';
UPDATE 0
-postgres=> update passwd set shell = '/bin/xx';
+postgres=> update passwd set shell = '/bin/xx';
ERROR: new row violates WITH CHECK OPTION for "passwd"
-postgres=> delete from passwd;
+postgres=> delete from passwd;
ERROR: permission denied for relation passwd
-postgres=> insert into passwd (username) values ('xxx');
+postgres=> insert into passwd (username) values ('xxx');
ERROR: permission denied for relation passwd
-- Alice can change her own password
-postgres=> update passwd set pwhash = 'abc';
+postgres=> update passwd set pwhash = 'abc';
UPDATE 1
ii integer[]
);
-testdb=> SELECT * FROM t3;
+testdb=> SELECT * FROM t3;
ii
-------------
{1,2,3,4,5}
Create interval from years, months, weeks, days, hours, minutes and
seconds fields
- make_interval(days => 10)
+ make_interval(days => 10)
10 days
hstore
delete pair with matching key
delete('a=>1,b=>2','b')
- "a"=>"1"
+ "a"=>"1"
|
hstore
delete pairs with matching keys
delete('a=>1,b=>2,c=>3',ARRAY['a','b'])
- "c"=>"3"
+ "c"=>"3"
|
hstore
delete pairs matching those in the second argument
delete('a=>1,b=>2','a=>4,b=>2'::hstore)
- "a"=>"1"
+ "a"=>"1"
|
target relation's name (optionally schema-qualified) or OID.
For example:
-test=> SELECT * FROM pgstattuple('pg_catalog.pg_proc');
+test=> SELECT * FROM pgstattuple('pg_catalog.pg_proc');
-[ RECORD 1 ]------+-------
table_len | 458752
tuple_count | 1470
pgstatindex returns a record showing information
about a B-tree index. For example:
-test=> SELECT * FROM pgstatindex('pg_cast_oid_index');
+test=> SELECT * FROM pgstatindex('pg_cast_oid_index');
-[ RECORD 1 ]------+------
version | 2
tree_level | 0
pgstatginindex returns a record showing information
about a GIN index. For example:
-test=> SELECT * FROM pgstatginindex('test_gin_index');
+test=> SELECT * FROM pgstatginindex('test_gin_index');
-[ RECORD 1 ]--+--
version | 1
pending_pages | 0
The argument is the target relation's OID.
For example:
-test=> SELECT * FROM pgstattuple_approx('pg_catalog.pg_proc'::regclass);
+test=> SELECT * FROM pgstattuple_approx('pg_catalog.pg_proc'::regclass);
-[ RECORD 1 ]--------+-------
table_len | 573440
scanned_percent | 2
it's usually better to override the default names with a table alias
list, like this:
-=> SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);
+=> SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);
num | letter
-----+--------
1 | one
CREATE FUNCTION tricky(text, text) RETURNS bool AS $$
BEGIN
- RAISE NOTICE '% => %', $1, $2;
+ RAISE NOTICE '% => %', $1, $2;
RETURN true;
END
$$ LANGUAGE plpgsql COST 0.0000000000000000000001;
when you fetch it? Watch:
-test=> select 6.50 :: float8 as "pH";
+test=> select 6.50 :: float8 as "pH";
pH
---
6.5
Check this out:
-test=> select '6.25 .. 6.50'::seg as "pH";
+test=> select '6.25 .. 6.50'::seg as "pH";
pH
------------
6.25 .. 6.50
boundary if the resulting interval includes a power of ten:
-postgres=> select '10(+-)1'::seg as seg;
+postgres=> select '10(+-)1'::seg as seg;
seg
---------
9.0 .. 11 -- should be: 9 .. 11
In named notation, each argument's name is specified using
- => to separate it from the argument expression.
+ => to separate it from the argument expression.
For example:
-SELECT concat_lower_or_upper(a => 'Hello', b => 'World');
+SELECT concat_lower_or_upper(a => 'Hello', b => 'World');
concat_lower_or_upper
-----------------------
hello world
using named notation is that the arguments may be specified in any
order, for example:
-SELECT concat_lower_or_upper(a => 'Hello', b => 'World', uppercase => true);
+SELECT concat_lower_or_upper(a => 'Hello', b => 'World', uppercase => true);
concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
-SELECT concat_lower_or_upper(a => 'Hello', uppercase => true, b => 'World');
+SELECT concat_lower_or_upper(a => 'Hello', uppercase => true, b => 'World');
concat_lower_or_upper
-----------------------
HELLO WORLD
already mentioned, named arguments cannot precede positional arguments.
For example:
-SELECT concat_lower_or_upper('Hello', 'World', uppercase => true);
+SELECT concat_lower_or_upper('Hello', 'World', uppercase => true);
concat_lower_or_upper
-----------------------
HELLO WORLD
VARIADIC>. For example, this will work:
-SELECT mleast(VARIADIC arr => ARRAY[10, -1, 5, 4.4]);
+SELECT mleast(VARIADIC arr => ARRAY[10, -1, 5, 4.4]);
but not these:
-SELECT mleast(arr => 10);
-SELECT mleast(arr => ARRAY[10, -1, 5, 4.4]);
+SELECT mleast(arr => 10);
+SELECT mleast(arr => ARRAY[10, -1, 5, 4.4]);