-- multiple backslash commands in one line select1as x, 2as y \gset pref01_ \\ \echo :pref01_x select3as x, 4as y \gset pref01_ \echo :pref01_x \echo :pref01_y select5as x, 6as y \gset pref01_ \\ \g \echo :pref01_x :pref01_y select7as x, 8as y \g \gset pref01_ \echo :pref01_x :pref01_y
-- NULL should unset the variable
\set var2 xyz select1as var1, NULLas var2, 3as var3 \gset
\echo :var1 :var2 :var3
-- \gset requires just one tuple select10as test01, 20as test02 from generate_series(1,3) \gset select10as test01, 20as test02 from generate_series(1,0) \gset
-- \gset returns no tuples select a from generate_series(1, 10) as a where a = 11 \gset
\echo :ROW_COUNT
-- \gset should work in FETCH_COUNT mode too
\set FETCH_COUNT 1
select1as x, 2as y \gset pref01_ \\ \echo :pref01_x select3as x, 4as y \gset pref01_ \echo :pref01_x \echo :pref01_y select10as test01, 20as test02 from generate_series(1,3) \gset select10as test01, 20as test02 from generate_series(1,0) \gset
\unset FETCH_COUNT
-- \gdesc
SELECT NULLAS zero, 1AS one, 2.0AS two, 'three'AS three,
$1AS four,
sin($2) as five, 'foo'::varchar(4) as six, CURRENT_DATEAS now
\gdesc
-- should work with tuple-returning utilities, such as EXECUTE
PREPARE test ASSELECT1AS first, 2AS second;
EXECUTE test \gdesc EXPLAIN EXECUTE test \gdesc
-- should fail cleanly - syntax error SELECT1 + \gdesc
-- subject command should not have executed TABLE bububu; -- fail
-- query buffer should remain unchanged SELECT1AS x, 'Hello', 2AS y, trueAS"dirty\name"
\gdesc
\g
-- all on one line SELECT3AS x, 'Hello', 4AS y, trueAS"dirty\name" \gdesc \g
-- test for server bug #17983 with empty statement in aborted transaction set search_path = default;
begin;
bogus;
;
\gdesc
rollback;
-- \gexec
create temporary table gexec_test(a int, b text, c date, d float); select format('create index on gexec_test(%I)', attname) from pg_attribute where attrelid = 'gexec_test'::regclass and attnum > 0 orderby attnum
\gexec
-- \gexec should work in FETCH_COUNT mode too -- (though the fetch limit applies to the executed queries not the meta query)
\set FETCH_COUNT 1
select'select 1 as ones', 'select x.y, x.y*2 as double from generate_series(1,4) as x(y)' unionall select'drop table gexec_test', NULL unionall select'drop table gexec_test', 'select ''2000-01-01''::date as party_over'
\gexec
\unset FETCH_COUNT
-- \setenv, \getenv
-- ensure MYVAR isn't set
\setenv MYVAR -- in which case, reading it doesn't change the target
\getenv res MYVAR
\echo :res -- now set it
\setenv MYVAR 'environment value'
\getenv res MYVAR
\echo :res
-- show all pset options
\pset
-- test multi-line headers, wrapping, and newline indicators -- in aligned, unaligned, and wrapped formats
prepare q asselect array_to_string(array_agg(repeat('x',2*n)),E'\n') as"ab
c", array_to_string(array_agg(repeat('y',20-2*n)),E'\n') as "a
bc" from generate_series(1,10) as n(n) group by n>1 order by n>1;
\pset linestyle ascii
\pset expanded off
\pset columns 40
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset expanded on
\pset columns 20
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset linestyle old-ascii
\pset expanded off
\pset columns 40
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset expanded on
\pset columns 20
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
deallocate q;
-- test single-line header and data
prepare q asselectrepeat('x',2*n) as"0123456789abcdef", repeat('y',20-2*n) as"0123456789"from generate_series(1,10) as n;
\pset linestyle ascii
\pset expanded off
\pset columns 40
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset expanded on
\pset columns 30
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset expanded on
\pset columns 20
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset linestyle old-ascii
\pset expanded off
\pset columns 40
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset expanded on
\pset border 0
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 1
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
\pset border 2
\pset format unaligned
execute q;
\pset format aligned
execute q;
\pset format wrapped
execute q;
deallocate q;
\pset linestyle ascii
\pset border 1
-- support table for output-format tests (useful to create a footer)
createtable psql_serial_tab (id serial);
-- test header/footer/tuples_only behavior in aligned/unaligned/wrapped cases
\pset format aligned
\pset expanded off
\d psql_serial_tab_id_seq
\pset tuples_only true
\df exp
\dfx exp
\pset tuples_only false
\pset expanded on
\d psql_serial_tab_id_seq
\pset tuples_only true
\df exp
\pset tuples_only false -- empty table is a special case for this format select1wherefalse;
-- check conditional am display
\pset expanded off
CREATESCHEMA tableam_display; CREATE ROLE regress_display_role; ALTERSCHEMA tableam_display OWNER TO regress_display_role; SET search_path TO tableam_display; CREATE ACCESS METHOD heap_psql TYPE TABLE HANDLER heap_tableam_handler; SET ROLE TO regress_display_role; -- Use only relations with a physical size of zero. CREATETABLE tbl_heap_psql(f1 int, f2 char(100)) using heap_psql; CREATETABLE tbl_heap(f1 int, f2 char(100)) using heap; CREATE VIEW view_heap_psql ASSELECT f1 from tbl_heap_psql; CREATE MATERIALIZED VIEW mat_view_heap_psql USING heap_psql ASSELECT f1 from tbl_heap_psql;
\d+ tbl_heap_psql
\d+ tbl_heap
\set HIDE_TABLEAM off
\d+ tbl_heap_psql
\d+ tbl_heap -- AM is displayed for tables, indexes and materialized views.
\d+
\dt+
\dm+ -- But not for views and sequences.
\dv+
\set HIDE_TABLEAM on
\d+ -- \d with 'x' enables expanded mode, but only without a pattern
\d+x tbl_heap
\d+x
RESET ROLE;
RESET search_path; DROPSCHEMA tableam_display CASCADE; DROP ACCESS METHOD heap_psql; DROP ROLE regress_display_role;
-- test numericlocale (as best we can without control of psql's locale)
\pset format aligned
\pset expanded off
\pset numericlocale true
select n, -n as m, n * 111as x, '1e90'::float8as f from generate_series(0,3) n;
-- test a large nested if using a variety of true-equivalents
\iftrue
\if1
\if yes
\ifon
\echo 'all true'
\else
\echo 'should not print #1-1'
\endif
\else
\echo 'should not print #1-2'
\endif
\else
\echo 'should not print #1-3'
\endif
\else
\echo 'should not print #1-4'
\endif
-- test a variety of false-equivalents in an if/elif/else structure
\iffalse
\echo 'should not print #2-1'
\elif 0
\echo 'should not print #2-2'
\elif no
\echo 'should not print #2-3'
\elif off
\echo 'should not print #2-4'
\else
\echo 'all false'
\endif
-- test true-false elif after initial true branch
\iftrue
\echo 'should print #2-5'
\elif true
\echo 'should not print #2-6'
\elif false
\echo 'should not print #2-7'
\else
\echo 'should not print #2-8'
\endif
-- test simple true-then-else
\iftrue
\echo 'first thing true'
\else
\echo 'should not print #3-1'
\endif
-- test simple false-true-else
\iffalse
\echo 'should not print #4-1'
\elif true
\echo 'second thing true'
\else
\echo 'should not print #5-1'
\endif
-- test double-else error
\iftrue
\else
\else
\endif
-- test elif out-of-order
\iffalse
\else
\elif
\endif
-- test if-endif matching in a false branch
\iffalse
\iffalse
\echo 'should not print #7-1'
\else
\echo 'should not print #7-2'
\endif
\echo 'should not print #7-3'
\else
\echo 'should print #7-4'
\endif
-- show that vars and backticks are not expanded when ignoring extra args
\set foo bar
\echo :foo :'foo' :"foo"
\pset fieldsep | `nosuchcommand` :foo :'foo' :"foo"
-- show that vars and backticks are not expanded and commands are ignored -- when in a false if-branch
\set try_to_quit '\\q'
\iffalse
:try_to_quit
\echo `nosuchcommand` :foo :'foo' :"foo"
\pset fieldsep | `nosuchcommand` :foo :'foo' :"foo"
\a SELECT $1 \bind 1 \g
\bind_named stmt1 12 \g
\C arg1
\c arg1 arg2 arg3 arg4
\cd arg1
\close_prepared stmt1
\conninfo
\copy arg1 arg2 arg3 arg4 arg5 arg6
\copyright SELECT1as one, 2, 3 \crosstabview
\dt arg1
\e arg1 arg2
\ef whole_line
\ev whole_line
\echo arg1 arg2 arg3 arg4 arg5
\echo arg1
\encoding arg1
\endpipeline
\errverbose
\f arg1
\flush
\flushrequest
\g arg1
\gx arg1
\gexec
\getresults SELECT1AS one \gset
\h
\?
\html
\i arg1
\ir arg1
\l arg1
\lo arg1 arg2
\lo_list
\o arg1
\p SELECT1 \parse
\password arg1
\prompt arg1 arg2
\pset arg1 arg2
\q
\reset
\restrict test
\s arg1
\sendpipeline
\set arg1 arg2 arg3 arg4 arg5 arg6 arg7
\setenv arg1 arg2
\sf whole_line
\sv whole_line
\startpipeline
\syncpipeline
\t arg1
\T arg1
\timing arg1
\unrestrict not_valid
\unset arg1
\w arg1
\watch arg1 arg2
\x arg1 -- \else here is eaten as part of OT_FILEPIPE argument
\w |/no/such/file \else -- \endif here is eaten as part of whole-line argument
\! whole_line \endif
\z
\else
\echo 'should print #8-1'
\endif
-- :{?...} defined variable test
\set i 1
\if :{?i}
\echo '#9-1 ok, variable i is defined'
\else
\echo 'should not print #9-2'
\endif
\if :{?no_such_variable}
\echo 'should not print #10-1'
\else
\echo '#10-2 ok, variable no_such_variable is not defined'
\endif
SELECT :{?i} AS i_is_defined;
SELECTNOT :{?no_such_var} AS no_such_var_is_not_defined;
-- SHOW_CONTEXT
\set SHOW_CONTEXT never
do $$
begin
raise notice 'foo';
raise exception 'bar';
end $$;
\set SHOW_CONTEXT errors
do $$
begin
raise notice 'foo';
raise exception 'bar';
end $$;
\set SHOW_CONTEXT always
do $$
begin
raise notice 'foo';
raise exception 'bar';
end $$;
-- test printing and clearing the query buffer SELECT1;
\p SELECT2 \r
\p SELECT3 \p UNIONSELECT4 \p UNIONSELECT5 ORDERBY1;
\r
\p
createtable parent_tab (id int) partition by range (id); createindex parent_index on parent_tab (id); createtable child_0_10 partition of parent_tab forvaluesfrom (0) to (10); createtable child_10_20 partition of parent_tab forvaluesfrom (10) to (20); createtable child_20_30 partition of parent_tab forvaluesfrom (20) to (30); insertinto parent_tab values (generate_series(0,29)); createtable child_30_40 partition of parent_tab forvaluesfrom (30) to (40)
partition by range(id); createtable child_30_35 partition of child_30_40 forvaluesfrom (30) to (35); createtable child_35_40 partition of child_30_40 forvaluesfrom (35) to (40); insertinto parent_tab values (generate_series(30,39));
\dPt
\dPi
\dP testpart.*
\dP
\dPtn
\dPin
\dPn
\dPn testpart.*
droptable parent_tab cascade;
dropschema testpart;
set search_path todefault;
set role todefault; drop role regress_partitioning_role;
-- \d on toast table (use pg_statistic's toast table, which has a known name)
\d pg_toast.pg_toast_2619
-- check \df, \do with argument specifications
\df *sqrt
\df *sqrt num*
\df int*pl
\df int*pl int4
\df int*pl * pg_catalog.int8
\df acl* aclitem[]
\df has_database_privilege oid text
\df has_database_privilege oid text -
\dfa bit* small*
\df *._pg_expandarray
\do - pg_catalog.int4
\do && anyarray *
-- check \df+ -- we have to use functions with a predictable owner name, so make a role create role regress_psql_user superuser;
begin; set session authorization regress_psql_user;
create function psql_df_internal (float8)
returns float8
language internal immutable parallel safe strict as'dsin'; create function psql_df_sql (x integer)
returns integer
security definer
begin atomic select x + 1; end; create function psql_df_plpgsql ()
returns void
language plpgsql as $$ begin return; end; $$;
comment on function psql_df_plpgsql () is'some comment';
\df+ psql_df_*
rollback; drop role regress_psql_user;
-- ECHO errors
\set ECHO errors SELECT * FROM notexists;
\set ECHO all
-- -- combined queries -- CREATE FUNCTION warn(msg TEXT) RETURNS BOOLEAN LANGUAGE plpgsql AS $$
BEGIN RAISE NOTICE 'warn %', msg ; RETURNTRUE ; END
$$;
-- show both SELECT1AS one \; SELECT warn('1.5') \; SELECT2AS two ; -- \gset applies to last query only SELECT3AS three \; SELECT warn('3.5') \; SELECT4AS four \gset
\echo :three :four -- syntax error stops all processing SELECT5 \; SELECT6 + \; SELECT warn('6.5') \; SELECT7 ; -- with aborted transaction, stop on first error
BEGIN \; SELECT8AS eight \; SELECT9/0AS nine \; ROLLBACK \; SELECT10AS ten ; -- close previously aborted transaction
ROLLBACK;
-- miscellaneous SQL commands -- (non SELECT output is sent to stderr, thus is not shown in expected results) SELECT'ok'AS"begin" \; CREATETABLE psql_comics(s TEXT) \; INSERTINTO psql_comics VALUES ('Calvin'), ('hobbes') \;
COPY psql_comics FROM STDIN \; UPDATE psql_comics SET s = 'Hobbes'WHERE s = 'hobbes' \; DELETEFROM psql_comics WHERE s = 'Moe' \;
COPY psql_comics TO STDOUT \;
TRUNCATE psql_comics \; DROPTABLE psql_comics \; SELECT'ok'AS"done" ;
Moe
Susie
\.
\set SHOW_ALL_RESULTS off SELECT1AS one \; SELECT warn('1.5') \; SELECT2AS two ;
\set SHOW_ALL_RESULTS on DROP FUNCTION warn(TEXT);
CREATE TEMPORARY TABLE reload_output(
lineno intNOTNULL GENERATED ALWAYS AS IDENTITY,
line text
);
SELECT1AS a \g :g_out_file
COPY reload_output(line) FROM :'g_out_file'; SELECT2AS b\; SELECT3AS c\; SELECT4AS d \g :g_out_file
COPY reload_output(line) FROM :'g_out_file';
COPY (SELECT'foo') TO STDOUT \; COPY (SELECT'bar') TO STDOUT \g :g_out_file
COPY reload_output(line) FROM :'g_out_file';
SELECT line FROM reload_output ORDERBY lineno;
TRUNCATE TABLE reload_output;
-- -- \o with file --
\set o_out_file :abs_builddir '/results/psql-output2'
-- COPY TO file -- The data goes to :g_out_file and the status to :o_out_file
\set QUIET false
COPY (SELECT unique1 FROM onek ORDERBY unique1 LIMIT10) TO :'g_out_file'; -- DML command status UPDATE onek SET unique1 = unique1 WHEREfalse;
\set QUIET true
\o
-- Check the contents of the files generated.
COPY reload_output(line) FROM :'g_out_file'; SELECT line FROM reload_output ORDERBY lineno;
TRUNCATE TABLE reload_output;
COPY reload_output(line) FROM :'o_out_file'; SELECT line FROM reload_output ORDERBY lineno;
TRUNCATE TABLE reload_output;
-- Multiple COPY TO STDOUT with output file
\o :o_out_file -- The data goes to :o_out_file with no status generated.
COPY (SELECT'foo1') TO STDOUT \; COPY (SELECT'bar1') TO STDOUT; -- Combination of \o and \g file with multiple COPY queries.
COPY (SELECT'foo2') TO STDOUT \; COPY (SELECT'bar2') TO STDOUT \g :g_out_file
\o
-- Check the contents of the files generated.
COPY reload_output(line) FROM :'g_out_file'; SELECT line FROM reload_output ORDERBY lineno;
TRUNCATE TABLE reload_output;
COPY reload_output(line) FROM :'o_out_file'; SELECT line FROM reload_output ORDERBY lineno;
DROPTABLE reload_output;
-- -- AUTOCOMMIT and combined queries --
\set AUTOCOMMIT off
\echo '# AUTOCOMMIT:' :AUTOCOMMIT -- BEGIN is now implicit
-- -- test ON_ERROR_ROLLBACK and combined queries -- CREATE FUNCTION psql_error(msg TEXT) RETURNS BOOLEAN AS $$
BEGIN
RAISE EXCEPTION 'error %', msg;
END;
$$ LANGUAGE plpgsql;
BEGIN; INSERTINTO bla VALUES ('Susie'); -- succeeds -- now with combined queries INSERTINTO bla VALUES ('Rosalyn') \; -- will rollback SELECT'before error'ASshow \; -- will show nevertheless! SELECT psql_error('boum!') \; -- failure SELECT'after error'AS noshow; -- hidden by preceding error INSERTINTO bla(s) VALUES ('Moe') \; -- will rollback SELECT psql_error('bam!'); INSERTINTO bla VALUES ('Miss Wormwood'); -- succeeds COMMIT; SELECT * FROM bla ORDERBY1;
-- some with autocommit off
\set AUTOCOMMIT off
\echo '# AUTOCOMMIT:' :AUTOCOMMIT
-- implicit BEGIN INSERTINTO bla VALUES ('Dad'); -- succeeds SELECT psql_error('bad!'); -- implicit partial rollback
INSERTINTO bla VALUES ('Mum') \; -- will rollback SELECT COUNT(*) AS"#mum" FROM bla WHERE s = 'Mum' \; -- but be counted here SELECT psql_error('bad!'); -- implicit partial rollback COMMIT;
SELECT COUNT(*) AS"#mum" FROM bla WHERE s = 'Mum' \; -- no mum here SELECT * FROM bla ORDERBY1; COMMIT;
-- reset all
\set AUTOCOMMIT on
\set ON_ERROR_ROLLBACK off
\echo '# final ON_ERROR_ROLLBACK:' :ON_ERROR_ROLLBACK DROPTABLE bla; DROP FUNCTION psql_error;
-- check \drg and \du CREATE ROLE regress_du_role0; CREATE ROLE regress_du_role1; CREATE ROLE regress_du_role2; CREATE ROLE regress_du_admin;
GRANT regress_du_role0 TO regress_du_admin WITH ADMIN TRUE; GRANT regress_du_role1 TO regress_du_admin WITH ADMIN TRUE; GRANT regress_du_role2 TO regress_du_admin WITH ADMIN TRUE;
GRANT regress_du_role0 TO regress_du_role1 WITH ADMIN TRUE, INHERIT TRUE, SETTRUE GRANTED BY regress_du_admin; GRANT regress_du_role0 TO regress_du_role2 WITH ADMIN TRUE, INHERIT FALSE, SETFALSE GRANTED BY regress_du_admin; GRANT regress_du_role1 TO regress_du_role2 WITH ADMIN TRUE , INHERIT FALSE, SETTRUE GRANTED BY regress_du_admin; GRANT regress_du_role0 TO regress_du_role1 WITH ADMIN FALSE, INHERIT TRUE, SETFALSE GRANTED BY regress_du_role1; GRANT regress_du_role0 TO regress_du_role2 WITH ADMIN FALSE, INHERIT TRUE , SETTRUE GRANTED BY regress_du_role1; GRANT regress_du_role0 TO regress_du_role1 WITH ADMIN FALSE, INHERIT FALSE, SETTRUE GRANTED BY regress_du_role2; GRANT regress_du_role0 TO regress_du_role2 WITH ADMIN FALSE, INHERIT FALSE, SETFALSE GRANTED BY regress_du_role2;
\drg regress_du_role*
\du regress_du_role*
DROP ROLE regress_du_role0; DROP ROLE regress_du_role1; DROP ROLE regress_du_role2; DROP ROLE regress_du_admin;
-- Test display of empty privileges.
BEGIN; -- Create an owner for tested objects because output contains owner name. CREATE ROLE regress_zeropriv_owner; SET LOCAL ROLE regress_zeropriv_owner;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.