├── .gitignore ├── ChangeLog ├── Dockerfile ├── LICENSE ├── Makefile.PL ├── README ├── cgi.Dockerfile ├── doc ├── pg_format.conf.sample └── pg_format.pod ├── icon_pgformatter.ico ├── lib └── pgFormatter │ ├── Beautify.pm │ ├── CGI.pm │ └── CLI.pm ├── logo_pgformatter.png ├── pg_format └── t ├── 01_lint.t ├── 02_regress.t ├── pg-test-files ├── expected │ ├── advisory_lock.sql │ ├── aggregates.sql │ ├── alter_generic.sql │ ├── alter_operator.sql │ ├── alter_table.sql │ ├── amutils.sql │ ├── arrays.sql │ ├── async.sql │ ├── bit.sql │ ├── bitmapops.sql │ ├── boolean.sql │ ├── box.sql │ ├── brin.sql │ ├── btree_index.sql │ ├── case.sql │ ├── char.sql │ ├── circle.sql │ ├── cluster.sql │ ├── collate.icu.utf8.sql │ ├── collate.linux.utf8.sql │ ├── collate.sql │ ├── combocid.sql │ ├── comments.sql │ ├── conversion.sql │ ├── create_aggregate.sql │ ├── create_am.sql │ ├── create_cast.sql │ ├── create_function_3.sql │ ├── create_index.sql │ ├── create_index_spgist.sql │ ├── create_misc.sql │ ├── create_operator.sql │ ├── create_procedure.sql │ ├── create_table.sql │ ├── create_table_like.sql │ ├── create_type.sql │ ├── create_view.sql │ ├── date.sql │ ├── dbsize.sql │ ├── delete.sql │ ├── dependency.sql │ ├── domain.sql │ ├── drop_if_exists.sql │ ├── drop_operator.sql │ ├── enum.sql │ ├── equivclass.sql │ ├── errors.sql │ ├── event_trigger.sql │ ├── expressions.sql │ ├── fast_default.sql │ ├── float4.sql │ ├── float8.sql │ ├── foreign_data.sql │ ├── foreign_key.sql │ ├── fsm.sql │ ├── functional_deps.sql │ ├── generated.sql │ ├── geometry.sql │ ├── gin.sql │ ├── gist.sql │ ├── groupingsets.sql │ ├── guc.sql │ ├── hash_func.sql │ ├── hash_index.sql │ ├── hash_part.sql │ ├── horology.sql │ ├── hs_primary_extremes.sql │ ├── hs_primary_setup.sql │ ├── hs_standby_allowed.sql │ ├── hs_standby_check.sql │ ├── hs_standby_disallowed.sql │ ├── hs_standby_functions.sql │ ├── identity.sql │ ├── index_including.sql │ ├── index_including_gist.sql │ ├── indexing.sql │ ├── indirect_toast.sql │ ├── inet.sql │ ├── inherit.sql │ ├── init_privs.sql │ ├── insert.sql │ ├── insert_conflict.sql │ ├── int2.sql │ ├── int4.sql │ ├── int8.sql │ ├── interval.sql │ ├── join.sql │ ├── join_hash.sql │ ├── json.sql │ ├── json_encoding.sql │ ├── jsonb.sql │ ├── jsonb_jsonpath.sql │ ├── jsonpath.sql │ ├── jsonpath_encoding.sql │ ├── limit.sql │ ├── line.sql │ ├── lock.sql │ ├── lseg.sql │ ├── macaddr.sql │ ├── macaddr8.sql │ ├── matview.sql │ ├── misc_functions.sql │ ├── misc_sanity.sql │ ├── money.sql │ ├── name.sql │ ├── namespace.sql │ ├── numeric.sql │ ├── numeric_big.sql │ ├── numerology.sql │ ├── object_address.sql │ ├── oid.sql │ ├── oidjoins.sql │ ├── opr_sanity.sql │ ├── partition_aggregate.sql │ ├── partition_info.sql │ ├── partition_join.sql │ ├── partition_prune.sql │ ├── password.sql │ ├── path.sql │ ├── pg_lsn.sql │ ├── plancache.sql │ ├── plpgsql.sql │ ├── point.sql │ ├── polygon.sql │ ├── polymorphism.sql │ ├── portals.sql │ ├── portals_p2.sql │ ├── prepare.sql │ ├── prepared_xacts.sql │ ├── privileges.sql │ ├── psql.sql │ ├── psql_crosstab.sql │ ├── publication.sql │ ├── random.sql │ ├── rangefuncs.sql │ ├── rangetypes.sql │ ├── regex.linux.utf8.sql │ ├── regex.sql │ ├── regproc.sql │ ├── reindex_catalog.sql │ ├── reloptions.sql │ ├── replica_identity.sql │ ├── returning.sql │ ├── roleattributes.sql │ ├── rolenames.sql │ ├── rowsecurity.sql │ ├── rowtypes.sql │ ├── rules.sql │ ├── sanity_check.sql │ ├── security_label.sql │ ├── select.sql │ ├── select_distinct.sql │ ├── select_distinct_on.sql │ ├── select_having.sql │ ├── select_implicit.sql │ ├── select_into.sql │ ├── select_parallel.sql │ ├── select_views.sql │ ├── sequence.sql │ ├── spgist.sql │ ├── stats.sql │ ├── stats_ext.sql │ ├── strings.sql │ ├── subscription.sql │ ├── subselect.sql │ ├── sysviews.sql │ ├── tablesample.sql │ ├── temp.sql │ ├── text.sql │ ├── tidscan.sql │ ├── time.sql │ ├── timestamp.sql │ ├── timestamptz.sql │ ├── timetz.sql │ ├── transactions.sql │ ├── triggers.sql │ ├── truncate.sql │ ├── tsdicts.sql │ ├── tsearch.sql │ ├── tsrf.sql │ ├── tstypes.sql │ ├── txid.sql │ ├── type_sanity.sql │ ├── typed_table.sql │ ├── union.sql │ ├── updatable_views.sql │ ├── update.sql │ ├── uuid.sql │ ├── vacuum.sql │ ├── varchar.sql │ ├── window.sql │ ├── with.sql │ ├── write_parallel.sql │ ├── xml.sql │ └── xmlmap.sql └── sql │ ├── advisory_lock.sql │ ├── aggregates.sql │ ├── alter_generic.sql │ ├── alter_operator.sql │ ├── alter_table.sql │ ├── amutils.sql │ ├── arrays.sql │ ├── async.sql │ ├── bit.sql │ ├── bitmapops.sql │ ├── boolean.sql │ ├── box.sql │ ├── brin.sql │ ├── btree_index.sql │ ├── case.sql │ ├── char.sql │ ├── circle.sql │ ├── cluster.sql │ ├── collate.icu.utf8.sql │ ├── collate.linux.utf8.sql │ ├── collate.sql │ ├── combocid.sql │ ├── comments.sql │ ├── conversion.sql │ ├── create_aggregate.sql │ ├── create_am.sql │ ├── create_cast.sql │ ├── create_function_3.sql │ ├── create_index.sql │ ├── create_index_spgist.sql │ ├── create_misc.sql │ ├── create_operator.sql │ ├── create_procedure.sql │ ├── create_table.sql │ ├── create_table_like.sql │ ├── create_type.sql │ ├── create_view.sql │ ├── date.sql │ ├── dbsize.sql │ ├── delete.sql │ ├── dependency.sql │ ├── domain.sql │ ├── drop_if_exists.sql │ ├── drop_operator.sql │ ├── enum.sql │ ├── equivclass.sql │ ├── errors.sql │ ├── event_trigger.sql │ ├── expressions.sql │ ├── fast_default.sql │ ├── float4.sql │ ├── float8.sql │ ├── foreign_data.sql │ ├── foreign_key.sql │ ├── fsm.sql │ ├── functional_deps.sql │ ├── generated.sql │ ├── geometry.sql │ ├── gin.sql │ ├── gist.sql │ ├── groupingsets.sql │ ├── guc.sql │ ├── hash_func.sql │ ├── hash_index.sql │ ├── hash_part.sql │ ├── horology.sql │ ├── hs_primary_extremes.sql │ ├── hs_primary_setup.sql │ ├── hs_standby_allowed.sql │ ├── hs_standby_check.sql │ ├── hs_standby_disallowed.sql │ ├── hs_standby_functions.sql │ ├── identity.sql │ ├── index_including.sql │ ├── index_including_gist.sql │ ├── indexing.sql │ ├── indirect_toast.sql │ ├── inet.sql │ ├── inherit.sql │ ├── init_privs.sql │ ├── insert.sql │ ├── insert_conflict.sql │ ├── int2.sql │ ├── int4.sql │ ├── int8.sql │ ├── interval.sql │ ├── join.sql │ ├── join_hash.sql │ ├── json.sql │ ├── json_encoding.sql │ ├── jsonb.sql │ ├── jsonb_jsonpath.sql │ ├── jsonpath.sql │ ├── jsonpath_encoding.sql │ ├── limit.sql │ ├── line.sql │ ├── lock.sql │ ├── lseg.sql │ ├── macaddr.sql │ ├── macaddr8.sql │ ├── matview.sql │ ├── misc_functions.sql │ ├── misc_sanity.sql │ ├── money.sql │ ├── name.sql │ ├── namespace.sql │ ├── numeric.sql │ ├── numeric_big.sql │ ├── numerology.sql │ ├── object_address.sql │ ├── oid.sql │ ├── oidjoins.sql │ ├── opr_sanity.sql │ ├── partition_aggregate.sql │ ├── partition_info.sql │ ├── partition_join.sql │ ├── partition_prune.sql │ ├── password.sql │ ├── path.sql │ ├── pg_lsn.sql │ ├── plancache.sql │ ├── plpgsql.sql │ ├── point.sql │ ├── polygon.sql │ ├── polymorphism.sql │ ├── portals.sql │ ├── portals_p2.sql │ ├── prepare.sql │ ├── prepared_xacts.sql │ ├── privileges.sql │ ├── psql.sql │ ├── psql_crosstab.sql │ ├── publication.sql │ ├── random.sql │ ├── rangefuncs.sql │ ├── rangetypes.sql │ ├── regex.linux.utf8.sql │ ├── regex.sql │ ├── regproc.sql │ ├── reindex_catalog.sql │ ├── reloptions.sql │ ├── replica_identity.sql │ ├── returning.sql │ ├── roleattributes.sql │ ├── rolenames.sql │ ├── rowsecurity.sql │ ├── rowtypes.sql │ ├── rules.sql │ ├── sanity_check.sql │ ├── security_label.sql │ ├── select.sql │ ├── select_distinct.sql │ ├── select_distinct_on.sql │ ├── select_having.sql │ ├── select_implicit.sql │ ├── select_into.sql │ ├── select_parallel.sql │ ├── select_views.sql │ ├── sequence.sql │ ├── spgist.sql │ ├── stats.sql │ ├── stats_ext.sql │ ├── strings.sql │ ├── subscription.sql │ ├── subselect.sql │ ├── sysviews.sql │ ├── tablesample.sql │ ├── temp.sql │ ├── text.sql │ ├── tidscan.sql │ ├── time.sql │ ├── timestamp.sql │ ├── timestamptz.sql │ ├── timetz.sql │ ├── transactions.sql │ ├── triggers.sql │ ├── truncate.sql │ ├── tsdicts.sql │ ├── tsearch.sql │ ├── tsrf.sql │ ├── tstypes.sql │ ├── txid.sql │ ├── type_sanity.sql │ ├── typed_table.sql │ ├── union.sql │ ├── updatable_views.sql │ ├── update.sql │ ├── uuid.sql │ ├── vacuum.sql │ ├── varchar.sql │ ├── window.sql │ ├── with.sql │ ├── write_parallel.sql │ ├── xml.sql │ └── xmlmap.sql ├── redshift.kw ├── regress_test.pl └── test-files ├── ex0.sql ├── ex1.sql ├── ex10.sql ├── ex11.sql ├── ex12.sql ├── ex13.sql ├── ex14.sql ├── ex15.sql ├── ex16.sql ├── ex17.sql ├── ex18.sql ├── ex19.sql ├── ex2.sql ├── ex20.sql ├── ex21.sql ├── ex22.sql ├── ex23.sql ├── ex24.sql ├── ex25.sql ├── ex26.sql ├── ex27.sql ├── ex28.sql ├── ex29.sql ├── ex3.sql ├── ex30.sql ├── ex31.sql ├── ex32.sql ├── ex33.sql ├── ex34.sql ├── ex35.sql ├── ex36.sql ├── ex37.sql ├── ex38.sql ├── ex39.sql ├── ex4.sql ├── ex40.sql ├── ex41.sql ├── ex42.sql ├── ex43.sql ├── ex44.sql ├── ex45.sql ├── ex46.sql ├── ex47.sql ├── ex48.sql ├── ex49.sql ├── ex5.sql ├── ex50.sql ├── ex51.sql ├── ex52.sql ├── ex53.sql ├── ex54.sql ├── ex55.sql ├── ex56.sql ├── ex57.sql ├── ex58.sql ├── ex59.sql ├── ex6.sql ├── ex60.sql ├── ex61.sql ├── ex62.sql ├── ex63.sql ├── ex64.sql ├── ex65.sql ├── ex66.sql ├── ex67.sql ├── ex68.sql ├── ex69.sql ├── ex7.sql ├── ex70.sql ├── ex71.sql ├── ex72.sql ├── ex73.sql ├── ex74.sql ├── ex75.sql ├── ex8.sql ├── ex9.sql └── expected ├── ex0.sql ├── ex1.sql ├── ex10.sql ├── ex11.sql ├── ex12.sql ├── ex13.sql ├── ex14.sql ├── ex15.sql ├── ex16.sql ├── ex17.sql ├── ex18.sql ├── ex19.sql ├── ex2.sql ├── ex20.sql ├── ex21.sql ├── ex22.sql ├── ex23.sql ├── ex24.sql ├── ex25.sql ├── ex26.sql ├── ex27.sql ├── ex28.sql ├── ex29.sql ├── ex3.sql ├── ex30.sql ├── ex31.sql ├── ex32.sql ├── ex33.sql ├── ex34.sql ├── ex35.sql ├── ex36.sql ├── ex37.sql ├── ex38.sql ├── ex39.sql ├── ex4.sql ├── ex40.sql ├── ex41.sql ├── ex42.sql ├── ex43.sql ├── ex44.sql ├── ex45.sql ├── ex46.sql ├── ex47.sql ├── ex48.sql ├── ex49.sql ├── ex5.sql ├── ex50.sql ├── ex51.sql ├── ex52.sql ├── ex53.sql ├── ex54.sql ├── ex55.sql ├── ex56.sql ├── ex57.sql ├── ex58.sql ├── ex59.sql ├── ex6.sql ├── ex60.sql ├── ex61.sql ├── ex62.sql ├── ex63.sql ├── ex64.sql ├── ex65.sql ├── ex66.sql ├── ex68.sql ├── ex69.sql ├── ex7.sql ├── ex70.sql ├── ex71.sql ├── ex72.sql ├── ex73.sql ├── ex74.sql ├── ex75.sql ├── ex8.sql └── ex9.sql /.gitignore: -------------------------------------------------------------------------------- 1 | # Swap files 2 | *.swp 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM perl 2 | WORKDIR /pgformatter 3 | COPY . /pgformatter 4 | RUN perl Makefile.PL 5 | RUN make && make install 6 | ENTRYPOINT ["pg_format"] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2025, Gilles Darold 2 | 3 | Permission to use, copy, modify, and distribute this software and its 4 | documentation for any purpose, without fee, and without a written agreement 5 | is hereby granted, provided that the above copyright notice and this 6 | paragraph and the following two paragraphs appear in all copies. 7 | 8 | IN NO EVENT SHALL Gilles Darold BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, 9 | SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, 10 | ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 11 | Gilles Darold HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | 13 | Gilles Darold SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED 14 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 | PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND Gilles Darold 16 | HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 17 | OR MODIFICATIONS. 18 | 19 | -------------------------------------------------------------------------------- /Makefile.PL: -------------------------------------------------------------------------------- 1 | use ExtUtils::MakeMaker; 2 | # See lib/ExtUtils/MakeMaker.pm for details of how to influence 3 | # the contents of the Makefile that is written. 4 | 5 | use strict; 6 | 7 | my @ALLOWED_ARGS = ('INSTALLDIRS','DESTDIR'); 8 | 9 | # Parse command line arguments and store them as environment variables 10 | while ($_ = shift) { 11 | my ($k,$v) = split(/=/, $_, 2); 12 | if (grep(/^$k$/, @ALLOWED_ARGS)) { 13 | $ENV{$k} = $v; 14 | } 15 | } 16 | $ENV{DESTDIR} =~ s/\/$//; 17 | 18 | # Default install path 19 | my $DESTDIR = $ENV{DESTDIR} || ''; 20 | my $INSTALLDIRS = $ENV{INSTALLDIRS} || 'site'; 21 | 22 | WriteMakefile( 23 | 'DISTNAME' => 'pgformatter', 24 | 'NAME' => 'pgFormatter', 25 | 'VERSION_FROM' => 'pg_format', 26 | 'dist' => { 27 | 'COMPRESS'=>'gzip -9f', 'SUFFIX' => 'gz', 28 | 'ZIP'=>'/usr/bin/zip','ZIPFLAGS'=>'-rl' 29 | }, 30 | 'AUTHOR' => 'Gilles Darold (gilles@darold.net)', 31 | 'ABSTRACT' => 'pgFormatter - PostgreSQL SQL and PL/PGSQL syntaxe beautifier', 32 | 'EXE_FILES' => [ qw(pg_format) ], 33 | 'MAN1PODS' => { 'doc/pg_format.pod' => 'blib/man1/pg_format.1' }, 34 | 'DESTDIR' => $DESTDIR, 35 | 'INSTALLDIRS' => $INSTALLDIRS, 36 | 'clean' => {}, 37 | 'META_MERGE' => { 38 | resources => { 39 | homepage => 'http://sqlformat.darold.net/', 40 | repository => { 41 | type => 'git', 42 | git => 'git@github.com:darold/pgformatter.git', 43 | web => 'https://github.com/darold/pgformatter', 44 | }, 45 | }, 46 | } 47 | ); 48 | 49 | -------------------------------------------------------------------------------- /cgi.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:latest 2 | RUN apt-get clean && apt-get update && apt-get install -y spawn-fcgi fcgiwrap libcgi-pm-perl libjson-perl libdata-dump-perl && rm -rf /var/lib/{apt,dpkg} 3 | RUN sed -i 's/www-data/nginx/g' /etc/init.d/fcgiwrap 4 | RUN chown nginx:nginx /etc/init.d/fcgiwrap 5 | RUN < /etc/nginx/conf.d/default.conf < @ARGV ) { 9 | @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV; 10 | } 11 | 12 | # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/ 13 | 14 | # Improve warning/exception handling 15 | use Carp qw( carp croak confess cluck ); 16 | 17 | # give a full stack dump on any untrapped exceptions 18 | local $SIG{ __DIE__ } = sub { 19 | confess "Uncaught exception: @_" unless $^S; 20 | }; 21 | 22 | # now promote run-time warnings into stackdumped exceptions 23 | # *unless* we're in an try block, in which 24 | # case just generate a clucking stackdump instead 25 | local $SIG{ __WARN__ } = sub { 26 | if ( $^S ) { cluck "Trapped warning: @_" } 27 | else { confess "Deadly warning: @_" } 28 | }; 29 | 30 | # Improve warning/exception handling 31 | 32 | # Find libraries path (should be in lib/ directory of where binary is, or on system perl library directory 33 | use FindBin; 34 | use lib "$FindBin::RealBin/lib"; 35 | 36 | our $VERSION = '5.3'; 37 | 38 | # Find out whether current run should be treated as CGI or CLI 39 | my $program; 40 | if ( $ENV{ 'GATEWAY_INTERFACE' } ) { 41 | require pgFormatter::CGI; 42 | $program = pgFormatter::CGI->new(); 43 | } 44 | else { 45 | require pgFormatter::CLI; 46 | $program = pgFormatter::CLI->new(); 47 | } 48 | 49 | $program->run(); 50 | 51 | exit 0; 52 | -------------------------------------------------------------------------------- /t/01_lint.t: -------------------------------------------------------------------------------- 1 | use Test::Simple tests => 2; 2 | 3 | my $ret = `perl -I. -wc pg_format 2>&1`; 4 | ok( $? == 0, "PERL syntax check"); 5 | 6 | $ret = `podchecker doc/*.pod 2>&1`; 7 | ok( $? == 0, "pod syntax check"); 8 | 9 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/async.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- ASYNC 3 | -- 4 | --Should work. Send a valid message via a valid channel name 5 | SELECT 6 | pg_notify('notify_async1', 'sample message1'); 7 | 8 | SELECT 9 | pg_notify('notify_async1', ''); 10 | 11 | SELECT 12 | pg_notify('notify_async1', NULL); 13 | 14 | -- Should fail. Send a valid message via an invalid channel name 15 | SELECT 16 | pg_notify('', 'sample message1'); 17 | 18 | SELECT 19 | pg_notify(NULL, 'sample message1'); 20 | 21 | SELECT 22 | pg_notify('notify_async_channel_name_too_long______________________________', 'sample_message1'); 23 | 24 | --Should work. Valid NOTIFY/LISTEN/UNLISTEN commands 25 | NOTIFY notify_async2; 26 | 27 | LISTEN notify_async2; 28 | 29 | UNLISTEN notify_async2; 30 | 31 | UNLISTEN *; 32 | 33 | -- Should return zero while there are no pending notifications. 34 | -- src/test/isolation/specs/async-notify.spec tests for actual usage. 35 | SELECT 36 | pg_notification_queue_usage (); 37 | 38 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/bitmapops.sql: -------------------------------------------------------------------------------- 1 | -- Test bitmap AND and OR 2 | -- Generate enough data that we can test the lossy bitmaps. 3 | -- There's 55 tuples per page in the table. 53 is just 4 | -- below 55, so that an index scan with qual a = constant 5 | -- will return at least one hit per page. 59 is just above 6 | -- 55, so that an index scan with qual b = constant will return 7 | -- hits on most but not all pages. 53 and 59 are prime, so that 8 | -- there's a maximum number of a,b combinations in the table. 9 | -- That allows us to test all the different combinations of 10 | -- lossy and non-lossy pages with the minimum amount of data 11 | CREATE TABLE bmscantest ( 12 | a int, 13 | b int, 14 | t text 15 | ); 16 | 17 | INSERT INTO bmscantest 18 | SELECT 19 | (r % 53), 20 | (r % 59), 21 | 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' 22 | FROM 23 | generate_series(1, 70000) r; 24 | 25 | CREATE INDEX i_bmtest_a ON bmscantest (a); 26 | 27 | CREATE INDEX i_bmtest_b ON bmscantest (b); 28 | 29 | -- We want to use bitmapscans. With default settings, the planner currently 30 | -- chooses a bitmap scan for the queries below anyway, but let's make sure. 31 | SET enable_indexscan = FALSE; 32 | 33 | SET enable_seqscan = FALSE; 34 | 35 | -- Lower work_mem to trigger use of lossy bitmaps 36 | SET work_mem = 64; 37 | 38 | -- Test bitmap-and. 39 | SELECT 40 | count(*) 41 | FROM 42 | bmscantest 43 | WHERE 44 | a = 1 45 | AND b = 1; 46 | 47 | -- Test bitmap-or. 48 | SELECT 49 | count(*) 50 | FROM 51 | bmscantest 52 | WHERE 53 | a = 1 54 | OR b = 1; 55 | 56 | -- clean up 57 | DROP TABLE bmscantest; 58 | 59 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/char.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- CHAR 3 | -- 4 | -- fixed-length by value 5 | -- internally passed by value if <= 4 bytes in storage 6 | SELECT 7 | char 'c' = char 'c' AS true; 8 | 9 | -- 10 | -- Build a table for testing 11 | -- 12 | CREATE TABLE CHAR_TBL ( 13 | f1 char 14 | ); 15 | 16 | INSERT INTO CHAR_TBL (f1) 17 | VALUES ('a'); 18 | 19 | INSERT INTO CHAR_TBL (f1) 20 | VALUES ('A'); 21 | 22 | -- any of the following three input formats are acceptable 23 | INSERT INTO CHAR_TBL (f1) 24 | VALUES ('1'); 25 | 26 | INSERT INTO CHAR_TBL (f1) 27 | VALUES (2); 28 | 29 | INSERT INTO CHAR_TBL (f1) 30 | VALUES ('3'); 31 | 32 | -- zero-length char 33 | INSERT INTO CHAR_TBL (f1) 34 | VALUES (''); 35 | 36 | -- try char's of greater than 1 length 37 | INSERT INTO CHAR_TBL (f1) 38 | VALUES ('cd'); 39 | 40 | INSERT INTO CHAR_TBL (f1) 41 | VALUES ('c '); 42 | 43 | SELECT 44 | '' AS seven, 45 | * 46 | FROM 47 | CHAR_TBL; 48 | 49 | SELECT 50 | '' AS six, 51 | c.* 52 | FROM 53 | CHAR_TBL c 54 | WHERE 55 | c.f1 <> 'a'; 56 | 57 | SELECT 58 | '' AS one, 59 | c.* 60 | FROM 61 | CHAR_TBL c 62 | WHERE 63 | c.f1 = 'a'; 64 | 65 | SELECT 66 | '' AS five, 67 | c.* 68 | FROM 69 | CHAR_TBL c 70 | WHERE 71 | c.f1 < 'a'; 72 | 73 | SELECT 74 | '' AS six, 75 | c.* 76 | FROM 77 | CHAR_TBL c 78 | WHERE 79 | c.f1 <= 'a'; 80 | 81 | SELECT 82 | '' AS one, 83 | c.* 84 | FROM 85 | CHAR_TBL c 86 | WHERE 87 | c.f1 > 'a'; 88 | 89 | SELECT 90 | '' AS two, 91 | c.* 92 | FROM 93 | CHAR_TBL c 94 | WHERE 95 | c.f1 >= 'a'; 96 | 97 | DROP TABLE CHAR_TBL; 98 | 99 | -- 100 | -- Now test longer arrays of char 101 | -- 102 | CREATE TABLE CHAR_TBL ( 103 | f1 char(4) 104 | ); 105 | 106 | INSERT INTO CHAR_TBL (f1) 107 | VALUES ('a'); 108 | 109 | INSERT INTO CHAR_TBL (f1) 110 | VALUES ('ab'); 111 | 112 | INSERT INTO CHAR_TBL (f1) 113 | VALUES ('abcd'); 114 | 115 | INSERT INTO CHAR_TBL (f1) 116 | VALUES ('abcde'); 117 | 118 | INSERT INTO CHAR_TBL (f1) 119 | VALUES ('abcd '); 120 | 121 | SELECT 122 | '' AS four, 123 | * 124 | FROM 125 | CHAR_TBL; 126 | 127 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/circle.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- CIRCLE 3 | -- 4 | -- avoid bit-exact output here because operations may not be bit-exact. 5 | SET extra_float_digits = 0; 6 | 7 | CREATE TABLE CIRCLE_TBL ( 8 | f1 circle 9 | ); 10 | 11 | INSERT INTO CIRCLE_TBL 12 | VALUES ('<(5,1),3>'); 13 | 14 | INSERT INTO CIRCLE_TBL 15 | VALUES ('<(1,2),100>'); 16 | 17 | INSERT INTO CIRCLE_TBL 18 | VALUES ('1,3,5'); 19 | 20 | INSERT INTO CIRCLE_TBL 21 | VALUES ('((1,2),3)'); 22 | 23 | INSERT INTO CIRCLE_TBL 24 | VALUES ('<(100,200),10>'); 25 | 26 | INSERT INTO CIRCLE_TBL 27 | VALUES (' < ( 100 , 1 ) , 115 > '); 28 | 29 | INSERT INTO CIRCLE_TBL 30 | VALUES ('<(3,5),0>'); 31 | 32 | -- Zero radius 33 | INSERT INTO CIRCLE_TBL 34 | VALUES ('<(3,5),NaN>'); 35 | 36 | -- NaN radius 37 | -- bad values 38 | INSERT INTO CIRCLE_TBL 39 | VALUES ('<(-100,0),-100>'); 40 | 41 | INSERT INTO CIRCLE_TBL 42 | VALUES ('<(100,200),10'); 43 | 44 | INSERT INTO CIRCLE_TBL 45 | VALUES ('<(100,200),10> x'); 46 | 47 | INSERT INTO CIRCLE_TBL 48 | VALUES ('1abc,3,5'); 49 | 50 | INSERT INTO CIRCLE_TBL 51 | VALUES ('(3,(1,2),3)'); 52 | 53 | SELECT 54 | * 55 | FROM 56 | CIRCLE_TBL; 57 | 58 | SELECT 59 | '' AS six, 60 | center(f1) AS center 61 | FROM 62 | CIRCLE_TBL; 63 | 64 | SELECT 65 | '' AS six, 66 | radius(f1) AS radius 67 | FROM 68 | CIRCLE_TBL; 69 | 70 | SELECT 71 | '' AS six, 72 | diameter(f1) AS diameter 73 | FROM 74 | CIRCLE_TBL; 75 | 76 | SELECT 77 | '' AS two, 78 | f1 79 | FROM 80 | CIRCLE_TBL 81 | WHERE 82 | radius(f1) < 5; 83 | 84 | SELECT 85 | '' AS four, 86 | f1 87 | FROM 88 | CIRCLE_TBL 89 | WHERE 90 | diameter(f1) >= 10; 91 | 92 | SELECT 93 | '' AS five, 94 | c1.f1 AS one, 95 | c2.f1 AS two, 96 | (c1.f1 <-> c2.f1) AS distance 97 | FROM 98 | CIRCLE_TBL c1, 99 | CIRCLE_TBL c2 100 | WHERE (c1.f1 < c2.f1) 101 | AND ((c1.f1 <-> c2.f1) > 0) 102 | ORDER BY 103 | distance, 104 | area(c1.f1), 105 | area(c2.f1); 106 | 107 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/comments.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- COMMENTS 3 | -- 4 | SELECT 5 | 'trailing' AS first; 6 | 7 | -- trailing single line 8 | SELECT 9 | /* embedded single line */ 10 | 'embedded' AS second; 11 | 12 | SELECT 13 | /* both embedded and trailing single line */ 14 | 'both' AS third; 15 | 16 | -- trailing single line 17 | SELECT 18 | 'before multi-line' AS fourth; 19 | 20 | 21 | /* This is an example of SQL which should not execute: 22 | * select 'multi-line'; 23 | */ 24 | SELECT 25 | 'after multi-line' AS fifth; 26 | 27 | -- 28 | -- Nested comments 29 | -- 30 | /* 31 | SELECT 'trailing' as x1; -- inside block comment 32 | */ 33 | /* This block comment surrounds a query which itself has a block comment... 34 | SELECT /* embedded single line */ 35 | 'embedded' AS x2; 36 | 37 | / 38 | SELECT 39 | -- continued after the following block comments... 40 | /* Deeply nested comment. 41 | This includes a single apostrophe to make sure we aren't decoding this part as a string. 42 | SELECT 'deep nest' AS n1; 43 | /* Second level of nesting... 44 | SELECT 'deeper nest' as n2; 45 | /* Third level of nesting... 46 | SELECT 'deepest nest' as n3; 47 | */ 48 | Hoo boy. Still two deep... / Now just one deep... / 'deeply nested example' AS sixth; 49 | 50 | 51 | /* and this is the end of the file */ 52 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/conversion.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- create user defined conversion 3 | -- 4 | CREATE USER regress_conversion_user WITH NOCREATEDB NOCREATEROLE; 5 | 6 | SET SESSION AUTHORIZATION regress_conversion_user; 7 | 8 | CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; 9 | 10 | -- 11 | -- cannot make same name conversion in same schema 12 | -- 13 | CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; 14 | 15 | -- 16 | -- create default conversion with qualified name 17 | -- 18 | CREATE DEFAULT CONVERSION public.mydef FOR 'LATIN1' TO 'UTF8' 19 | FROM 20 | iso8859_1_to_utf8; 21 | 22 | -- 23 | -- cannot make default conversion with same schema/for_encoding/to_encoding 24 | -- 25 | CREATE DEFAULT CONVERSION public.mydef2 FOR 'LATIN1' TO 'UTF8' 26 | FROM 27 | iso8859_1_to_utf8; 28 | 29 | -- test comments 30 | COMMENT ON CONVERSION myconv_bad IS 'foo'; 31 | 32 | COMMENT ON CONVERSION myconv IS 'bar'; 33 | 34 | COMMENT ON CONVERSION myconv IS NULL; 35 | 36 | -- 37 | -- drop user defined conversion 38 | -- 39 | DROP CONVERSION myconv; 40 | 41 | DROP CONVERSION mydef; 42 | 43 | -- 44 | -- Note: the built-in conversions are exercised in opr_sanity.sql, 45 | -- so there's no need to do that here. 46 | -- 47 | -- 48 | -- return to the super user 49 | -- 50 | RESET SESSION AUTHORIZATION; 51 | 52 | DROP USER regress_conversion_user; 53 | 54 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/create_cast.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- CREATE_CAST 3 | -- 4 | -- Create some types to test with 5 | CREATE TYPE casttesttype; 6 | 7 | CREATE FUNCTION casttesttype_in (cstring) 8 | RETURNS casttesttype 9 | AS 'textin' 10 | LANGUAGE internal 11 | STRICT IMMUTABLE; 12 | 13 | CREATE FUNCTION casttesttype_out (casttesttype) 14 | RETURNS cstring 15 | AS 'textout' 16 | LANGUAGE internal 17 | STRICT IMMUTABLE; 18 | 19 | CREATE TYPE casttesttype ( 20 | internallength = variable, 21 | input = casttesttype_in, 22 | output = casttesttype_out, 23 | alignment = int4 24 | ); 25 | 26 | -- a dummy function to test with 27 | CREATE FUNCTION casttestfunc (casttesttype) 28 | RETURNS int4 29 | LANGUAGE SQL 30 | AS $$ 31 | SELECT 32 | 1; 33 | $$; 34 | 35 | SELECT 36 | casttestfunc ('foo'::text); 37 | 38 | -- fails, as there's no cast 39 | -- Try binary coercion cast 40 | CREATE CAST (text AS casttesttype) WITHOUT FUNCTION; 41 | 42 | SELECT 43 | casttestfunc ('foo'::text); 44 | 45 | -- doesn't work, as the cast is explicit 46 | SELECT 47 | casttestfunc ('foo'::text::casttesttype); 48 | 49 | -- should work 50 | DROP CAST(text AS casttesttype); 51 | 52 | -- cleanup 53 | -- Try IMPLICIT binary coercion cast 54 | CREATE CAST (text AS casttesttype) WITHOUT FUNCTION AS IMPLICIT; 55 | 56 | SELECT 57 | casttestfunc ('foo'::text); 58 | 59 | -- Should work now 60 | -- Try I/O conversion cast. 61 | SELECT 62 | 1234::int4::casttesttype; 63 | 64 | -- No cast yet, should fail 65 | CREATE CAST (int4 AS casttesttype) WITH INOUT; 66 | 67 | SELECT 68 | 1234::int4::casttesttype; 69 | 70 | -- Should work now 71 | DROP CAST(int4 AS casttesttype); 72 | 73 | -- Try cast with a function 74 | CREATE FUNCTION int4_casttesttype (int4) 75 | RETURNS casttesttype 76 | LANGUAGE SQL 77 | AS $$ 78 | SELECT 79 | ('foo'::text || $1::text)::casttesttype; 80 | $$; 81 | 82 | CREATE CAST (int4 AS casttesttype) WITH FUNCTION int4_casttesttype (int4) AS IMPLICIT; 83 | 84 | SELECT 85 | 1234::int4::casttesttype; 86 | 87 | -- Should work now 88 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/delete.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE delete_test ( 2 | id serial PRIMARY KEY, 3 | a int, 4 | b text 5 | ); 6 | 7 | INSERT INTO delete_test (a) 8 | VALUES (10); 9 | 10 | INSERT INTO delete_test (a, b) 11 | VALUES (50, repeat('x', 10000)); 12 | 13 | INSERT INTO delete_test (a) 14 | VALUES (100); 15 | 16 | -- allow an alias to be specified for DELETE's target table 17 | DELETE FROM delete_test AS dt 18 | WHERE dt.a > 75; 19 | 20 | -- if an alias is specified, don't allow the original table name 21 | -- to be referenced 22 | DELETE FROM delete_test dt 23 | WHERE delete_test.a > 25; 24 | 25 | SELECT 26 | id, 27 | a, 28 | char_length(b) 29 | FROM 30 | delete_test; 31 | 32 | -- delete a row with a TOASTed value 33 | DELETE FROM delete_test 34 | WHERE a > 25; 35 | 36 | SELECT 37 | id, 38 | a, 39 | char_length(b) 40 | FROM 41 | delete_test; 42 | 43 | DROP TABLE delete_test; 44 | 45 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/drop_operator.sql: -------------------------------------------------------------------------------- 1 | CREATE OPERATOR === ( 2 | PROCEDURE = int8eq, 3 | LEFTARG = bigint, 4 | RIGHTARG = bigint, 5 | COMMUTATOR = === 6 | ); 7 | 8 | CREATE OPERATOR !== ( 9 | PROCEDURE = int8ne, 10 | LEFTARG = bigint, 11 | RIGHTARG = bigint, 12 | NEGATOR = ===, 13 | COMMUTATOR = !== 14 | ); 15 | 16 | DROP OPERATOR !== (bigint, bigint); 17 | 18 | SELECT 19 | ctid, 20 | oprcom 21 | FROM 22 | pg_catalog.pg_operator fk 23 | WHERE 24 | oprcom != 0 25 | AND NOT EXISTS ( 26 | SELECT 27 | 1 28 | FROM 29 | pg_catalog.pg_operator pk 30 | WHERE 31 | pk.oid = fk.oprcom); 32 | 33 | SELECT 34 | ctid, 35 | oprnegate 36 | FROM 37 | pg_catalog.pg_operator fk 38 | WHERE 39 | oprnegate != 0 40 | AND NOT EXISTS ( 41 | SELECT 42 | 1 43 | FROM 44 | pg_catalog.pg_operator pk 45 | WHERE 46 | pk.oid = fk.oprnegate); 47 | 48 | DROP OPERATOR === (bigint, bigint); 49 | 50 | CREATE OPERATOR <||<| ( 51 | PROCEDURE = int8lt, 52 | LEFTARG = bigint, 53 | RIGHTARG = bigint 54 | ); 55 | 56 | CREATE OPERATOR <||><| ( 57 | PROCEDURE = int8gt, 58 | LEFTARG = bigint, 59 | RIGHTARG = bigint, 60 | NEGATOR = <|, 61 | COMMUTATOR = <| 62 | ); 63 | 64 | DROP OPERATOR <||><| (bigint, bigint); 65 | 66 | SELECT 67 | ctid, 68 | oprcom 69 | FROM 70 | pg_catalog.pg_operator fk 71 | WHERE 72 | oprcom != 0 73 | AND NOT EXISTS ( 74 | SELECT 75 | 1 76 | FROM 77 | pg_catalog.pg_operator pk 78 | WHERE 79 | pk.oid = fk.oprcom); 80 | 81 | SELECT 82 | ctid, 83 | oprnegate 84 | FROM 85 | pg_catalog.pg_operator fk 86 | WHERE 87 | oprnegate != 0 88 | AND NOT EXISTS ( 89 | SELECT 90 | 1 91 | FROM 92 | pg_catalog.pg_operator pk 93 | WHERE 94 | pk.oid = fk.oprnegate); 95 | 96 | DROP OPERATOR <||<| (bigint, bigint); 97 | 98 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/expressions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- expression evaluated tests that don't fit into a more specific file 3 | -- 4 | -- 5 | -- Tests for SQLVAlueFunction 6 | -- 7 | -- current_date (always matches because of transactional behaviour) 8 | SELECT 9 | date(now())::text = CURRENT_DATE::text; 10 | 11 | -- current_time / localtime 12 | SELECT 13 | now()::timetz::text = CURRENT_TIME::text; 14 | 15 | SELECT 16 | now()::time::text = LOCALTIME::text; 17 | 18 | -- current_timestamp / localtimestamp (always matches because of transactional behaviour) 19 | SELECT 20 | CURRENT_TIMESTAMP = NOW(); 21 | 22 | -- precision 23 | SELECT 24 | length(CURRENT_TIMESTAMP::text) >= length(current_timestamp(0)::text); 25 | 26 | -- localtimestamp 27 | SELECT 28 | now()::timestamp::text = LOCALTIMESTAMP::text; 29 | 30 | -- current_role/user/user is tested in rolnames.sql 31 | -- current database / catalog 32 | SELECT 33 | current_catalog = current_database(); 34 | 35 | -- current_schema 36 | SELECT 37 | current_schema; 38 | 39 | SET search_path = 'notme'; 40 | 41 | SELECT 42 | current_schema; 43 | 44 | SET search_path = 'pg_catalog'; 45 | 46 | SELECT 47 | current_schema; 48 | 49 | RESET search_path; 50 | 51 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/gin.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Test GIN indexes. 3 | -- 4 | -- There are other tests to test different GIN opclassed. This is for testing 5 | -- GIN itself. 6 | -- Create and populate a test table with a GIN index. 7 | CREATE TABLE gin_test_tbl ( 8 | i int4[] 9 | ) 10 | WITH ( 11 | autovacuum_enabled = OFF 12 | ); 13 | 14 | CREATE INDEX gin_test_idx ON gin_test_tbl USING gin (i) WITH (fastupdate = ON, gin_pending_list_limit = 4096); 15 | 16 | INSERT INTO gin_test_tbl 17 | SELECT 18 | ARRAY[1, 2, g] 19 | FROM 20 | generate_series(1, 20000) g; 21 | 22 | INSERT INTO gin_test_tbl 23 | SELECT 24 | ARRAY[1, 3, g] 25 | FROM 26 | generate_series(1, 1000) g; 27 | 28 | SELECT 29 | gin_clean_pending_list ('gin_test_idx') > 10 AS many; 30 | 31 | -- flush the fastupdate buffers 32 | INSERT INTO gin_test_tbl 33 | SELECT 34 | ARRAY[3, 1, g] 35 | FROM 36 | generate_series(1, 1000) g; 37 | 38 | VACUUM gin_test_tbl; 39 | 40 | -- flush the fastupdate buffers 41 | SELECT 42 | gin_clean_pending_list ('gin_test_idx'); 43 | 44 | -- nothing to flush 45 | -- Test vacuuming 46 | DELETE FROM gin_test_tbl 47 | WHERE i @> ARRAY[2]; 48 | 49 | VACUUM gin_test_tbl; 50 | 51 | -- Disable fastupdate, and do more insertions. With fastupdate enabled, most 52 | -- insertions (by flushing the list pages) cause page splits. Without 53 | -- fastupdate, we get more churn in the GIN data leaf pages, and exercise the 54 | -- recompression codepaths. 55 | ALTER INDEX gin_test_idx SET (fastupdate = OFF); 56 | 57 | INSERT INTO gin_test_tbl 58 | SELECT 59 | ARRAY[1, 2, g] 60 | FROM 61 | generate_series(1, 1000) g; 62 | 63 | INSERT INTO gin_test_tbl 64 | SELECT 65 | ARRAY[1, 3, g] 66 | FROM 67 | generate_series(1, 1000) g; 68 | 69 | DELETE FROM gin_test_tbl 70 | WHERE i @> ARRAY[2]; 71 | 72 | VACUUM gin_test_tbl; 73 | 74 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/hs_primary_extremes.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_primary_extremes.sql 5 | -- 6 | DROP TABLE IF EXISTS hs_extreme; 7 | 8 | CREATE TABLE hs_extreme ( 9 | col1 integer 10 | ); 11 | 12 | CREATE OR REPLACE FUNCTION hs_subxids (n integer) 13 | RETURNS void 14 | LANGUAGE plpgsql 15 | AS $$ 16 | BEGIN 17 | IF n <= 0 THEN 18 | RETURN; 19 | END IF; 20 | INSERT INTO hs_extreme 21 | VALUES (n); 22 | PERFORM 23 | hs_subxids (n - 1); 24 | RETURN; 25 | EXCEPTION 26 | WHEN raise_exception THEN 27 | NULL; 28 | END; 29 | 30 | $$; 31 | 32 | BEGIN; 33 | SELECT 34 | hs_subxids (257); 35 | ROLLBACK; 36 | 37 | BEGIN; 38 | SELECT 39 | hs_subxids (257); 40 | COMMIT; 41 | 42 | SET client_min_messages = 'warning'; 43 | 44 | CREATE OR REPLACE FUNCTION hs_locks_create (n integer) 45 | RETURNS void 46 | LANGUAGE plpgsql 47 | AS $$ 48 | BEGIN 49 | IF n <= 0 THEN 50 | CHECKPOINT; 51 | RETURN; 52 | END IF; 53 | EXECUTE 'CREATE TABLE hs_locks_' || n::text || ' ()'; 54 | PERFORM 55 | hs_locks_create (n - 1); 56 | RETURN; 57 | EXCEPTION 58 | WHEN raise_exception THEN 59 | NULL; 60 | END; 61 | 62 | $$; 63 | 64 | CREATE OR REPLACE FUNCTION hs_locks_drop (n integer) 65 | RETURNS void 66 | LANGUAGE plpgsql 67 | AS $$ 68 | BEGIN 69 | IF n <= 0 THEN 70 | CHECKPOINT; 71 | RETURN; 72 | END IF; 73 | EXECUTE 'DROP TABLE IF EXISTS hs_locks_' || n::text; 74 | PERFORM 75 | hs_locks_drop (n - 1); 76 | RETURN; 77 | EXCEPTION 78 | WHEN raise_exception THEN 79 | NULL; 80 | END; 81 | 82 | $$; 83 | 84 | BEGIN; 85 | SELECT 86 | hs_locks_drop (257); 87 | SELECT 88 | hs_locks_create (257); 89 | SELECT 90 | count(*) > 257 91 | FROM 92 | pg_locks; 93 | ROLLBACK; 94 | 95 | BEGIN; 96 | SELECT 97 | hs_locks_drop (257); 98 | SELECT 99 | hs_locks_create (257); 100 | SELECT 101 | count(*) > 257 102 | FROM 103 | pg_locks; 104 | COMMIT; 105 | 106 | SELECT 107 | hs_locks_drop (257); 108 | 109 | SELECT 110 | pg_switch_wal (); 111 | 112 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/hs_primary_setup.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_primary_setup.sql 5 | -- 6 | DROP TABLE IF EXISTS hs1; 7 | 8 | CREATE TABLE hs1 ( 9 | col1 integer PRIMARY KEY 10 | ); 11 | 12 | INSERT INTO hs1 13 | VALUES (1); 14 | 15 | DROP TABLE IF EXISTS hs2; 16 | 17 | CREATE TABLE hs2 ( 18 | col1 integer PRIMARY KEY 19 | ); 20 | 21 | INSERT INTO hs2 22 | VALUES (12); 23 | 24 | INSERT INTO hs2 25 | VALUES (13); 26 | 27 | DROP TABLE IF EXISTS hs3; 28 | 29 | CREATE TABLE hs3 ( 30 | col1 integer PRIMARY KEY 31 | ); 32 | 33 | INSERT INTO hs3 34 | VALUES (113); 35 | 36 | INSERT INTO hs3 37 | VALUES (114); 38 | 39 | INSERT INTO hs3 40 | VALUES (115); 41 | 42 | DROP SEQUENCE IF EXISTS hsseq; 43 | 44 | CREATE SEQUENCE hsseq; 45 | 46 | SELECT 47 | pg_switch_wal (); 48 | 49 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/hs_standby_check.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_standby_check.sql 5 | -- 6 | -- 7 | -- If the query below returns false then all other tests will fail after it. 8 | -- 9 | SELECT 10 | CASE pg_is_in_recovery() 11 | WHEN FALSE THEN 12 | 'These tests are intended only for execution on a standby server that is reading ' || 'WAL from a server upon which the regression database is already created and into ' || 'which src/test/regress/sql/hs_primary_setup.sql has been run' 13 | ELSE 14 | 'Tests are running on a standby server during recovery' 15 | END; 16 | 17 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/hs_standby_functions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_standby_functions.sql 5 | -- 6 | -- should fail 7 | SELECT 8 | txid_current(); 9 | 10 | SELECT 11 | length(txid_current_snapshot()::text) >= 4; 12 | 13 | SELECT 14 | pg_start_backup('should fail'); 15 | 16 | SELECT 17 | pg_switch_wal (); 18 | 19 | SELECT 20 | pg_stop_backup(); 21 | 22 | -- should return no rows 23 | SELECT 24 | * 25 | FROM 26 | pg_prepared_xacts; 27 | 28 | -- just the startup process 29 | SELECT 30 | locktype, 31 | virtualxid, 32 | virtualtransaction, 33 | mode, 34 | granted 35 | FROM 36 | pg_locks 37 | WHERE 38 | virtualxid = '1/1'; 39 | 40 | -- suicide is painless 41 | SELECT 42 | pg_cancel_backend(pg_backend_pid()); 43 | 44 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/init_privs.sql: -------------------------------------------------------------------------------- 1 | -- Test initial privileges 2 | -- There should always be some initial privileges, set up by initdb 3 | SELECT 4 | count(*) > 0 5 | FROM 6 | pg_init_privs; 7 | 8 | -- Intentionally include some non-initial privs for pg_dump to dump out 9 | GRANT SELECT ON pg_proc TO CURRENT_USER; 10 | 11 | GRANT SELECT (prosrc) ON pg_proc TO CURRENT_USER; 12 | 13 | GRANT SELECT (rolname, rolsuper) ON pg_authid TO CURRENT_USER; 14 | 15 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/line.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- LINE 3 | -- Infinite lines 4 | -- 5 | --DROP TABLE LINE_TBL; 6 | CREATE TABLE LINE_TBL ( 7 | s line 8 | ); 9 | 10 | INSERT INTO LINE_TBL 11 | VALUES ('{0,-1,5}'); 12 | 13 | -- A == 0 14 | INSERT INTO LINE_TBL 15 | VALUES ('{1,0,5}'); 16 | 17 | -- B == 0 18 | INSERT INTO LINE_TBL 19 | VALUES ('{0,3,0}'); 20 | 21 | -- A == C == 0 22 | INSERT INTO LINE_TBL 23 | VALUES (' (0,0), (6,6)'); 24 | 25 | INSERT INTO LINE_TBL 26 | VALUES ('10,-10 ,-5,-4'); 27 | 28 | INSERT INTO LINE_TBL 29 | VALUES ('[-1e6,2e2,3e5, -4e1]'); 30 | 31 | INSERT INTO LINE_TBL 32 | VALUES ('{3,NaN,5}'); 33 | 34 | INSERT INTO LINE_TBL 35 | VALUES ('{NaN,NaN,NaN}'); 36 | 37 | -- horizontal 38 | INSERT INTO LINE_TBL 39 | VALUES ('[(1,3),(2,3)]'); 40 | 41 | -- vertical 42 | INSERT INTO LINE_TBL 43 | VALUES (line(point '(3,1)', point '(3,2)')); 44 | 45 | -- bad values for parser testing 46 | INSERT INTO LINE_TBL 47 | VALUES ('{}'); 48 | 49 | INSERT INTO LINE_TBL 50 | VALUES ('{0'); 51 | 52 | INSERT INTO LINE_TBL 53 | VALUES ('{0,0}'); 54 | 55 | INSERT INTO LINE_TBL 56 | VALUES ('{0,0,1'); 57 | 58 | INSERT INTO LINE_TBL 59 | VALUES ('{0,0,1}'); 60 | 61 | INSERT INTO LINE_TBL 62 | VALUES ('{0,0,1} x'); 63 | 64 | INSERT INTO LINE_TBL 65 | VALUES ('(3asdf,2 ,3,4r2)'); 66 | 67 | INSERT INTO LINE_TBL 68 | VALUES ('[1,2,3, 4'); 69 | 70 | INSERT INTO LINE_TBL 71 | VALUES ('[(,2),(3,4)]'); 72 | 73 | INSERT INTO LINE_TBL 74 | VALUES ('[(1,2),(3,4)'); 75 | 76 | INSERT INTO LINE_TBL 77 | VALUES ('[(1,2),(1,2)]'); 78 | 79 | INSERT INTO LINE_TBL 80 | VALUES (line(point '(1,0)', point '(1,0)')); 81 | 82 | SELECT 83 | * 84 | FROM 85 | LINE_TBL; 86 | 87 | SELECT 88 | '{nan, 1, nan}'::line = '{nan, 1, nan}'::line AS true, 89 | '{nan, 1, nan}'::line = '{nan, 2, nan}'::line AS false; 90 | 91 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/lseg.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- LSEG 3 | -- Line segments 4 | -- 5 | --DROP TABLE LSEG_TBL; 6 | CREATE TABLE LSEG_TBL ( 7 | s lseg 8 | ); 9 | 10 | INSERT INTO LSEG_TBL 11 | VALUES ('[(1,2),(3,4)]'); 12 | 13 | INSERT INTO LSEG_TBL 14 | VALUES ('(0,0),(6,6)'); 15 | 16 | INSERT INTO LSEG_TBL 17 | VALUES ('10,-10 ,-3,-4'); 18 | 19 | INSERT INTO LSEG_TBL 20 | VALUES ('[-1e6,2e2,3e5, -4e1]'); 21 | 22 | INSERT INTO LSEG_TBL 23 | VALUES (lseg(point(11, 22), point(33, 44))); 24 | 25 | INSERT INTO LSEG_TBL 26 | VALUES ('[(-10,2),(-10,3)]'); 27 | 28 | -- vertical 29 | INSERT INTO LSEG_TBL 30 | VALUES ('[(0,-20),(30,-20)]'); 31 | 32 | -- horizontal 33 | INSERT INTO LSEG_TBL 34 | VALUES ('[(NaN,1),(NaN,90)]'); 35 | 36 | -- NaN 37 | -- bad values for parser testing 38 | INSERT INTO LSEG_TBL 39 | VALUES ('(3asdf,2 ,3,4r2)'); 40 | 41 | INSERT INTO LSEG_TBL 42 | VALUES ('[1,2,3, 4'); 43 | 44 | INSERT INTO LSEG_TBL 45 | VALUES ('[(,2),(3,4)]'); 46 | 47 | INSERT INTO LSEG_TBL 48 | VALUES ('[(1,2),(3,4)'); 49 | 50 | SELECT 51 | * 52 | FROM 53 | LSEG_TBL; 54 | 55 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/namespace.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Regression tests for schemas (namespaces) 3 | -- 4 | CREATE SCHEMA test_ns_schema_1 5 | CREATE UNIQUE INDEX abc_a_idx ON abc ( 6 | a) 7 | CREATE VIEW abc_view AS 8 | SELECT 9 | a + 1 AS a, 10 | b + 1 AS b 11 | FROM 12 | abc 13 | CREATE TABLE abc ( 14 | a serial, 15 | b int UNIQUE 16 | ); 17 | 18 | -- verify that the objects were created 19 | SELECT 20 | COUNT(*) 21 | FROM 22 | pg_class 23 | WHERE 24 | relnamespace = ( 25 | SELECT 26 | oid 27 | FROM 28 | pg_namespace 29 | WHERE 30 | nspname = 'test_ns_schema_1'); 31 | 32 | INSERT INTO test_ns_schema_1.abc DEFAULT VALUES; INSERT INTO test_ns_schema_1.abc DEFAULT VALUES; INSERT INTO test_ns_schema_1.abc DEFAULT VALUES; 33 | SELECT 34 | * 35 | FROM 36 | test_ns_schema_1.abc; 37 | 38 | SELECT 39 | * 40 | FROM 41 | test_ns_schema_1.abc_view; 42 | 43 | ALTER SCHEMA test_ns_schema_1 RENAME TO test_ns_schema_renamed; 44 | 45 | SELECT 46 | COUNT(*) 47 | FROM 48 | pg_class 49 | WHERE 50 | relnamespace = ( 51 | SELECT 52 | oid 53 | FROM 54 | pg_namespace 55 | WHERE 56 | nspname = 'test_ns_schema_1'); 57 | 58 | -- test IF NOT EXISTS cases 59 | CREATE SCHEMA test_ns_schema_renamed; 60 | 61 | -- fail, already exists 62 | CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed; 63 | 64 | -- ok with notice 65 | CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed -- fail, disallowed 66 | CREATE TABLE abc ( 67 | a serial, 68 | b int UNIQUE 69 | ); 70 | 71 | DROP SCHEMA test_ns_schema_renamed CASCADE; 72 | 73 | -- verify that the objects were dropped 74 | SELECT 75 | COUNT(*) 76 | FROM 77 | pg_class 78 | WHERE 79 | relnamespace = ( 80 | SELECT 81 | oid 82 | FROM 83 | pg_namespace 84 | WHERE 85 | nspname = 'test_ns_schema_renamed'); 86 | 87 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/oid.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- OID 3 | -- 4 | CREATE TABLE OID_TBL ( 5 | f1 oid 6 | ); 7 | 8 | INSERT INTO OID_TBL (f1) 9 | VALUES ('1234'); 10 | 11 | INSERT INTO OID_TBL (f1) 12 | VALUES ('1235'); 13 | 14 | INSERT INTO OID_TBL (f1) 15 | VALUES ('987'); 16 | 17 | INSERT INTO OID_TBL (f1) 18 | VALUES ('-1040'); 19 | 20 | INSERT INTO OID_TBL (f1) 21 | VALUES ('99999999'); 22 | 23 | INSERT INTO OID_TBL (f1) 24 | VALUES ('5 '); 25 | 26 | INSERT INTO OID_TBL (f1) 27 | VALUES (' 10 '); 28 | 29 | -- leading/trailing hard tab is also allowed 30 | INSERT INTO OID_TBL (f1) 31 | VALUES (' 15 '); 32 | 33 | -- bad inputs 34 | INSERT INTO OID_TBL (f1) 35 | VALUES (''); 36 | 37 | INSERT INTO OID_TBL (f1) 38 | VALUES (' '); 39 | 40 | INSERT INTO OID_TBL (f1) 41 | VALUES ('asdfasd'); 42 | 43 | INSERT INTO OID_TBL (f1) 44 | VALUES ('99asdfasd'); 45 | 46 | INSERT INTO OID_TBL (f1) 47 | VALUES ('5 d'); 48 | 49 | INSERT INTO OID_TBL (f1) 50 | VALUES (' 5d'); 51 | 52 | INSERT INTO OID_TBL (f1) 53 | VALUES ('5 5'); 54 | 55 | INSERT INTO OID_TBL (f1) 56 | VALUES (' - 500'); 57 | 58 | INSERT INTO OID_TBL (f1) 59 | VALUES ('32958209582039852935'); 60 | 61 | INSERT INTO OID_TBL (f1) 62 | VALUES ('-23582358720398502385'); 63 | 64 | SELECT 65 | '' AS six, 66 | * 67 | FROM 68 | OID_TBL; 69 | 70 | SELECT 71 | '' AS one, 72 | o.* 73 | FROM 74 | OID_TBL o 75 | WHERE 76 | o.f1 = 1234; 77 | 78 | SELECT 79 | '' AS five, 80 | o.* 81 | FROM 82 | OID_TBL o 83 | WHERE 84 | o.f1 <> '1234'; 85 | 86 | SELECT 87 | '' AS three, 88 | o.* 89 | FROM 90 | OID_TBL o 91 | WHERE 92 | o.f1 <= '1234'; 93 | 94 | SELECT 95 | '' AS two, 96 | o.* 97 | FROM 98 | OID_TBL o 99 | WHERE 100 | o.f1 < '1234'; 101 | 102 | SELECT 103 | '' AS four, 104 | o.* 105 | FROM 106 | OID_TBL o 107 | WHERE 108 | o.f1 >= '1234'; 109 | 110 | SELECT 111 | '' AS three, 112 | o.* 113 | FROM 114 | OID_TBL o 115 | WHERE 116 | o.f1 > '1234'; 117 | 118 | DROP TABLE OID_TBL; 119 | 120 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/path.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PATH 3 | -- 4 | --DROP TABLE PATH_TBL; 5 | CREATE TABLE PATH_TBL ( 6 | f1 path 7 | ); 8 | 9 | INSERT INTO PATH_TBL 10 | VALUES ('[(1,2),(3,4)]'); 11 | 12 | INSERT INTO PATH_TBL 13 | VALUES (' ( ( 1 , 2 ) , ( 3 , 4 ) ) '); 14 | 15 | INSERT INTO PATH_TBL 16 | VALUES ('[ (0,0),(3,0),(4,5),(1,6) ]'); 17 | 18 | INSERT INTO PATH_TBL 19 | VALUES ('((1,2) ,(3,4 ))'); 20 | 21 | INSERT INTO PATH_TBL 22 | VALUES ('1,2 ,3,4 '); 23 | 24 | INSERT INTO PATH_TBL 25 | VALUES (' [1,2,3, 4] '); 26 | 27 | INSERT INTO PATH_TBL 28 | VALUES ('((10,20))'); 29 | 30 | -- Only one point 31 | INSERT INTO PATH_TBL 32 | VALUES ('[ 11,12,13,14 ]'); 33 | 34 | INSERT INTO PATH_TBL 35 | VALUES ('( 11,12,13,14) '); 36 | 37 | -- bad values for parser testing 38 | INSERT INTO PATH_TBL 39 | VALUES ('[]'); 40 | 41 | INSERT INTO PATH_TBL 42 | VALUES ('[(,2),(3,4)]'); 43 | 44 | INSERT INTO PATH_TBL 45 | VALUES ('[(1,2),(3,4)'); 46 | 47 | INSERT INTO PATH_TBL 48 | VALUES ('(1,2,3,4'); 49 | 50 | INSERT INTO PATH_TBL 51 | VALUES ('(1,2),(3,4)]'); 52 | 53 | SELECT 54 | '' AS count, 55 | f1 AS open_path 56 | FROM 57 | PATH_TBL 58 | WHERE 59 | isopen(f1); 60 | 61 | SELECT 62 | '' AS count, 63 | f1 AS closed_path 64 | FROM 65 | PATH_TBL 66 | WHERE 67 | isclosed(f1); 68 | 69 | SELECT 70 | '' AS count, 71 | pclose(f1) AS closed_path 72 | FROM 73 | PATH_TBL; 74 | 75 | SELECT 76 | '' AS count, 77 | popen(f1) AS open_path 78 | FROM 79 | PATH_TBL; 80 | 81 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/pg_lsn.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PG_LSN 3 | -- 4 | CREATE TABLE PG_LSN_TBL ( 5 | f1 pg_lsn 6 | ); 7 | 8 | -- Largest and smallest input 9 | INSERT INTO PG_LSN_TBL 10 | VALUES ('0/0'); 11 | 12 | INSERT INTO PG_LSN_TBL 13 | VALUES ('FFFFFFFF/FFFFFFFF'); 14 | 15 | -- Incorrect input 16 | INSERT INTO PG_LSN_TBL 17 | VALUES ('G/0'); 18 | 19 | INSERT INTO PG_LSN_TBL 20 | VALUES ('-1/0'); 21 | 22 | INSERT INTO PG_LSN_TBL 23 | VALUES (' 0/12345678'); 24 | 25 | INSERT INTO PG_LSN_TBL 26 | VALUES ('ABCD/'); 27 | 28 | INSERT INTO PG_LSN_TBL 29 | VALUES ('/ABCD'); 30 | 31 | DROP TABLE PG_LSN_TBL; 32 | 33 | -- Operators 34 | SELECT 35 | '0/16AE7F8' = '0/16AE7F8'::pg_lsn; 36 | 37 | SELECT 38 | '0/16AE7F8'::pg_lsn != '0/16AE7F7'; 39 | 40 | SELECT 41 | '0/16AE7F7' < '0/16AE7F8'::pg_lsn; 42 | 43 | SELECT 44 | '0/16AE7F8' > pg_lsn '0/16AE7F7'; 45 | 46 | SELECT 47 | '0/16AE7F7'::pg_lsn - '0/16AE7F8'::pg_lsn; 48 | 49 | SELECT 50 | '0/16AE7F8'::pg_lsn - '0/16AE7F7'::pg_lsn; 51 | 52 | -- Check btree and hash opclasses 53 | EXPLAIN ( 54 | COSTS OFF 55 | ) SELECT DISTINCT 56 | (i || '/' || j)::pg_lsn f 57 | FROM 58 | generate_series(1, 10) i, 59 | generate_series(1, 10) j, 60 | generate_series(1, 5) k 61 | WHERE 62 | i <= 10 63 | AND j > 0 64 | AND j <= 10 65 | ORDER BY 66 | f; 67 | 68 | SELECT DISTINCT 69 | (i || '/' || j)::pg_lsn f 70 | FROM 71 | generate_series(1, 10) i, 72 | generate_series(1, 10) j, 73 | generate_series(1, 5) k 74 | WHERE 75 | i <= 10 76 | AND j > 0 77 | AND j <= 10 78 | ORDER BY 79 | f; 80 | 81 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/random.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- RANDOM 3 | -- Test the random function 4 | -- 5 | -- count the number of tuples originally, should be 1000 6 | SELECT 7 | count(*) 8 | FROM 9 | onek; 10 | 11 | -- pick three random rows, they shouldn't match 12 | ( 13 | SELECT 14 | unique1 AS random 15 | FROM 16 | onek 17 | ORDER BY 18 | random() 19 | LIMIT 1) 20 | INTERSECT ( 21 | SELECT 22 | unique1 AS random 23 | FROM 24 | onek 25 | ORDER BY 26 | random() 27 | LIMIT 1) 28 | INTERSECT ( 29 | SELECT 30 | unique1 AS random 31 | FROM 32 | onek 33 | ORDER BY 34 | random() 35 | LIMIT 1); 36 | 37 | -- count roughly 1/10 of the tuples 38 | SELECT 39 | count(*) AS random INTO RANDOM_TBL 40 | FROM 41 | onek 42 | WHERE 43 | random() < 1.0 / 10; 44 | 45 | -- select again, the count should be different 46 | INSERT INTO RANDOM_TBL (random) 47 | SELECT 48 | count(*) 49 | FROM 50 | onek 51 | WHERE 52 | random() < 1.0 / 10; 53 | 54 | -- select again, the count should be different 55 | INSERT INTO RANDOM_TBL (random) 56 | SELECT 57 | count(*) 58 | FROM 59 | onek 60 | WHERE 61 | random() < 1.0 / 10; 62 | 63 | -- select again, the count should be different 64 | INSERT INTO RANDOM_TBL (random) 65 | SELECT 66 | count(*) 67 | FROM 68 | onek 69 | WHERE 70 | random() < 1.0 / 10; 71 | 72 | -- now test that they are different counts 73 | SELECT 74 | random, 75 | count(random) 76 | FROM 77 | RANDOM_TBL 78 | GROUP BY 79 | random 80 | HAVING 81 | count(random) > 3; 82 | 83 | SELECT 84 | AVG(random) 85 | FROM 86 | RANDOM_TBL 87 | HAVING 88 | AVG(random) 89 | NOT BETWEEN 80 AND 120; 90 | 91 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/regex.linux.utf8.sql: -------------------------------------------------------------------------------- 1 | /* 2 | * This test is for Linux/glibc systems and others that implement proper 3 | * locale classification of Unicode characters with high code values. 4 | * It must be run in a database with UTF8 encoding and a Unicode-aware locale. 5 | */ 6 | SET client_encoding TO UTF8; 7 | 8 | -- 9 | -- Test the "high colormap" logic with single characters and ranges that 10 | -- exceed the MAX_SIMPLE_CHR cutoff, here assumed to be less than U+2000. 11 | -- 12 | -- trivial cases: 13 | SELECT 14 | 'aⓐ' ~ U & 'a\24D0' AS t; 15 | 16 | SELECT 17 | 'aⓐ' ~ U & 'a\24D1' AS f; 18 | 19 | SELECT 20 | 'aⓕ' ~ 'a[ⓐ-ⓩ]' AS t; 21 | 22 | SELECT 23 | 'aⒻ' ~ 'a[ⓐ-ⓩ]' AS f; 24 | 25 | -- cases requiring splitting of ranges: 26 | SELECT 27 | 'aⓕⓕ' ~ 'aⓕ[ⓐ-ⓩ]' AS t; 28 | 29 | SELECT 30 | 'aⓕⓐ' ~ 'aⓕ[ⓐ-ⓩ]' AS t; 31 | 32 | SELECT 33 | 'aⓐⓕ' ~ 'aⓕ[ⓐ-ⓩ]' AS f; 34 | 35 | SELECT 36 | 'aⓕⓕ' ~ 'a[ⓐ-ⓩ]ⓕ' AS t; 37 | 38 | SELECT 39 | 'aⓕⓐ' ~ 'a[ⓐ-ⓩ]ⓕ' AS f; 40 | 41 | SELECT 42 | 'aⓐⓕ' ~ 'a[ⓐ-ⓩ]ⓕ' AS t; 43 | 44 | SELECT 45 | 'aⒶⓜ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS t; 46 | 47 | SELECT 48 | 'aⓜⓜ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS t; 49 | 50 | SELECT 51 | 'aⓜⓩ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS t; 52 | 53 | SELECT 54 | 'aⓩⓩ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS f; 55 | 56 | SELECT 57 | 'aⓜ⓪' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS f; 58 | 59 | SELECT 60 | 'a0' ~ 'a[a-ⓩ]' AS f; 61 | 62 | SELECT 63 | 'aq' ~ 'a[a-ⓩ]' AS t; 64 | 65 | SELECT 66 | 'aⓜ' ~ 'a[a-ⓩ]' AS t; 67 | 68 | SELECT 69 | 'a⓪' ~ 'a[a-ⓩ]' AS f; 70 | 71 | -- Locale-dependent character classes 72 | SELECT 73 | 'aⒶⓜ⓪' ~ '[[:alpha:]][[:alpha:]][[:alpha:]][[:graph:]]' AS t; 74 | 75 | SELECT 76 | 'aⒶⓜ⓪' ~ '[[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]]' AS f; 77 | 78 | -- Locale-dependent character classes with high ranges 79 | SELECT 80 | 'aⒶⓜ⓪' ~ '[a-z][[:alpha:]][ⓐ-ⓩ][[:graph:]]' AS t; 81 | 82 | SELECT 83 | 'aⓜⒶ⓪' ~ '[a-z][[:alpha:]][ⓐ-ⓩ][[:graph:]]' AS f; 84 | 85 | SELECT 86 | 'aⓜⒶ⓪' ~ '[a-z][ⓐ-ⓩ][[:alpha:]][[:graph:]]' AS t; 87 | 88 | SELECT 89 | 'aⒶⓜ⓪' ~ '[a-z][ⓐ-ⓩ][[:alpha:]][[:graph:]]' AS f; 90 | 91 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/reindex_catalog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Check that system tables can be reindexed. 3 | -- 4 | -- Note that this test currently has to run without parallel tests 5 | -- being scheduled, as currently reindex catalog tables can cause 6 | -- deadlocks: 7 | -- 8 | -- * The lock upgrade between the ShareLock acquired for the reindex 9 | -- and RowExclusiveLock needed for pg_class/pg_index locks can 10 | -- trigger deadlocks. 11 | -- 12 | -- * The uniqueness checks performed when reindexing a unique/primary 13 | -- key index possibly need to wait for the transaction of a 14 | -- about-to-deleted row in pg_class to commit. That can cause 15 | -- deadlocks because, in contrast to user tables, locks on catalog 16 | -- tables are routinely released before commit - therefore the lock 17 | -- held for reindexing doesn't guarantee that no running transaction 18 | -- performed modifications in the table underlying the index. 19 | -- Check reindexing of whole tables 20 | REINDEX TABLE pg_class; 21 | 22 | -- mapped, non-shared, critical 23 | REINDEX TABLE pg_index; 24 | 25 | -- non-mapped, non-shared, critical 26 | REINDEX TABLE pg_operator; 27 | 28 | -- non-mapped, non-shared, critical 29 | REINDEX TABLE pg_database; 30 | 31 | -- mapped, shared, critical 32 | REINDEX TABLE pg_shdescription; 33 | 34 | -- mapped, shared non-critical 35 | -- Check that individual system indexes can be reindexed. That's a bit 36 | -- different from the entire-table case because reindex_relation 37 | -- treats e.g. pg_class special. 38 | REINDEX INDEX pg_class_oid_index; 39 | 40 | -- mapped, non-shared, critical 41 | REINDEX INDEX pg_class_relname_nsp_index; 42 | 43 | -- mapped, non-shared, non-critical 44 | REINDEX INDEX pg_index_indexrelid_index; 45 | 46 | -- non-mapped, non-shared, critical 47 | REINDEX INDEX pg_index_indrelid_index; 48 | 49 | -- non-mapped, non-shared, non-critical 50 | REINDEX INDEX pg_database_oid_index; 51 | 52 | -- mapped, shared, critical 53 | REINDEX INDEX pg_shdescription_o_c_index; 54 | 55 | -- mapped, shared, non-critical 56 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/sanity_check.sql: -------------------------------------------------------------------------------- 1 | VACUUM; 2 | 3 | -- 4 | -- sanity check, if we don't have indices the test will take years to 5 | -- complete. But skip TOAST relations (since they will have varying 6 | -- names depending on the current OID counter) as well as temp tables 7 | -- of other backends (to avoid timing-dependent behavior). 8 | -- 9 | -- temporarily disable fancy output, so catalog changes create less diff noise 10 | a \t 11 | SELECT 12 | relname, 13 | relhasindex 14 | FROM 15 | pg_class c 16 | LEFT JOIN pg_namespace n ON n.oid = relnamespace 17 | WHERE 18 | relkind IN ('r', 'p') 19 | AND (nspname ~ '^pg_temp_') IS NOT TRUE 20 | ORDER BY 21 | relname; 22 | 23 | -- restore normal output mode 24 | a \t 25 | -- 26 | -- another sanity check: every system catalog that has OIDs should have 27 | -- a unique index on OID. This ensures that the OIDs will be unique, 28 | -- even after the OID counter wraps around. 29 | -- We exclude non-system tables from the check by looking at nspname. 30 | -- 31 | SELECT 32 | relname, 33 | nspname 34 | FROM 35 | pg_class c 36 | LEFT JOIN pg_namespace n ON n.oid = relnamespace 37 | JOIN pg_attribute a ON (attrelid = c.oid 38 | AND attname = 'oid') 39 | WHERE 40 | relkind = 'r' 41 | AND c.oid < 16384 42 | AND ((nspname ~ '^pg_') IS NOT FALSE) 43 | AND NOT EXISTS ( 44 | SELECT 45 | 1 46 | FROM 47 | pg_index i 48 | WHERE 49 | indrelid = c.oid 50 | AND indkey[0] = a.attnum 51 | AND indnatts = 1 52 | AND indisunique 53 | AND indimmediate); 54 | 55 | -- check that relations without storage don't have relfilenode 56 | SELECT 57 | relname, 58 | relkind 59 | FROM 60 | pg_class 61 | WHERE 62 | relkind IN ('v', 'c', 'f', 'p', 'I') 63 | AND relfilenode <> 0; 64 | 65 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/security_label.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Test for facilities of security label 3 | -- 4 | -- initial setups 5 | SET client_min_messages TO 'warning'; 6 | 7 | DROP ROLE IF EXISTS regress_seclabel_user1; 8 | 9 | DROP ROLE IF EXISTS regress_seclabel_user2; 10 | 11 | RESET client_min_messages; 12 | 13 | CREATE USER regress_seclabel_user1 WITH CREATEROLE; 14 | 15 | CREATE USER regress_seclabel_user2; 16 | 17 | CREATE TABLE seclabel_tbl1 ( 18 | a int, 19 | b text 20 | ); 21 | 22 | CREATE TABLE seclabel_tbl2 ( 23 | x int, 24 | y text 25 | ); 26 | 27 | CREATE VIEW seclabel_view1 AS 28 | SELECT 29 | * 30 | FROM 31 | seclabel_tbl2; 32 | 33 | CREATE FUNCTION seclabel_four () 34 | RETURNS integer 35 | AS $$ 36 | SELECT 37 | 4 38 | $$ 39 | LANGUAGE sql; 40 | 41 | CREATE DOMAIN seclabel_domain AS text; 42 | 43 | ALTER TABLE seclabel_tbl1 OWNER TO regress_seclabel_user1; 44 | 45 | ALTER TABLE seclabel_tbl2 OWNER TO regress_seclabel_user2; 46 | 47 | -- 48 | -- Test of SECURITY LABEL statement without a plugin 49 | -- 50 | SECURITY LABEL ON TABLE seclabel_tbl1 IS 'classified'; 51 | 52 | -- fail 53 | SECURITY LABEL FOR 'dummy' ON TABLE seclabel_tbl1 IS 'classified'; 54 | 55 | -- fail 56 | SECURITY LABEL ON TABLE seclabel_tbl1 IS '...invalid label...'; 57 | 58 | -- fail 59 | SECURITY LABEL ON TABLE seclabel_tbl3 IS 'unclassified'; 60 | 61 | -- fail 62 | SECURITY LABEL ON ROLE regress_seclabel_user1 IS 'classified'; 63 | 64 | -- fail 65 | SECURITY LABEL FOR 'dummy' ON ROLE regress_seclabel_user1 IS 'classified'; 66 | 67 | -- fail 68 | SECURITY LABEL ON ROLE regress_seclabel_user1 IS '...invalid label...'; 69 | 70 | -- fail 71 | SECURITY LABEL ON ROLE regress_seclabel_user3 IS 'unclassified'; 72 | 73 | -- fail 74 | -- clean up objects 75 | DROP FUNCTION seclabel_four (); 76 | 77 | DROP DOMAIN seclabel_domain; 78 | 79 | DROP VIEW seclabel_view1; 80 | 81 | DROP TABLE seclabel_tbl1; 82 | 83 | DROP TABLE seclabel_tbl2; 84 | 85 | DROP USER regress_seclabel_user1; 86 | 87 | DROP USER regress_seclabel_user2; 88 | 89 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/select_distinct_on.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- SELECT_DISTINCT_ON 3 | -- 4 | SELECT DISTINCT ON (string4) 5 | string4, 6 | two, 7 | ten 8 | FROM 9 | tmp 10 | ORDER BY 11 | string4 USING <, 12 | two USING >, 13 | ten USING <; 14 | 15 | -- this will fail due to conflict of ordering requirements 16 | SELECT DISTINCT ON (string4, ten) 17 | string4, 18 | two, 19 | ten 20 | FROM 21 | tmp 22 | ORDER BY 23 | string4 USING <, 24 | two USING <, 25 | ten USING <; 26 | 27 | SELECT DISTINCT ON (string4, ten) 28 | string4, 29 | ten, 30 | two 31 | FROM 32 | tmp 33 | ORDER BY 34 | string4 USING <, 35 | ten USING >, 36 | two USING <; 37 | 38 | -- bug #5049: early 8.4.x chokes on volatile DISTINCT ON clauses 39 | SELECT DISTINCT ON (1) 40 | floor(random()) AS r, 41 | f1 42 | FROM 43 | int4_tbl 44 | ORDER BY 45 | 1, 46 | 2; 47 | 48 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/time.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- TIME 3 | -- 4 | CREATE TABLE TIME_TBL ( 5 | f1 time(2) 6 | ); 7 | 8 | INSERT INTO TIME_TBL 9 | VALUES ('00:00'); 10 | 11 | INSERT INTO TIME_TBL 12 | VALUES ('01:00'); 13 | 14 | -- as of 7.4, timezone spec should be accepted and ignored 15 | INSERT INTO TIME_TBL 16 | VALUES ('02:03 PST'); 17 | 18 | INSERT INTO TIME_TBL 19 | VALUES ('11:59 EDT'); 20 | 21 | INSERT INTO TIME_TBL 22 | VALUES ('12:00'); 23 | 24 | INSERT INTO TIME_TBL 25 | VALUES ('12:01'); 26 | 27 | INSERT INTO TIME_TBL 28 | VALUES ('23:59'); 29 | 30 | INSERT INTO TIME_TBL 31 | VALUES ('11:59:59.99 PM'); 32 | 33 | INSERT INTO TIME_TBL 34 | VALUES ('2003-03-07 15:36:39 America/New_York'); 35 | 36 | INSERT INTO TIME_TBL 37 | VALUES ('2003-07-07 15:36:39 America/New_York'); 38 | 39 | -- this should fail (the timezone offset is not known) 40 | INSERT INTO TIME_TBL 41 | VALUES ('15:36:39 America/New_York'); 42 | 43 | SELECT 44 | f1 AS "Time" 45 | FROM 46 | TIME_TBL; 47 | 48 | SELECT 49 | f1 AS "Three" 50 | FROM 51 | TIME_TBL 52 | WHERE 53 | f1 < '05:06:07'; 54 | 55 | SELECT 56 | f1 AS "Five" 57 | FROM 58 | TIME_TBL 59 | WHERE 60 | f1 > '05:06:07'; 61 | 62 | SELECT 63 | f1 AS "None" 64 | FROM 65 | TIME_TBL 66 | WHERE 67 | f1 < '00:00'; 68 | 69 | SELECT 70 | f1 AS "Eight" 71 | FROM 72 | TIME_TBL 73 | WHERE 74 | f1 >= '00:00'; 75 | 76 | -- 77 | -- TIME simple math 78 | -- 79 | -- We now make a distinction between time and intervals, 80 | -- and adding two times together makes no sense at all. 81 | -- Leave in one query to show that it is rejected, 82 | -- and do the rest of the testing in horology.sql 83 | -- where we do mixed-type arithmetic. - thomas 2000-12-02 84 | SELECT 85 | f1 + time '00:01' AS "Illegal" 86 | FROM 87 | TIME_TBL; 88 | 89 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/timetz.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- TIMETZ 3 | -- 4 | CREATE TABLE TIMETZ_TBL ( 5 | f1 time(2) with time zone 6 | ); 7 | 8 | INSERT INTO TIMETZ_TBL 9 | VALUES ('00:01 PDT'); 10 | 11 | INSERT INTO TIMETZ_TBL 12 | VALUES ('01:00 PDT'); 13 | 14 | INSERT INTO TIMETZ_TBL 15 | VALUES ('02:03 PDT'); 16 | 17 | INSERT INTO TIMETZ_TBL 18 | VALUES ('07:07 PST'); 19 | 20 | INSERT INTO TIMETZ_TBL 21 | VALUES ('08:08 EDT'); 22 | 23 | INSERT INTO TIMETZ_TBL 24 | VALUES ('11:59 PDT'); 25 | 26 | INSERT INTO TIMETZ_TBL 27 | VALUES ('12:00 PDT'); 28 | 29 | INSERT INTO TIMETZ_TBL 30 | VALUES ('12:01 PDT'); 31 | 32 | INSERT INTO TIMETZ_TBL 33 | VALUES ('23:59 PDT'); 34 | 35 | INSERT INTO TIMETZ_TBL 36 | VALUES ('11:59:59.99 PM PDT'); 37 | 38 | INSERT INTO TIMETZ_TBL 39 | VALUES ('2003-03-07 15:36:39 America/New_York'); 40 | 41 | INSERT INTO TIMETZ_TBL 42 | VALUES ('2003-07-07 15:36:39 America/New_York'); 43 | 44 | -- this should fail (the timezone offset is not known) 45 | INSERT INTO TIMETZ_TBL 46 | VALUES ('15:36:39 America/New_York'); 47 | 48 | SELECT 49 | f1 AS "Time TZ" 50 | FROM 51 | TIMETZ_TBL; 52 | 53 | SELECT 54 | f1 AS "Three" 55 | FROM 56 | TIMETZ_TBL 57 | WHERE 58 | f1 < '05:06:07-07'; 59 | 60 | SELECT 61 | f1 AS "Seven" 62 | FROM 63 | TIMETZ_TBL 64 | WHERE 65 | f1 > '05:06:07-07'; 66 | 67 | SELECT 68 | f1 AS "None" 69 | FROM 70 | TIMETZ_TBL 71 | WHERE 72 | f1 < '00:00-07'; 73 | 74 | SELECT 75 | f1 AS "Ten" 76 | FROM 77 | TIMETZ_TBL 78 | WHERE 79 | f1 >= '00:00-07'; 80 | 81 | -- 82 | -- TIME simple math 83 | -- 84 | -- We now make a distinction between time and intervals, 85 | -- and adding two times together makes no sense at all. 86 | -- Leave in one query to show that it is rejected, 87 | -- and do the rest of the testing in horology.sql 88 | -- where we do mixed-type arithmetic. - thomas 2000-12-02 89 | SELECT 90 | f1 + time with time zone '00:01' AS "Illegal" 91 | FROM 92 | TIMETZ_TBL; 93 | 94 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/varchar.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- VARCHAR 3 | -- 4 | CREATE TABLE VARCHAR_TBL ( 5 | f1 varchar(1) 6 | ); 7 | 8 | INSERT INTO VARCHAR_TBL (f1) 9 | VALUES ('a'); 10 | 11 | INSERT INTO VARCHAR_TBL (f1) 12 | VALUES ('A'); 13 | 14 | -- any of the following three input formats are acceptable 15 | INSERT INTO VARCHAR_TBL (f1) 16 | VALUES ('1'); 17 | 18 | INSERT INTO VARCHAR_TBL (f1) 19 | VALUES (2); 20 | 21 | INSERT INTO VARCHAR_TBL (f1) 22 | VALUES ('3'); 23 | 24 | -- zero-length char 25 | INSERT INTO VARCHAR_TBL (f1) 26 | VALUES (''); 27 | 28 | -- try varchar's of greater than 1 length 29 | INSERT INTO VARCHAR_TBL (f1) 30 | VALUES ('cd'); 31 | 32 | INSERT INTO VARCHAR_TBL (f1) 33 | VALUES ('c '); 34 | 35 | SELECT 36 | '' AS seven, 37 | * 38 | FROM 39 | VARCHAR_TBL; 40 | 41 | SELECT 42 | '' AS six, 43 | c.* 44 | FROM 45 | VARCHAR_TBL c 46 | WHERE 47 | c.f1 <> 'a'; 48 | 49 | SELECT 50 | '' AS one, 51 | c.* 52 | FROM 53 | VARCHAR_TBL c 54 | WHERE 55 | c.f1 = 'a'; 56 | 57 | SELECT 58 | '' AS five, 59 | c.* 60 | FROM 61 | VARCHAR_TBL c 62 | WHERE 63 | c.f1 < 'a'; 64 | 65 | SELECT 66 | '' AS six, 67 | c.* 68 | FROM 69 | VARCHAR_TBL c 70 | WHERE 71 | c.f1 <= 'a'; 72 | 73 | SELECT 74 | '' AS one, 75 | c.* 76 | FROM 77 | VARCHAR_TBL c 78 | WHERE 79 | c.f1 > 'a'; 80 | 81 | SELECT 82 | '' AS two, 83 | c.* 84 | FROM 85 | VARCHAR_TBL c 86 | WHERE 87 | c.f1 >= 'a'; 88 | 89 | DROP TABLE VARCHAR_TBL; 90 | 91 | -- 92 | -- Now test longer arrays of char 93 | -- 94 | CREATE TABLE VARCHAR_TBL ( 95 | f1 varchar(4) 96 | ); 97 | 98 | INSERT INTO VARCHAR_TBL (f1) 99 | VALUES ('a'); 100 | 101 | INSERT INTO VARCHAR_TBL (f1) 102 | VALUES ('ab'); 103 | 104 | INSERT INTO VARCHAR_TBL (f1) 105 | VALUES ('abcd'); 106 | 107 | INSERT INTO VARCHAR_TBL (f1) 108 | VALUES ('abcde'); 109 | 110 | INSERT INTO VARCHAR_TBL (f1) 111 | VALUES ('abcd '); 112 | 113 | SELECT 114 | '' AS four, 115 | * 116 | FROM 117 | VARCHAR_TBL; 118 | 119 | -------------------------------------------------------------------------------- /t/pg-test-files/expected/write_parallel.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PARALLEL 3 | -- 4 | -- Serializable isolation would disable parallel query, so explicitly use an 5 | -- arbitrary other level. 6 | BEGIN ISOLATION level REPEATABLE read; 7 | -- encourage use of parallel plans 8 | SET parallel_setup_cost = 0; 9 | SET parallel_tuple_cost = 0; 10 | SET min_parallel_table_scan_size = 0; 11 | SET max_parallel_workers_per_gather = 4; 12 | -- 13 | -- Test write operations that has an underlying query that is eligble 14 | -- for parallel plans 15 | -- 16 | EXPLAIN ( 17 | COSTS OFF 18 | ) CREATE TABLE parallel_write AS 19 | SELECT 20 | length(stringu1) 21 | FROM 22 | tenk1 23 | GROUP BY 24 | length(stringu1); 25 | CREATE TABLE parallel_write AS 26 | SELECT 27 | length(stringu1) 28 | FROM 29 | tenk1 30 | GROUP BY 31 | length(stringu1); 32 | DROP TABLE parallel_write; 33 | EXPLAIN ( 34 | COSTS OFF 35 | ) 36 | SELECT 37 | length(stringu1) INTO parallel_write 38 | FROM 39 | tenk1 40 | GROUP BY 41 | length(stringu1); 42 | SELECT 43 | length(stringu1) INTO parallel_write 44 | FROM 45 | tenk1 46 | GROUP BY 47 | length(stringu1); 48 | DROP TABLE parallel_write; 49 | EXPLAIN ( 50 | COSTS OFF 51 | ) CREATE MATERIALIZED VIEW parallel_mat_view AS 52 | SELECT 53 | length(stringu1) 54 | FROM 55 | tenk1 56 | GROUP BY 57 | length(stringu1); 58 | CREATE MATERIALIZED VIEW parallel_mat_view AS 59 | SELECT 60 | length(stringu1) 61 | FROM 62 | tenk1 63 | GROUP BY 64 | length(stringu1); 65 | DROP MATERIALIZED VIEW parallel_mat_view; 66 | PREPARE prep_stmt AS 67 | SELECT 68 | length(stringu1) 69 | FROM 70 | tenk1 71 | GROUP BY 72 | length(stringu1); 73 | EXPLAIN ( 74 | COSTS OFF 75 | ) CREATE TABLE parallel_write AS 76 | EXECUTE prep_stmt; 77 | CREATE TABLE parallel_write AS 78 | EXECUTE prep_stmt; 79 | DROP TABLE parallel_write; 80 | ROLLBACK; 81 | 82 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/async.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- ASYNC 3 | -- 4 | 5 | --Should work. Send a valid message via a valid channel name 6 | SELECT pg_notify('notify_async1','sample message1'); 7 | SELECT pg_notify('notify_async1',''); 8 | SELECT pg_notify('notify_async1',NULL); 9 | 10 | -- Should fail. Send a valid message via an invalid channel name 11 | SELECT pg_notify('','sample message1'); 12 | SELECT pg_notify(NULL,'sample message1'); 13 | SELECT pg_notify('notify_async_channel_name_too_long______________________________','sample_message1'); 14 | 15 | --Should work. Valid NOTIFY/LISTEN/UNLISTEN commands 16 | NOTIFY notify_async2; 17 | LISTEN notify_async2; 18 | UNLISTEN notify_async2; 19 | UNLISTEN *; 20 | 21 | -- Should return zero while there are no pending notifications. 22 | -- src/test/isolation/specs/async-notify.spec tests for actual usage. 23 | SELECT pg_notification_queue_usage(); 24 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/bitmapops.sql: -------------------------------------------------------------------------------- 1 | -- Test bitmap AND and OR 2 | 3 | 4 | -- Generate enough data that we can test the lossy bitmaps. 5 | 6 | -- There's 55 tuples per page in the table. 53 is just 7 | -- below 55, so that an index scan with qual a = constant 8 | -- will return at least one hit per page. 59 is just above 9 | -- 55, so that an index scan with qual b = constant will return 10 | -- hits on most but not all pages. 53 and 59 are prime, so that 11 | -- there's a maximum number of a,b combinations in the table. 12 | -- That allows us to test all the different combinations of 13 | -- lossy and non-lossy pages with the minimum amount of data 14 | 15 | CREATE TABLE bmscantest (a int, b int, t text); 16 | 17 | INSERT INTO bmscantest 18 | SELECT (r%53), (r%59), 'foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo' 19 | FROM generate_series(1,70000) r; 20 | 21 | CREATE INDEX i_bmtest_a ON bmscantest(a); 22 | CREATE INDEX i_bmtest_b ON bmscantest(b); 23 | 24 | -- We want to use bitmapscans. With default settings, the planner currently 25 | -- chooses a bitmap scan for the queries below anyway, but let's make sure. 26 | set enable_indexscan=false; 27 | set enable_seqscan=false; 28 | 29 | -- Lower work_mem to trigger use of lossy bitmaps 30 | set work_mem = 64; 31 | 32 | 33 | -- Test bitmap-and. 34 | SELECT count(*) FROM bmscantest WHERE a = 1 AND b = 1; 35 | 36 | -- Test bitmap-or. 37 | SELECT count(*) FROM bmscantest WHERE a = 1 OR b = 1; 38 | 39 | 40 | -- clean up 41 | DROP TABLE bmscantest; 42 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/char.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- CHAR 3 | -- 4 | 5 | -- fixed-length by value 6 | -- internally passed by value if <= 4 bytes in storage 7 | 8 | SELECT char 'c' = char 'c' AS true; 9 | 10 | -- 11 | -- Build a table for testing 12 | -- 13 | 14 | CREATE TABLE CHAR_TBL(f1 char); 15 | 16 | INSERT INTO CHAR_TBL (f1) VALUES ('a'); 17 | 18 | INSERT INTO CHAR_TBL (f1) VALUES ('A'); 19 | 20 | -- any of the following three input formats are acceptable 21 | INSERT INTO CHAR_TBL (f1) VALUES ('1'); 22 | 23 | INSERT INTO CHAR_TBL (f1) VALUES (2); 24 | 25 | INSERT INTO CHAR_TBL (f1) VALUES ('3'); 26 | 27 | -- zero-length char 28 | INSERT INTO CHAR_TBL (f1) VALUES (''); 29 | 30 | -- try char's of greater than 1 length 31 | INSERT INTO CHAR_TBL (f1) VALUES ('cd'); 32 | INSERT INTO CHAR_TBL (f1) VALUES ('c '); 33 | 34 | 35 | SELECT '' AS seven, * FROM CHAR_TBL; 36 | 37 | SELECT '' AS six, c.* 38 | FROM CHAR_TBL c 39 | WHERE c.f1 <> 'a'; 40 | 41 | SELECT '' AS one, c.* 42 | FROM CHAR_TBL c 43 | WHERE c.f1 = 'a'; 44 | 45 | SELECT '' AS five, c.* 46 | FROM CHAR_TBL c 47 | WHERE c.f1 < 'a'; 48 | 49 | SELECT '' AS six, c.* 50 | FROM CHAR_TBL c 51 | WHERE c.f1 <= 'a'; 52 | 53 | SELECT '' AS one, c.* 54 | FROM CHAR_TBL c 55 | WHERE c.f1 > 'a'; 56 | 57 | SELECT '' AS two, c.* 58 | FROM CHAR_TBL c 59 | WHERE c.f1 >= 'a'; 60 | 61 | DROP TABLE CHAR_TBL; 62 | 63 | -- 64 | -- Now test longer arrays of char 65 | -- 66 | 67 | CREATE TABLE CHAR_TBL(f1 char(4)); 68 | 69 | INSERT INTO CHAR_TBL (f1) VALUES ('a'); 70 | INSERT INTO CHAR_TBL (f1) VALUES ('ab'); 71 | INSERT INTO CHAR_TBL (f1) VALUES ('abcd'); 72 | INSERT INTO CHAR_TBL (f1) VALUES ('abcde'); 73 | INSERT INTO CHAR_TBL (f1) VALUES ('abcd '); 74 | 75 | SELECT '' AS four, * FROM CHAR_TBL; 76 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/circle.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- CIRCLE 3 | -- 4 | 5 | -- avoid bit-exact output here because operations may not be bit-exact. 6 | SET extra_float_digits = 0; 7 | 8 | CREATE TABLE CIRCLE_TBL (f1 circle); 9 | 10 | INSERT INTO CIRCLE_TBL VALUES ('<(5,1),3>'); 11 | 12 | INSERT INTO CIRCLE_TBL VALUES ('<(1,2),100>'); 13 | 14 | INSERT INTO CIRCLE_TBL VALUES ('1,3,5'); 15 | 16 | INSERT INTO CIRCLE_TBL VALUES ('((1,2),3)'); 17 | 18 | INSERT INTO CIRCLE_TBL VALUES ('<(100,200),10>'); 19 | 20 | INSERT INTO CIRCLE_TBL VALUES (' < ( 100 , 1 ) , 115 > '); 21 | 22 | INSERT INTO CIRCLE_TBL VALUES ('<(3,5),0>'); -- Zero radius 23 | 24 | INSERT INTO CIRCLE_TBL VALUES ('<(3,5),NaN>'); -- NaN radius 25 | 26 | -- bad values 27 | 28 | INSERT INTO CIRCLE_TBL VALUES ('<(-100,0),-100>'); 29 | 30 | INSERT INTO CIRCLE_TBL VALUES ('<(100,200),10'); 31 | 32 | INSERT INTO CIRCLE_TBL VALUES ('<(100,200),10> x'); 33 | 34 | INSERT INTO CIRCLE_TBL VALUES ('1abc,3,5'); 35 | 36 | INSERT INTO CIRCLE_TBL VALUES ('(3,(1,2),3)'); 37 | 38 | SELECT * FROM CIRCLE_TBL; 39 | 40 | SELECT '' AS six, center(f1) AS center 41 | FROM CIRCLE_TBL; 42 | 43 | SELECT '' AS six, radius(f1) AS radius 44 | FROM CIRCLE_TBL; 45 | 46 | SELECT '' AS six, diameter(f1) AS diameter 47 | FROM CIRCLE_TBL; 48 | 49 | SELECT '' AS two, f1 FROM CIRCLE_TBL WHERE radius(f1) < 5; 50 | 51 | SELECT '' AS four, f1 FROM CIRCLE_TBL WHERE diameter(f1) >= 10; 52 | 53 | SELECT '' as five, c1.f1 AS one, c2.f1 AS two, (c1.f1 <-> c2.f1) AS distance 54 | FROM CIRCLE_TBL c1, CIRCLE_TBL c2 55 | WHERE (c1.f1 < c2.f1) AND ((c1.f1 <-> c2.f1) > 0) 56 | ORDER BY distance, area(c1.f1), area(c2.f1); 57 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/comments.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- COMMENTS 3 | -- 4 | 5 | SELECT 'trailing' AS first; -- trailing single line 6 | SELECT /* embedded single line */ 'embedded' AS second; 7 | SELECT /* both embedded and trailing single line */ 'both' AS third; -- trailing single line 8 | 9 | SELECT 'before multi-line' AS fourth; 10 | /* This is an example of SQL which should not execute: 11 | * select 'multi-line'; 12 | */ 13 | SELECT 'after multi-line' AS fifth; 14 | 15 | -- 16 | -- Nested comments 17 | -- 18 | 19 | /* 20 | SELECT 'trailing' as x1; -- inside block comment 21 | */ 22 | 23 | /* This block comment surrounds a query which itself has a block comment... 24 | SELECT /* embedded single line */ 'embedded' AS x2; 25 | */ 26 | 27 | SELECT -- continued after the following block comments... 28 | /* Deeply nested comment. 29 | This includes a single apostrophe to make sure we aren't decoding this part as a string. 30 | SELECT 'deep nest' AS n1; 31 | /* Second level of nesting... 32 | SELECT 'deeper nest' as n2; 33 | /* Third level of nesting... 34 | SELECT 'deepest nest' as n3; 35 | */ 36 | Hoo boy. Still two deep... 37 | */ 38 | Now just one deep... 39 | */ 40 | 'deeply nested example' AS sixth; 41 | 42 | /* and this is the end of the file */ 43 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/conversion.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- create user defined conversion 3 | -- 4 | CREATE USER regress_conversion_user WITH NOCREATEDB NOCREATEROLE; 5 | SET SESSION AUTHORIZATION regress_conversion_user; 6 | CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; 7 | -- 8 | -- cannot make same name conversion in same schema 9 | -- 10 | CREATE CONVERSION myconv FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; 11 | -- 12 | -- create default conversion with qualified name 13 | -- 14 | CREATE DEFAULT CONVERSION public.mydef FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; 15 | -- 16 | -- cannot make default conversion with same schema/for_encoding/to_encoding 17 | -- 18 | CREATE DEFAULT CONVERSION public.mydef2 FOR 'LATIN1' TO 'UTF8' FROM iso8859_1_to_utf8; 19 | -- test comments 20 | COMMENT ON CONVERSION myconv_bad IS 'foo'; 21 | COMMENT ON CONVERSION myconv IS 'bar'; 22 | COMMENT ON CONVERSION myconv IS NULL; 23 | -- 24 | -- drop user defined conversion 25 | -- 26 | DROP CONVERSION myconv; 27 | DROP CONVERSION mydef; 28 | -- 29 | -- Note: the built-in conversions are exercised in opr_sanity.sql, 30 | -- so there's no need to do that here. 31 | -- 32 | -- 33 | -- return to the super user 34 | -- 35 | RESET SESSION AUTHORIZATION; 36 | DROP USER regress_conversion_user; 37 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/create_cast.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- CREATE_CAST 3 | -- 4 | 5 | -- Create some types to test with 6 | CREATE TYPE casttesttype; 7 | 8 | CREATE FUNCTION casttesttype_in(cstring) 9 | RETURNS casttesttype 10 | AS 'textin' 11 | LANGUAGE internal STRICT IMMUTABLE; 12 | CREATE FUNCTION casttesttype_out(casttesttype) 13 | RETURNS cstring 14 | AS 'textout' 15 | LANGUAGE internal STRICT IMMUTABLE; 16 | 17 | CREATE TYPE casttesttype ( 18 | internallength = variable, 19 | input = casttesttype_in, 20 | output = casttesttype_out, 21 | alignment = int4 22 | ); 23 | 24 | -- a dummy function to test with 25 | CREATE FUNCTION casttestfunc(casttesttype) RETURNS int4 LANGUAGE SQL AS 26 | $$ SELECT 1; $$; 27 | 28 | SELECT casttestfunc('foo'::text); -- fails, as there's no cast 29 | 30 | -- Try binary coercion cast 31 | CREATE CAST (text AS casttesttype) WITHOUT FUNCTION; 32 | SELECT casttestfunc('foo'::text); -- doesn't work, as the cast is explicit 33 | SELECT casttestfunc('foo'::text::casttesttype); -- should work 34 | DROP CAST (text AS casttesttype); -- cleanup 35 | 36 | -- Try IMPLICIT binary coercion cast 37 | CREATE CAST (text AS casttesttype) WITHOUT FUNCTION AS IMPLICIT; 38 | SELECT casttestfunc('foo'::text); -- Should work now 39 | 40 | -- Try I/O conversion cast. 41 | SELECT 1234::int4::casttesttype; -- No cast yet, should fail 42 | 43 | CREATE CAST (int4 AS casttesttype) WITH INOUT; 44 | SELECT 1234::int4::casttesttype; -- Should work now 45 | 46 | DROP CAST (int4 AS casttesttype); 47 | 48 | -- Try cast with a function 49 | 50 | CREATE FUNCTION int4_casttesttype(int4) RETURNS casttesttype LANGUAGE SQL AS 51 | $$ SELECT ('foo'::text || $1::text)::casttesttype; $$; 52 | 53 | CREATE CAST (int4 AS casttesttype) WITH FUNCTION int4_casttesttype(int4) AS IMPLICIT; 54 | SELECT 1234::int4::casttesttype; -- Should work now 55 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/dbsize.sql: -------------------------------------------------------------------------------- 1 | SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM 2 | (VALUES (10::bigint), (1000::bigint), (1000000::bigint), 3 | (1000000000::bigint), (1000000000000::bigint), 4 | (1000000000000000::bigint)) x(size); 5 | 6 | SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM 7 | (VALUES (10::numeric), (1000::numeric), (1000000::numeric), 8 | (1000000000::numeric), (1000000000000::numeric), 9 | (1000000000000000::numeric), 10 | (10.5::numeric), (1000.5::numeric), (1000000.5::numeric), 11 | (1000000000.5::numeric), (1000000000000.5::numeric), 12 | (1000000000000000.5::numeric)) x(size); 13 | 14 | SELECT size, pg_size_bytes(size) FROM 15 | (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), 16 | ('1TB'), ('3000 TB'), ('1e6 MB')) x(size); 17 | 18 | -- case-insensitive units are supported 19 | SELECT size, pg_size_bytes(size) FROM 20 | (VALUES ('1'), ('123bYteS'), ('1kb'), ('1mb'), (' 1 Gb'), ('1.5 gB '), 21 | ('1tb'), ('3000 tb'), ('1e6 mb')) x(size); 22 | 23 | -- negative numbers are supported 24 | SELECT size, pg_size_bytes(size) FROM 25 | (VALUES ('-1'), ('-123bytes'), ('-1kb'), ('-1mb'), (' -1 Gb'), ('-1.5 gB '), 26 | ('-1tb'), ('-3000 TB'), ('-10e-1 MB')) x(size); 27 | 28 | -- different cases with allowed points 29 | SELECT size, pg_size_bytes(size) FROM 30 | (VALUES ('-1.'), ('-1.kb'), ('-1. kb'), ('-0. gb'), 31 | ('-.1'), ('-.1kb'), ('-.1 kb'), ('-.0 gb')) x(size); 32 | 33 | -- invalid inputs 34 | SELECT pg_size_bytes('1 AB'); 35 | SELECT pg_size_bytes('1 AB A'); 36 | SELECT pg_size_bytes('1 AB A '); 37 | SELECT pg_size_bytes('9223372036854775807.9'); 38 | SELECT pg_size_bytes('1e100'); 39 | SELECT pg_size_bytes('1e1000000000000000000'); 40 | SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported 41 | SELECT pg_size_bytes(''); 42 | 43 | SELECT pg_size_bytes('kb'); 44 | SELECT pg_size_bytes('..'); 45 | SELECT pg_size_bytes('-.'); 46 | SELECT pg_size_bytes('-.kb'); 47 | SELECT pg_size_bytes('-. kb'); 48 | 49 | SELECT pg_size_bytes('.+912'); 50 | SELECT pg_size_bytes('+912+ kB'); 51 | SELECT pg_size_bytes('++123 kB'); 52 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/delete.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE delete_test ( 2 | id SERIAL PRIMARY KEY, 3 | a INT, 4 | b text 5 | ); 6 | 7 | INSERT INTO delete_test (a) VALUES (10); 8 | INSERT INTO delete_test (a, b) VALUES (50, repeat('x', 10000)); 9 | INSERT INTO delete_test (a) VALUES (100); 10 | 11 | -- allow an alias to be specified for DELETE's target table 12 | DELETE FROM delete_test AS dt WHERE dt.a > 75; 13 | 14 | -- if an alias is specified, don't allow the original table name 15 | -- to be referenced 16 | DELETE FROM delete_test dt WHERE delete_test.a > 25; 17 | 18 | SELECT id, a, char_length(b) FROM delete_test; 19 | 20 | -- delete a row with a TOASTed value 21 | DELETE FROM delete_test WHERE a > 25; 22 | 23 | SELECT id, a, char_length(b) FROM delete_test; 24 | 25 | DROP TABLE delete_test; 26 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/drop_operator.sql: -------------------------------------------------------------------------------- 1 | CREATE OPERATOR === ( 2 | PROCEDURE = int8eq, 3 | LEFTARG = bigint, 4 | RIGHTARG = bigint, 5 | COMMUTATOR = === 6 | ); 7 | 8 | CREATE OPERATOR !== ( 9 | PROCEDURE = int8ne, 10 | LEFTARG = bigint, 11 | RIGHTARG = bigint, 12 | NEGATOR = ===, 13 | COMMUTATOR = !== 14 | ); 15 | 16 | DROP OPERATOR !==(bigint, bigint); 17 | 18 | SELECT ctid, oprcom 19 | FROM pg_catalog.pg_operator fk 20 | WHERE oprcom != 0 AND 21 | NOT EXISTS(SELECT 1 FROM pg_catalog.pg_operator pk WHERE pk.oid = fk.oprcom); 22 | 23 | SELECT ctid, oprnegate 24 | FROM pg_catalog.pg_operator fk 25 | WHERE oprnegate != 0 AND 26 | NOT EXISTS(SELECT 1 FROM pg_catalog.pg_operator pk WHERE pk.oid = fk.oprnegate); 27 | 28 | DROP OPERATOR ===(bigint, bigint); 29 | 30 | CREATE OPERATOR <| ( 31 | PROCEDURE = int8lt, 32 | LEFTARG = bigint, 33 | RIGHTARG = bigint 34 | ); 35 | 36 | CREATE OPERATOR |> ( 37 | PROCEDURE = int8gt, 38 | LEFTARG = bigint, 39 | RIGHTARG = bigint, 40 | NEGATOR = <|, 41 | COMMUTATOR = <| 42 | ); 43 | 44 | DROP OPERATOR |>(bigint, bigint); 45 | 46 | SELECT ctid, oprcom 47 | FROM pg_catalog.pg_operator fk 48 | WHERE oprcom != 0 AND 49 | NOT EXISTS(SELECT 1 FROM pg_catalog.pg_operator pk WHERE pk.oid = fk.oprcom); 50 | 51 | SELECT ctid, oprnegate 52 | FROM pg_catalog.pg_operator fk 53 | WHERE oprnegate != 0 AND 54 | NOT EXISTS(SELECT 1 FROM pg_catalog.pg_operator pk WHERE pk.oid = fk.oprnegate); 55 | 56 | DROP OPERATOR <|(bigint, bigint); 57 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/expressions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- expression evaluated tests that don't fit into a more specific file 3 | -- 4 | 5 | -- 6 | -- Tests for SQLVAlueFunction 7 | -- 8 | 9 | 10 | -- current_date (always matches because of transactional behaviour) 11 | SELECT date(now())::text = current_date::text; 12 | 13 | 14 | -- current_time / localtime 15 | SELECT now()::timetz::text = current_time::text; 16 | SELECT now()::time::text = localtime::text; 17 | 18 | -- current_timestamp / localtimestamp (always matches because of transactional behaviour) 19 | SELECT current_timestamp = NOW(); 20 | -- precision 21 | SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text); 22 | -- localtimestamp 23 | SELECT now()::timestamp::text = localtimestamp::text; 24 | 25 | -- current_role/user/user is tested in rolnames.sql 26 | 27 | -- current database / catalog 28 | SELECT current_catalog = current_database(); 29 | 30 | -- current_schema 31 | SELECT current_schema; 32 | SET search_path = 'notme'; 33 | SELECT current_schema; 34 | SET search_path = 'pg_catalog'; 35 | SELECT current_schema; 36 | RESET search_path; 37 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/gin.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Test GIN indexes. 3 | -- 4 | -- There are other tests to test different GIN opclassed. This is for testing 5 | -- GIN itself. 6 | 7 | -- Create and populate a test table with a GIN index. 8 | create table gin_test_tbl(i int4[]) with (autovacuum_enabled = off); 9 | create index gin_test_idx on gin_test_tbl using gin (i) 10 | with (fastupdate = on, gin_pending_list_limit = 4096); 11 | insert into gin_test_tbl select array[1, 2, g] from generate_series(1, 20000) g; 12 | insert into gin_test_tbl select array[1, 3, g] from generate_series(1, 1000) g; 13 | 14 | select gin_clean_pending_list('gin_test_idx')>10 as many; -- flush the fastupdate buffers 15 | 16 | insert into gin_test_tbl select array[3, 1, g] from generate_series(1, 1000) g; 17 | 18 | vacuum gin_test_tbl; -- flush the fastupdate buffers 19 | 20 | select gin_clean_pending_list('gin_test_idx'); -- nothing to flush 21 | 22 | -- Test vacuuming 23 | delete from gin_test_tbl where i @> array[2]; 24 | vacuum gin_test_tbl; 25 | 26 | -- Disable fastupdate, and do more insertions. With fastupdate enabled, most 27 | -- insertions (by flushing the list pages) cause page splits. Without 28 | -- fastupdate, we get more churn in the GIN data leaf pages, and exercise the 29 | -- recompression codepaths. 30 | alter index gin_test_idx set (fastupdate = off); 31 | 32 | insert into gin_test_tbl select array[1, 2, g] from generate_series(1, 1000) g; 33 | insert into gin_test_tbl select array[1, 3, g] from generate_series(1, 1000) g; 34 | 35 | delete from gin_test_tbl where i @> array[2]; 36 | vacuum gin_test_tbl; 37 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/hs_primary_extremes.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_primary_extremes.sql 5 | -- 6 | 7 | drop table if exists hs_extreme; 8 | create table hs_extreme (col1 integer); 9 | 10 | CREATE OR REPLACE FUNCTION hs_subxids (n integer) 11 | RETURNS void 12 | LANGUAGE plpgsql 13 | AS $$ 14 | BEGIN 15 | IF n <= 0 THEN RETURN; END IF; 16 | INSERT INTO hs_extreme VALUES (n); 17 | PERFORM hs_subxids(n - 1); 18 | RETURN; 19 | EXCEPTION WHEN raise_exception THEN NULL; END; 20 | $$; 21 | 22 | BEGIN; 23 | SELECT hs_subxids(257); 24 | ROLLBACK; 25 | BEGIN; 26 | SELECT hs_subxids(257); 27 | COMMIT; 28 | 29 | set client_min_messages = 'warning'; 30 | 31 | CREATE OR REPLACE FUNCTION hs_locks_create (n integer) 32 | RETURNS void 33 | LANGUAGE plpgsql 34 | AS $$ 35 | BEGIN 36 | IF n <= 0 THEN 37 | CHECKPOINT; 38 | RETURN; 39 | END IF; 40 | EXECUTE 'CREATE TABLE hs_locks_' || n::text || ' ()'; 41 | PERFORM hs_locks_create(n - 1); 42 | RETURN; 43 | EXCEPTION WHEN raise_exception THEN NULL; END; 44 | $$; 45 | 46 | CREATE OR REPLACE FUNCTION hs_locks_drop (n integer) 47 | RETURNS void 48 | LANGUAGE plpgsql 49 | AS $$ 50 | BEGIN 51 | IF n <= 0 THEN 52 | CHECKPOINT; 53 | RETURN; 54 | END IF; 55 | EXECUTE 'DROP TABLE IF EXISTS hs_locks_' || n::text; 56 | PERFORM hs_locks_drop(n - 1); 57 | RETURN; 58 | EXCEPTION WHEN raise_exception THEN NULL; END; 59 | $$; 60 | 61 | BEGIN; 62 | SELECT hs_locks_drop(257); 63 | SELECT hs_locks_create(257); 64 | SELECT count(*) > 257 FROM pg_locks; 65 | ROLLBACK; 66 | BEGIN; 67 | SELECT hs_locks_drop(257); 68 | SELECT hs_locks_create(257); 69 | SELECT count(*) > 257 FROM pg_locks; 70 | COMMIT; 71 | SELECT hs_locks_drop(257); 72 | 73 | SELECT pg_switch_wal(); 74 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/hs_primary_setup.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_primary_setup.sql 5 | -- 6 | 7 | drop table if exists hs1; 8 | create table hs1 (col1 integer primary key); 9 | insert into hs1 values (1); 10 | 11 | drop table if exists hs2; 12 | create table hs2 (col1 integer primary key); 13 | insert into hs2 values (12); 14 | insert into hs2 values (13); 15 | 16 | drop table if exists hs3; 17 | create table hs3 (col1 integer primary key); 18 | insert into hs3 values (113); 19 | insert into hs3 values (114); 20 | insert into hs3 values (115); 21 | 22 | DROP sequence if exists hsseq; 23 | create sequence hsseq; 24 | 25 | SELECT pg_switch_wal(); 26 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/hs_standby_check.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_standby_check.sql 5 | -- 6 | 7 | -- 8 | -- If the query below returns false then all other tests will fail after it. 9 | -- 10 | select case pg_is_in_recovery() when false then 11 | 'These tests are intended only for execution on a standby server that is reading ' || 12 | 'WAL from a server upon which the regression database is already created and into ' || 13 | 'which src/test/regress/sql/hs_primary_setup.sql has been run' 14 | else 15 | 'Tests are running on a standby server during recovery' 16 | end; 17 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/hs_standby_disallowed.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_standby_disallowed.sql 5 | -- 6 | 7 | SET transaction_read_only = off; 8 | 9 | begin transaction read write; 10 | commit; 11 | 12 | -- SELECT 13 | 14 | select * from hs1 FOR SHARE; 15 | select * from hs1 FOR UPDATE; 16 | 17 | -- DML 18 | BEGIN; 19 | insert into hs1 values (37); 20 | ROLLBACK; 21 | BEGIN; 22 | delete from hs1 where col1 = 1; 23 | ROLLBACK; 24 | BEGIN; 25 | update hs1 set col1 = NULL where col1 > 0; 26 | ROLLBACK; 27 | BEGIN; 28 | truncate hs3; 29 | ROLLBACK; 30 | 31 | -- DDL 32 | 33 | create temporary table hstemp1 (col1 integer); 34 | BEGIN; 35 | drop table hs2; 36 | ROLLBACK; 37 | BEGIN; 38 | create table hs4 (col1 integer); 39 | ROLLBACK; 40 | 41 | -- Sequences 42 | 43 | SELECT nextval('hsseq'); 44 | 45 | -- Two-phase commit transaction stuff 46 | 47 | BEGIN; 48 | SELECT count(*) FROM hs1; 49 | PREPARE TRANSACTION 'foobar'; 50 | ROLLBACK; 51 | BEGIN; 52 | SELECT count(*) FROM hs1; 53 | COMMIT PREPARED 'foobar'; 54 | ROLLBACK; 55 | 56 | BEGIN; 57 | SELECT count(*) FROM hs1; 58 | PREPARE TRANSACTION 'foobar'; 59 | ROLLBACK PREPARED 'foobar'; 60 | ROLLBACK; 61 | 62 | BEGIN; 63 | SELECT count(*) FROM hs1; 64 | ROLLBACK PREPARED 'foobar'; 65 | ROLLBACK; 66 | 67 | 68 | -- Locks 69 | BEGIN; 70 | LOCK hs1; 71 | COMMIT; 72 | BEGIN; 73 | LOCK hs1 IN SHARE UPDATE EXCLUSIVE MODE; 74 | COMMIT; 75 | BEGIN; 76 | LOCK hs1 IN SHARE MODE; 77 | COMMIT; 78 | BEGIN; 79 | LOCK hs1 IN SHARE ROW EXCLUSIVE MODE; 80 | COMMIT; 81 | BEGIN; 82 | LOCK hs1 IN EXCLUSIVE MODE; 83 | COMMIT; 84 | BEGIN; 85 | LOCK hs1 IN ACCESS EXCLUSIVE MODE; 86 | COMMIT; 87 | 88 | -- Listen 89 | listen a; 90 | notify a; 91 | 92 | -- disallowed commands 93 | 94 | ANALYZE hs1; 95 | 96 | VACUUM hs2; 97 | 98 | CLUSTER hs2 using hs1_pkey; 99 | 100 | REINDEX TABLE hs2; 101 | 102 | REVOKE SELECT ON hs1 FROM PUBLIC; 103 | GRANT SELECT ON hs1 TO PUBLIC; 104 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/hs_standby_functions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Hot Standby tests 3 | -- 4 | -- hs_standby_functions.sql 5 | -- 6 | 7 | -- should fail 8 | select txid_current(); 9 | 10 | select length(txid_current_snapshot()::text) >= 4; 11 | 12 | select pg_start_backup('should fail'); 13 | select pg_switch_wal(); 14 | select pg_stop_backup(); 15 | 16 | -- should return no rows 17 | select * from pg_prepared_xacts; 18 | 19 | -- just the startup process 20 | select locktype, virtualxid, virtualtransaction, mode, granted 21 | from pg_locks where virtualxid = '1/1'; 22 | 23 | -- suicide is painless 24 | select pg_cancel_backend(pg_backend_pid()); 25 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/init_privs.sql: -------------------------------------------------------------------------------- 1 | -- Test initial privileges 2 | 3 | -- There should always be some initial privileges, set up by initdb 4 | SELECT count(*) > 0 FROM pg_init_privs; 5 | 6 | -- Intentionally include some non-initial privs for pg_dump to dump out 7 | GRANT SELECT ON pg_proc TO CURRENT_USER; 8 | GRANT SELECT (prosrc) ON pg_proc TO CURRENT_USER; 9 | 10 | GRANT SELECT (rolname, rolsuper) ON pg_authid TO CURRENT_USER; 11 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/line.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- LINE 3 | -- Infinite lines 4 | -- 5 | 6 | --DROP TABLE LINE_TBL; 7 | CREATE TABLE LINE_TBL (s line); 8 | 9 | INSERT INTO LINE_TBL VALUES ('{0,-1,5}'); -- A == 0 10 | INSERT INTO LINE_TBL VALUES ('{1,0,5}'); -- B == 0 11 | INSERT INTO LINE_TBL VALUES ('{0,3,0}'); -- A == C == 0 12 | INSERT INTO LINE_TBL VALUES (' (0,0), (6,6)'); 13 | INSERT INTO LINE_TBL VALUES ('10,-10 ,-5,-4'); 14 | INSERT INTO LINE_TBL VALUES ('[-1e6,2e2,3e5, -4e1]'); 15 | 16 | INSERT INTO LINE_TBL VALUES ('{3,NaN,5}'); 17 | INSERT INTO LINE_TBL VALUES ('{NaN,NaN,NaN}'); 18 | 19 | -- horizontal 20 | INSERT INTO LINE_TBL VALUES ('[(1,3),(2,3)]'); 21 | -- vertical 22 | INSERT INTO LINE_TBL VALUES (line(point '(3,1)', point '(3,2)')); 23 | 24 | -- bad values for parser testing 25 | INSERT INTO LINE_TBL VALUES ('{}'); 26 | INSERT INTO LINE_TBL VALUES ('{0'); 27 | INSERT INTO LINE_TBL VALUES ('{0,0}'); 28 | INSERT INTO LINE_TBL VALUES ('{0,0,1'); 29 | INSERT INTO LINE_TBL VALUES ('{0,0,1}'); 30 | INSERT INTO LINE_TBL VALUES ('{0,0,1} x'); 31 | INSERT INTO LINE_TBL VALUES ('(3asdf,2 ,3,4r2)'); 32 | INSERT INTO LINE_TBL VALUES ('[1,2,3, 4'); 33 | INSERT INTO LINE_TBL VALUES ('[(,2),(3,4)]'); 34 | INSERT INTO LINE_TBL VALUES ('[(1,2),(3,4)'); 35 | INSERT INTO LINE_TBL VALUES ('[(1,2),(1,2)]'); 36 | 37 | INSERT INTO LINE_TBL VALUES (line(point '(1,0)', point '(1,0)')); 38 | 39 | select * from LINE_TBL; 40 | 41 | select '{nan, 1, nan}'::line = '{nan, 1, nan}'::line as true, 42 | '{nan, 1, nan}'::line = '{nan, 2, nan}'::line as false; 43 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/lseg.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- LSEG 3 | -- Line segments 4 | -- 5 | 6 | --DROP TABLE LSEG_TBL; 7 | CREATE TABLE LSEG_TBL (s lseg); 8 | 9 | INSERT INTO LSEG_TBL VALUES ('[(1,2),(3,4)]'); 10 | INSERT INTO LSEG_TBL VALUES ('(0,0),(6,6)'); 11 | INSERT INTO LSEG_TBL VALUES ('10,-10 ,-3,-4'); 12 | INSERT INTO LSEG_TBL VALUES ('[-1e6,2e2,3e5, -4e1]'); 13 | INSERT INTO LSEG_TBL VALUES (lseg(point(11, 22), point(33,44))); 14 | INSERT INTO LSEG_TBL VALUES ('[(-10,2),(-10,3)]'); -- vertical 15 | INSERT INTO LSEG_TBL VALUES ('[(0,-20),(30,-20)]'); -- horizontal 16 | INSERT INTO LSEG_TBL VALUES ('[(NaN,1),(NaN,90)]'); -- NaN 17 | 18 | -- bad values for parser testing 19 | INSERT INTO LSEG_TBL VALUES ('(3asdf,2 ,3,4r2)'); 20 | INSERT INTO LSEG_TBL VALUES ('[1,2,3, 4'); 21 | INSERT INTO LSEG_TBL VALUES ('[(,2),(3,4)]'); 22 | INSERT INTO LSEG_TBL VALUES ('[(1,2),(3,4)'); 23 | 24 | select * from LSEG_TBL; 25 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/macaddr.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- macaddr 3 | -- 4 | 5 | CREATE TABLE macaddr_data (a int, b macaddr); 6 | 7 | INSERT INTO macaddr_data VALUES (1, '08:00:2b:01:02:03'); 8 | INSERT INTO macaddr_data VALUES (2, '08-00-2b-01-02-03'); 9 | INSERT INTO macaddr_data VALUES (3, '08002b:010203'); 10 | INSERT INTO macaddr_data VALUES (4, '08002b-010203'); 11 | INSERT INTO macaddr_data VALUES (5, '0800.2b01.0203'); 12 | INSERT INTO macaddr_data VALUES (6, '0800-2b01-0203'); 13 | INSERT INTO macaddr_data VALUES (7, '08002b010203'); 14 | INSERT INTO macaddr_data VALUES (8, '0800:2b01:0203'); -- invalid 15 | INSERT INTO macaddr_data VALUES (9, 'not even close'); -- invalid 16 | 17 | INSERT INTO macaddr_data VALUES (10, '08:00:2b:01:02:04'); 18 | INSERT INTO macaddr_data VALUES (11, '08:00:2b:01:02:02'); 19 | INSERT INTO macaddr_data VALUES (12, '08:00:2a:01:02:03'); 20 | INSERT INTO macaddr_data VALUES (13, '08:00:2c:01:02:03'); 21 | INSERT INTO macaddr_data VALUES (14, '08:00:2a:01:02:04'); 22 | 23 | SELECT * FROM macaddr_data; 24 | 25 | CREATE INDEX macaddr_data_btree ON macaddr_data USING btree (b); 26 | CREATE INDEX macaddr_data_hash ON macaddr_data USING hash (b); 27 | 28 | SELECT a, b, trunc(b) FROM macaddr_data ORDER BY 2, 1; 29 | 30 | SELECT b < '08:00:2b:01:02:04' FROM macaddr_data WHERE a = 1; -- true 31 | SELECT b > '08:00:2b:01:02:04' FROM macaddr_data WHERE a = 1; -- false 32 | SELECT b > '08:00:2b:01:02:03' FROM macaddr_data WHERE a = 1; -- false 33 | SELECT b <= '08:00:2b:01:02:04' FROM macaddr_data WHERE a = 1; -- true 34 | SELECT b >= '08:00:2b:01:02:04' FROM macaddr_data WHERE a = 1; -- false 35 | SELECT b = '08:00:2b:01:02:03' FROM macaddr_data WHERE a = 1; -- true 36 | SELECT b <> '08:00:2b:01:02:04' FROM macaddr_data WHERE a = 1; -- true 37 | SELECT b <> '08:00:2b:01:02:03' FROM macaddr_data WHERE a = 1; -- false 38 | 39 | SELECT ~b FROM macaddr_data; 40 | SELECT b & '00:00:00:ff:ff:ff' FROM macaddr_data; 41 | SELECT b | '01:02:03:04:05:06' FROM macaddr_data; 42 | 43 | DROP TABLE macaddr_data; 44 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/namespace.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Regression tests for schemas (namespaces) 3 | -- 4 | 5 | CREATE SCHEMA test_ns_schema_1 6 | CREATE UNIQUE INDEX abc_a_idx ON abc (a) 7 | 8 | CREATE VIEW abc_view AS 9 | SELECT a+1 AS a, b+1 AS b FROM abc 10 | 11 | CREATE TABLE abc ( 12 | a serial, 13 | b int UNIQUE 14 | ); 15 | 16 | -- verify that the objects were created 17 | SELECT COUNT(*) FROM pg_class WHERE relnamespace = 18 | (SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1'); 19 | 20 | INSERT INTO test_ns_schema_1.abc DEFAULT VALUES; 21 | INSERT INTO test_ns_schema_1.abc DEFAULT VALUES; 22 | INSERT INTO test_ns_schema_1.abc DEFAULT VALUES; 23 | 24 | SELECT * FROM test_ns_schema_1.abc; 25 | SELECT * FROM test_ns_schema_1.abc_view; 26 | 27 | ALTER SCHEMA test_ns_schema_1 RENAME TO test_ns_schema_renamed; 28 | SELECT COUNT(*) FROM pg_class WHERE relnamespace = 29 | (SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_1'); 30 | 31 | -- test IF NOT EXISTS cases 32 | CREATE SCHEMA test_ns_schema_renamed; -- fail, already exists 33 | CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed; -- ok with notice 34 | CREATE SCHEMA IF NOT EXISTS test_ns_schema_renamed -- fail, disallowed 35 | CREATE TABLE abc ( 36 | a serial, 37 | b int UNIQUE 38 | ); 39 | 40 | DROP SCHEMA test_ns_schema_renamed CASCADE; 41 | 42 | -- verify that the objects were dropped 43 | SELECT COUNT(*) FROM pg_class WHERE relnamespace = 44 | (SELECT oid FROM pg_namespace WHERE nspname = 'test_ns_schema_renamed'); 45 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/oid.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- OID 3 | -- 4 | 5 | CREATE TABLE OID_TBL(f1 oid); 6 | 7 | INSERT INTO OID_TBL(f1) VALUES ('1234'); 8 | INSERT INTO OID_TBL(f1) VALUES ('1235'); 9 | INSERT INTO OID_TBL(f1) VALUES ('987'); 10 | INSERT INTO OID_TBL(f1) VALUES ('-1040'); 11 | INSERT INTO OID_TBL(f1) VALUES ('99999999'); 12 | INSERT INTO OID_TBL(f1) VALUES ('5 '); 13 | INSERT INTO OID_TBL(f1) VALUES (' 10 '); 14 | -- leading/trailing hard tab is also allowed 15 | INSERT INTO OID_TBL(f1) VALUES (' 15 '); 16 | 17 | -- bad inputs 18 | INSERT INTO OID_TBL(f1) VALUES (''); 19 | INSERT INTO OID_TBL(f1) VALUES (' '); 20 | INSERT INTO OID_TBL(f1) VALUES ('asdfasd'); 21 | INSERT INTO OID_TBL(f1) VALUES ('99asdfasd'); 22 | INSERT INTO OID_TBL(f1) VALUES ('5 d'); 23 | INSERT INTO OID_TBL(f1) VALUES (' 5d'); 24 | INSERT INTO OID_TBL(f1) VALUES ('5 5'); 25 | INSERT INTO OID_TBL(f1) VALUES (' - 500'); 26 | INSERT INTO OID_TBL(f1) VALUES ('32958209582039852935'); 27 | INSERT INTO OID_TBL(f1) VALUES ('-23582358720398502385'); 28 | 29 | SELECT '' AS six, * FROM OID_TBL; 30 | 31 | SELECT '' AS one, o.* FROM OID_TBL o WHERE o.f1 = 1234; 32 | 33 | SELECT '' AS five, o.* FROM OID_TBL o WHERE o.f1 <> '1234'; 34 | 35 | SELECT '' AS three, o.* FROM OID_TBL o WHERE o.f1 <= '1234'; 36 | 37 | SELECT '' AS two, o.* FROM OID_TBL o WHERE o.f1 < '1234'; 38 | 39 | SELECT '' AS four, o.* FROM OID_TBL o WHERE o.f1 >= '1234'; 40 | 41 | SELECT '' AS three, o.* FROM OID_TBL o WHERE o.f1 > '1234'; 42 | 43 | DROP TABLE OID_TBL; 44 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/path.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PATH 3 | -- 4 | 5 | --DROP TABLE PATH_TBL; 6 | 7 | CREATE TABLE PATH_TBL (f1 path); 8 | 9 | INSERT INTO PATH_TBL VALUES ('[(1,2),(3,4)]'); 10 | 11 | INSERT INTO PATH_TBL VALUES (' ( ( 1 , 2 ) , ( 3 , 4 ) ) '); 12 | 13 | INSERT INTO PATH_TBL VALUES ('[ (0,0),(3,0),(4,5),(1,6) ]'); 14 | 15 | INSERT INTO PATH_TBL VALUES ('((1,2) ,(3,4 ))'); 16 | 17 | INSERT INTO PATH_TBL VALUES ('1,2 ,3,4 '); 18 | 19 | INSERT INTO PATH_TBL VALUES (' [1,2,3, 4] '); 20 | 21 | INSERT INTO PATH_TBL VALUES ('((10,20))'); -- Only one point 22 | 23 | INSERT INTO PATH_TBL VALUES ('[ 11,12,13,14 ]'); 24 | 25 | INSERT INTO PATH_TBL VALUES ('( 11,12,13,14) '); 26 | 27 | -- bad values for parser testing 28 | INSERT INTO PATH_TBL VALUES ('[]'); 29 | 30 | INSERT INTO PATH_TBL VALUES ('[(,2),(3,4)]'); 31 | 32 | INSERT INTO PATH_TBL VALUES ('[(1,2),(3,4)'); 33 | 34 | INSERT INTO PATH_TBL VALUES ('(1,2,3,4'); 35 | 36 | INSERT INTO PATH_TBL VALUES ('(1,2),(3,4)]'); 37 | 38 | SELECT '' AS count, f1 AS open_path FROM PATH_TBL WHERE isopen(f1); 39 | 40 | SELECT '' AS count, f1 AS closed_path FROM PATH_TBL WHERE isclosed(f1); 41 | 42 | SELECT '' AS count, pclose(f1) AS closed_path FROM PATH_TBL; 43 | 44 | SELECT '' AS count, popen(f1) AS open_path FROM PATH_TBL; 45 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/pg_lsn.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PG_LSN 3 | -- 4 | 5 | CREATE TABLE PG_LSN_TBL (f1 pg_lsn); 6 | 7 | -- Largest and smallest input 8 | INSERT INTO PG_LSN_TBL VALUES ('0/0'); 9 | INSERT INTO PG_LSN_TBL VALUES ('FFFFFFFF/FFFFFFFF'); 10 | 11 | -- Incorrect input 12 | INSERT INTO PG_LSN_TBL VALUES ('G/0'); 13 | INSERT INTO PG_LSN_TBL VALUES ('-1/0'); 14 | INSERT INTO PG_LSN_TBL VALUES (' 0/12345678'); 15 | INSERT INTO PG_LSN_TBL VALUES ('ABCD/'); 16 | INSERT INTO PG_LSN_TBL VALUES ('/ABCD'); 17 | DROP TABLE PG_LSN_TBL; 18 | 19 | -- Operators 20 | SELECT '0/16AE7F8' = '0/16AE7F8'::pg_lsn; 21 | SELECT '0/16AE7F8'::pg_lsn != '0/16AE7F7'; 22 | SELECT '0/16AE7F7' < '0/16AE7F8'::pg_lsn; 23 | SELECT '0/16AE7F8' > pg_lsn '0/16AE7F7'; 24 | SELECT '0/16AE7F7'::pg_lsn - '0/16AE7F8'::pg_lsn; 25 | SELECT '0/16AE7F8'::pg_lsn - '0/16AE7F7'::pg_lsn; 26 | 27 | -- Check btree and hash opclasses 28 | EXPLAIN (COSTS OFF) 29 | SELECT DISTINCT (i || '/' || j)::pg_lsn f 30 | FROM generate_series(1, 10) i, 31 | generate_series(1, 10) j, 32 | generate_series(1, 5) k 33 | WHERE i <= 10 AND j > 0 AND j <= 10 34 | ORDER BY f; 35 | 36 | SELECT DISTINCT (i || '/' || j)::pg_lsn f 37 | FROM generate_series(1, 10) i, 38 | generate_series(1, 10) j, 39 | generate_series(1, 5) k 40 | WHERE i <= 10 AND j > 0 AND j <= 10 41 | ORDER BY f; 42 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/portals_p2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PORTALS_P2 3 | -- 4 | 5 | BEGIN; 6 | 7 | DECLARE foo13 CURSOR FOR 8 | SELECT * FROM onek WHERE unique1 = 50; 9 | 10 | DECLARE foo14 CURSOR FOR 11 | SELECT * FROM onek WHERE unique1 = 51; 12 | 13 | DECLARE foo15 CURSOR FOR 14 | SELECT * FROM onek WHERE unique1 = 52; 15 | 16 | DECLARE foo16 CURSOR FOR 17 | SELECT * FROM onek WHERE unique1 = 53; 18 | 19 | DECLARE foo17 CURSOR FOR 20 | SELECT * FROM onek WHERE unique1 = 54; 21 | 22 | DECLARE foo18 CURSOR FOR 23 | SELECT * FROM onek WHERE unique1 = 55; 24 | 25 | DECLARE foo19 CURSOR FOR 26 | SELECT * FROM onek WHERE unique1 = 56; 27 | 28 | DECLARE foo20 CURSOR FOR 29 | SELECT * FROM onek WHERE unique1 = 57; 30 | 31 | DECLARE foo21 CURSOR FOR 32 | SELECT * FROM onek WHERE unique1 = 58; 33 | 34 | DECLARE foo22 CURSOR FOR 35 | SELECT * FROM onek WHERE unique1 = 59; 36 | 37 | DECLARE foo23 CURSOR FOR 38 | SELECT * FROM onek WHERE unique1 = 60; 39 | 40 | DECLARE foo24 CURSOR FOR 41 | SELECT * FROM onek2 WHERE unique1 = 50; 42 | 43 | DECLARE foo25 CURSOR FOR 44 | SELECT * FROM onek2 WHERE unique1 = 60; 45 | 46 | FETCH all in foo13; 47 | 48 | FETCH all in foo14; 49 | 50 | FETCH all in foo15; 51 | 52 | FETCH all in foo16; 53 | 54 | FETCH all in foo17; 55 | 56 | FETCH all in foo18; 57 | 58 | FETCH all in foo19; 59 | 60 | FETCH all in foo20; 61 | 62 | FETCH all in foo21; 63 | 64 | FETCH all in foo22; 65 | 66 | FETCH all in foo23; 67 | 68 | FETCH all in foo24; 69 | 70 | FETCH all in foo25; 71 | 72 | CLOSE foo13; 73 | 74 | CLOSE foo14; 75 | 76 | CLOSE foo15; 77 | 78 | CLOSE foo16; 79 | 80 | CLOSE foo17; 81 | 82 | CLOSE foo18; 83 | 84 | CLOSE foo19; 85 | 86 | CLOSE foo20; 87 | 88 | CLOSE foo21; 89 | 90 | CLOSE foo22; 91 | 92 | CLOSE foo23; 93 | 94 | CLOSE foo24; 95 | 96 | CLOSE foo25; 97 | 98 | END; 99 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/random.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- RANDOM 3 | -- Test the random function 4 | -- 5 | 6 | -- count the number of tuples originally, should be 1000 7 | SELECT count(*) FROM onek; 8 | 9 | -- pick three random rows, they shouldn't match 10 | (SELECT unique1 AS random 11 | FROM onek ORDER BY random() LIMIT 1) 12 | INTERSECT 13 | (SELECT unique1 AS random 14 | FROM onek ORDER BY random() LIMIT 1) 15 | INTERSECT 16 | (SELECT unique1 AS random 17 | FROM onek ORDER BY random() LIMIT 1); 18 | 19 | -- count roughly 1/10 of the tuples 20 | SELECT count(*) AS random INTO RANDOM_TBL 21 | FROM onek WHERE random() < 1.0/10; 22 | 23 | -- select again, the count should be different 24 | INSERT INTO RANDOM_TBL (random) 25 | SELECT count(*) 26 | FROM onek WHERE random() < 1.0/10; 27 | 28 | -- select again, the count should be different 29 | INSERT INTO RANDOM_TBL (random) 30 | SELECT count(*) 31 | FROM onek WHERE random() < 1.0/10; 32 | 33 | -- select again, the count should be different 34 | INSERT INTO RANDOM_TBL (random) 35 | SELECT count(*) 36 | FROM onek WHERE random() < 1.0/10; 37 | 38 | -- now test that they are different counts 39 | SELECT random, count(random) FROM RANDOM_TBL 40 | GROUP BY random HAVING count(random) > 3; 41 | 42 | SELECT AVG(random) FROM RANDOM_TBL 43 | HAVING AVG(random) NOT BETWEEN 80 AND 120; 44 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/regex.linux.utf8.sql: -------------------------------------------------------------------------------- 1 | /* 2 | * This test is for Linux/glibc systems and others that implement proper 3 | * locale classification of Unicode characters with high code values. 4 | * It must be run in a database with UTF8 encoding and a Unicode-aware locale. 5 | */ 6 | 7 | SET client_encoding TO UTF8; 8 | 9 | -- 10 | -- Test the "high colormap" logic with single characters and ranges that 11 | -- exceed the MAX_SIMPLE_CHR cutoff, here assumed to be less than U+2000. 12 | -- 13 | 14 | -- trivial cases: 15 | SELECT 'aⓐ' ~ U&'a\24D0' AS t; 16 | SELECT 'aⓐ' ~ U&'a\24D1' AS f; 17 | SELECT 'aⓕ' ~ 'a[ⓐ-ⓩ]' AS t; 18 | SELECT 'aⒻ' ~ 'a[ⓐ-ⓩ]' AS f; 19 | -- cases requiring splitting of ranges: 20 | SELECT 'aⓕⓕ' ~ 'aⓕ[ⓐ-ⓩ]' AS t; 21 | SELECT 'aⓕⓐ' ~ 'aⓕ[ⓐ-ⓩ]' AS t; 22 | SELECT 'aⓐⓕ' ~ 'aⓕ[ⓐ-ⓩ]' AS f; 23 | SELECT 'aⓕⓕ' ~ 'a[ⓐ-ⓩ]ⓕ' AS t; 24 | SELECT 'aⓕⓐ' ~ 'a[ⓐ-ⓩ]ⓕ' AS f; 25 | SELECT 'aⓐⓕ' ~ 'a[ⓐ-ⓩ]ⓕ' AS t; 26 | SELECT 'aⒶⓜ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS t; 27 | SELECT 'aⓜⓜ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS t; 28 | SELECT 'aⓜⓩ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS t; 29 | SELECT 'aⓩⓩ' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS f; 30 | SELECT 'aⓜ⓪' ~ 'a[Ⓐ-ⓜ][ⓜ-ⓩ]' AS f; 31 | SELECT 'a0' ~ 'a[a-ⓩ]' AS f; 32 | SELECT 'aq' ~ 'a[a-ⓩ]' AS t; 33 | SELECT 'aⓜ' ~ 'a[a-ⓩ]' AS t; 34 | SELECT 'a⓪' ~ 'a[a-ⓩ]' AS f; 35 | 36 | -- Locale-dependent character classes 37 | 38 | SELECT 'aⒶⓜ⓪' ~ '[[:alpha:]][[:alpha:]][[:alpha:]][[:graph:]]' AS t; 39 | SELECT 'aⒶⓜ⓪' ~ '[[:alpha:]][[:alpha:]][[:alpha:]][[:alpha:]]' AS f; 40 | 41 | -- Locale-dependent character classes with high ranges 42 | 43 | SELECT 'aⒶⓜ⓪' ~ '[a-z][[:alpha:]][ⓐ-ⓩ][[:graph:]]' AS t; 44 | SELECT 'aⓜⒶ⓪' ~ '[a-z][[:alpha:]][ⓐ-ⓩ][[:graph:]]' AS f; 45 | SELECT 'aⓜⒶ⓪' ~ '[a-z][ⓐ-ⓩ][[:alpha:]][[:graph:]]' AS t; 46 | SELECT 'aⒶⓜ⓪' ~ '[a-z][ⓐ-ⓩ][[:alpha:]][[:graph:]]' AS f; 47 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/reindex_catalog.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Check that system tables can be reindexed. 3 | -- 4 | -- Note that this test currently has to run without parallel tests 5 | -- being scheduled, as currently reindex catalog tables can cause 6 | -- deadlocks: 7 | -- 8 | -- * The lock upgrade between the ShareLock acquired for the reindex 9 | -- and RowExclusiveLock needed for pg_class/pg_index locks can 10 | -- trigger deadlocks. 11 | -- 12 | -- * The uniqueness checks performed when reindexing a unique/primary 13 | -- key index possibly need to wait for the transaction of a 14 | -- about-to-deleted row in pg_class to commit. That can cause 15 | -- deadlocks because, in contrast to user tables, locks on catalog 16 | -- tables are routinely released before commit - therefore the lock 17 | -- held for reindexing doesn't guarantee that no running transaction 18 | -- performed modifications in the table underlying the index. 19 | 20 | 21 | -- Check reindexing of whole tables 22 | REINDEX TABLE pg_class; -- mapped, non-shared, critical 23 | REINDEX TABLE pg_index; -- non-mapped, non-shared, critical 24 | REINDEX TABLE pg_operator; -- non-mapped, non-shared, critical 25 | REINDEX TABLE pg_database; -- mapped, shared, critical 26 | REINDEX TABLE pg_shdescription; -- mapped, shared non-critical 27 | 28 | -- Check that individual system indexes can be reindexed. That's a bit 29 | -- different from the entire-table case because reindex_relation 30 | -- treats e.g. pg_class special. 31 | REINDEX INDEX pg_class_oid_index; -- mapped, non-shared, critical 32 | REINDEX INDEX pg_class_relname_nsp_index; -- mapped, non-shared, non-critical 33 | REINDEX INDEX pg_index_indexrelid_index; -- non-mapped, non-shared, critical 34 | REINDEX INDEX pg_index_indrelid_index; -- non-mapped, non-shared, non-critical 35 | REINDEX INDEX pg_database_oid_index; -- mapped, shared, critical 36 | REINDEX INDEX pg_shdescription_o_c_index; -- mapped, shared, non-critical 37 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/sanity_check.sql: -------------------------------------------------------------------------------- 1 | VACUUM; 2 | 3 | -- 4 | -- sanity check, if we don't have indices the test will take years to 5 | -- complete. But skip TOAST relations (since they will have varying 6 | -- names depending on the current OID counter) as well as temp tables 7 | -- of other backends (to avoid timing-dependent behavior). 8 | -- 9 | 10 | -- temporarily disable fancy output, so catalog changes create less diff noise 11 | \a\t 12 | 13 | SELECT relname, relhasindex 14 | FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = relnamespace 15 | WHERE relkind IN ('r', 'p') AND (nspname ~ '^pg_temp_') IS NOT TRUE 16 | ORDER BY relname; 17 | 18 | -- restore normal output mode 19 | \a\t 20 | 21 | -- 22 | -- another sanity check: every system catalog that has OIDs should have 23 | -- a unique index on OID. This ensures that the OIDs will be unique, 24 | -- even after the OID counter wraps around. 25 | -- We exclude non-system tables from the check by looking at nspname. 26 | -- 27 | SELECT relname, nspname 28 | FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = relnamespace JOIN pg_attribute a ON (attrelid = c.oid AND attname = 'oid') 29 | WHERE relkind = 'r' and c.oid < 16384 30 | AND ((nspname ~ '^pg_') IS NOT FALSE) 31 | AND NOT EXISTS (SELECT 1 FROM pg_index i WHERE indrelid = c.oid 32 | AND indkey[0] = a.attnum AND indnatts = 1 33 | AND indisunique AND indimmediate); 34 | 35 | -- check that relations without storage don't have relfilenode 36 | SELECT relname, relkind 37 | FROM pg_class 38 | WHERE relkind IN ('v', 'c', 'f', 'p', 'I') 39 | AND relfilenode <> 0; 40 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/security_label.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Test for facilities of security label 3 | -- 4 | 5 | -- initial setups 6 | SET client_min_messages TO 'warning'; 7 | 8 | DROP ROLE IF EXISTS regress_seclabel_user1; 9 | DROP ROLE IF EXISTS regress_seclabel_user2; 10 | 11 | RESET client_min_messages; 12 | 13 | CREATE USER regress_seclabel_user1 WITH CREATEROLE; 14 | CREATE USER regress_seclabel_user2; 15 | 16 | CREATE TABLE seclabel_tbl1 (a int, b text); 17 | CREATE TABLE seclabel_tbl2 (x int, y text); 18 | CREATE VIEW seclabel_view1 AS SELECT * FROM seclabel_tbl2; 19 | CREATE FUNCTION seclabel_four() RETURNS integer AS $$SELECT 4$$ language sql; 20 | CREATE DOMAIN seclabel_domain AS text; 21 | 22 | ALTER TABLE seclabel_tbl1 OWNER TO regress_seclabel_user1; 23 | ALTER TABLE seclabel_tbl2 OWNER TO regress_seclabel_user2; 24 | 25 | -- 26 | -- Test of SECURITY LABEL statement without a plugin 27 | -- 28 | SECURITY LABEL ON TABLE seclabel_tbl1 IS 'classified'; -- fail 29 | SECURITY LABEL FOR 'dummy' ON TABLE seclabel_tbl1 IS 'classified'; -- fail 30 | SECURITY LABEL ON TABLE seclabel_tbl1 IS '...invalid label...'; -- fail 31 | SECURITY LABEL ON TABLE seclabel_tbl3 IS 'unclassified'; -- fail 32 | 33 | SECURITY LABEL ON ROLE regress_seclabel_user1 IS 'classified'; -- fail 34 | SECURITY LABEL FOR 'dummy' ON ROLE regress_seclabel_user1 IS 'classified'; -- fail 35 | SECURITY LABEL ON ROLE regress_seclabel_user1 IS '...invalid label...'; -- fail 36 | SECURITY LABEL ON ROLE regress_seclabel_user3 IS 'unclassified'; -- fail 37 | 38 | -- clean up objects 39 | DROP FUNCTION seclabel_four(); 40 | DROP DOMAIN seclabel_domain; 41 | DROP VIEW seclabel_view1; 42 | DROP TABLE seclabel_tbl1; 43 | DROP TABLE seclabel_tbl2; 44 | DROP USER regress_seclabel_user1; 45 | DROP USER regress_seclabel_user2; 46 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/select_distinct_on.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- SELECT_DISTINCT_ON 3 | -- 4 | 5 | SELECT DISTINCT ON (string4) string4, two, ten 6 | FROM tmp 7 | ORDER BY string4 using <, two using >, ten using <; 8 | 9 | -- this will fail due to conflict of ordering requirements 10 | SELECT DISTINCT ON (string4, ten) string4, two, ten 11 | FROM tmp 12 | ORDER BY string4 using <, two using <, ten using <; 13 | 14 | SELECT DISTINCT ON (string4, ten) string4, ten, two 15 | FROM tmp 16 | ORDER BY string4 using <, ten using >, two using <; 17 | 18 | -- bug #5049: early 8.4.x chokes on volatile DISTINCT ON clauses 19 | select distinct on (1) floor(random()) as r, f1 from int4_tbl order by 1,2; 20 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/select_having.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- SELECT_HAVING 3 | -- 4 | 5 | -- load test data 6 | CREATE TABLE test_having (a int, b int, c char(8), d char); 7 | INSERT INTO test_having VALUES (0, 1, 'XXXX', 'A'); 8 | INSERT INTO test_having VALUES (1, 2, 'AAAA', 'b'); 9 | INSERT INTO test_having VALUES (2, 2, 'AAAA', 'c'); 10 | INSERT INTO test_having VALUES (3, 3, 'BBBB', 'D'); 11 | INSERT INTO test_having VALUES (4, 3, 'BBBB', 'e'); 12 | INSERT INTO test_having VALUES (5, 3, 'bbbb', 'F'); 13 | INSERT INTO test_having VALUES (6, 4, 'cccc', 'g'); 14 | INSERT INTO test_having VALUES (7, 4, 'cccc', 'h'); 15 | INSERT INTO test_having VALUES (8, 4, 'CCCC', 'I'); 16 | INSERT INTO test_having VALUES (9, 4, 'CCCC', 'j'); 17 | 18 | SELECT b, c FROM test_having 19 | GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c; 20 | 21 | -- HAVING is effectively equivalent to WHERE in this case 22 | SELECT b, c FROM test_having 23 | GROUP BY b, c HAVING b = 3 ORDER BY b, c; 24 | 25 | SELECT lower(c), count(c) FROM test_having 26 | GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a) 27 | ORDER BY lower(c); 28 | 29 | SELECT c, max(a) FROM test_having 30 | GROUP BY c HAVING count(*) > 2 OR min(a) = max(a) 31 | ORDER BY c; 32 | 33 | -- test degenerate cases involving HAVING without GROUP BY 34 | -- Per SQL spec, these should generate 0 or 1 row, even without aggregates 35 | 36 | SELECT min(a), max(a) FROM test_having HAVING min(a) = max(a); 37 | SELECT min(a), max(a) FROM test_having HAVING min(a) < max(a); 38 | 39 | -- errors: ungrouped column references 40 | SELECT a FROM test_having HAVING min(a) < max(a); 41 | SELECT 1 AS one FROM test_having HAVING a > 1; 42 | 43 | -- the really degenerate case: need not scan table at all 44 | SELECT 1 AS one FROM test_having HAVING 1 > 2; 45 | SELECT 1 AS one FROM test_having HAVING 1 < 2; 46 | 47 | -- and just to prove that we aren't scanning the table: 48 | SELECT 1 AS one FROM test_having WHERE 1/a = 1 HAVING 1 < 2; 49 | 50 | DROP TABLE test_having; 51 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/time.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- TIME 3 | -- 4 | 5 | CREATE TABLE TIME_TBL (f1 time(2)); 6 | 7 | INSERT INTO TIME_TBL VALUES ('00:00'); 8 | INSERT INTO TIME_TBL VALUES ('01:00'); 9 | -- as of 7.4, timezone spec should be accepted and ignored 10 | INSERT INTO TIME_TBL VALUES ('02:03 PST'); 11 | INSERT INTO TIME_TBL VALUES ('11:59 EDT'); 12 | INSERT INTO TIME_TBL VALUES ('12:00'); 13 | INSERT INTO TIME_TBL VALUES ('12:01'); 14 | INSERT INTO TIME_TBL VALUES ('23:59'); 15 | INSERT INTO TIME_TBL VALUES ('11:59:59.99 PM'); 16 | 17 | INSERT INTO TIME_TBL VALUES ('2003-03-07 15:36:39 America/New_York'); 18 | INSERT INTO TIME_TBL VALUES ('2003-07-07 15:36:39 America/New_York'); 19 | -- this should fail (the timezone offset is not known) 20 | INSERT INTO TIME_TBL VALUES ('15:36:39 America/New_York'); 21 | 22 | 23 | SELECT f1 AS "Time" FROM TIME_TBL; 24 | 25 | SELECT f1 AS "Three" FROM TIME_TBL WHERE f1 < '05:06:07'; 26 | 27 | SELECT f1 AS "Five" FROM TIME_TBL WHERE f1 > '05:06:07'; 28 | 29 | SELECT f1 AS "None" FROM TIME_TBL WHERE f1 < '00:00'; 30 | 31 | SELECT f1 AS "Eight" FROM TIME_TBL WHERE f1 >= '00:00'; 32 | 33 | -- 34 | -- TIME simple math 35 | -- 36 | -- We now make a distinction between time and intervals, 37 | -- and adding two times together makes no sense at all. 38 | -- Leave in one query to show that it is rejected, 39 | -- and do the rest of the testing in horology.sql 40 | -- where we do mixed-type arithmetic. - thomas 2000-12-02 41 | 42 | SELECT f1 + time '00:01' AS "Illegal" FROM TIME_TBL; 43 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/timetz.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- TIMETZ 3 | -- 4 | 5 | CREATE TABLE TIMETZ_TBL (f1 time(2) with time zone); 6 | 7 | INSERT INTO TIMETZ_TBL VALUES ('00:01 PDT'); 8 | INSERT INTO TIMETZ_TBL VALUES ('01:00 PDT'); 9 | INSERT INTO TIMETZ_TBL VALUES ('02:03 PDT'); 10 | INSERT INTO TIMETZ_TBL VALUES ('07:07 PST'); 11 | INSERT INTO TIMETZ_TBL VALUES ('08:08 EDT'); 12 | INSERT INTO TIMETZ_TBL VALUES ('11:59 PDT'); 13 | INSERT INTO TIMETZ_TBL VALUES ('12:00 PDT'); 14 | INSERT INTO TIMETZ_TBL VALUES ('12:01 PDT'); 15 | INSERT INTO TIMETZ_TBL VALUES ('23:59 PDT'); 16 | INSERT INTO TIMETZ_TBL VALUES ('11:59:59.99 PM PDT'); 17 | 18 | INSERT INTO TIMETZ_TBL VALUES ('2003-03-07 15:36:39 America/New_York'); 19 | INSERT INTO TIMETZ_TBL VALUES ('2003-07-07 15:36:39 America/New_York'); 20 | -- this should fail (the timezone offset is not known) 21 | INSERT INTO TIMETZ_TBL VALUES ('15:36:39 America/New_York'); 22 | 23 | SELECT f1 AS "Time TZ" FROM TIMETZ_TBL; 24 | 25 | SELECT f1 AS "Three" FROM TIMETZ_TBL WHERE f1 < '05:06:07-07'; 26 | 27 | SELECT f1 AS "Seven" FROM TIMETZ_TBL WHERE f1 > '05:06:07-07'; 28 | 29 | SELECT f1 AS "None" FROM TIMETZ_TBL WHERE f1 < '00:00-07'; 30 | 31 | SELECT f1 AS "Ten" FROM TIMETZ_TBL WHERE f1 >= '00:00-07'; 32 | 33 | -- 34 | -- TIME simple math 35 | -- 36 | -- We now make a distinction between time and intervals, 37 | -- and adding two times together makes no sense at all. 38 | -- Leave in one query to show that it is rejected, 39 | -- and do the rest of the testing in horology.sql 40 | -- where we do mixed-type arithmetic. - thomas 2000-12-02 41 | 42 | SELECT f1 + time with time zone '00:01' AS "Illegal" FROM TIMETZ_TBL; 43 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/typed_table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE ttable1 OF nothing; 2 | 3 | CREATE TYPE person_type AS (id int, name text); 4 | CREATE TABLE persons OF person_type; 5 | CREATE TABLE IF NOT EXISTS persons OF person_type; 6 | SELECT * FROM persons; 7 | \d persons 8 | 9 | CREATE FUNCTION get_all_persons() RETURNS SETOF person_type 10 | LANGUAGE SQL 11 | AS $$ 12 | SELECT * FROM persons; 13 | $$; 14 | 15 | SELECT * FROM get_all_persons(); 16 | 17 | -- certain ALTER TABLE operations on typed tables are not allowed 18 | ALTER TABLE persons ADD COLUMN comment text; 19 | ALTER TABLE persons DROP COLUMN name; 20 | ALTER TABLE persons RENAME COLUMN id TO num; 21 | ALTER TABLE persons ALTER COLUMN name TYPE varchar; 22 | CREATE TABLE stuff (id int); 23 | ALTER TABLE persons INHERIT stuff; 24 | 25 | CREATE TABLE personsx OF person_type (myname WITH OPTIONS NOT NULL); -- error 26 | 27 | CREATE TABLE persons2 OF person_type ( 28 | id WITH OPTIONS PRIMARY KEY, 29 | UNIQUE (name) 30 | ); 31 | 32 | \d persons2 33 | 34 | CREATE TABLE persons3 OF person_type ( 35 | PRIMARY KEY (id), 36 | name WITH OPTIONS DEFAULT '' 37 | ); 38 | 39 | \d persons3 40 | 41 | CREATE TABLE persons4 OF person_type ( 42 | name WITH OPTIONS NOT NULL, 43 | name WITH OPTIONS DEFAULT '' -- error, specified more than once 44 | ); 45 | 46 | DROP TYPE person_type RESTRICT; 47 | DROP TYPE person_type CASCADE; 48 | 49 | CREATE TABLE persons5 OF stuff; -- only CREATE TYPE AS types may be used 50 | 51 | DROP TABLE stuff; 52 | 53 | 54 | -- implicit casting 55 | 56 | CREATE TYPE person_type AS (id int, name text); 57 | CREATE TABLE persons OF person_type; 58 | INSERT INTO persons VALUES (1, 'test'); 59 | 60 | CREATE FUNCTION namelen(person_type) RETURNS int LANGUAGE SQL AS $$ SELECT length($1.name) $$; 61 | SELECT id, namelen(persons) FROM persons; 62 | 63 | CREATE TABLE persons2 OF person_type ( 64 | id WITH OPTIONS PRIMARY KEY, 65 | UNIQUE (name) 66 | ); 67 | 68 | \d persons2 69 | 70 | CREATE TABLE persons3 OF person_type ( 71 | PRIMARY KEY (id), 72 | name NOT NULL DEFAULT '' 73 | ); 74 | 75 | \d persons3 76 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/varchar.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- VARCHAR 3 | -- 4 | 5 | CREATE TABLE VARCHAR_TBL(f1 varchar(1)); 6 | 7 | INSERT INTO VARCHAR_TBL (f1) VALUES ('a'); 8 | 9 | INSERT INTO VARCHAR_TBL (f1) VALUES ('A'); 10 | 11 | -- any of the following three input formats are acceptable 12 | INSERT INTO VARCHAR_TBL (f1) VALUES ('1'); 13 | 14 | INSERT INTO VARCHAR_TBL (f1) VALUES (2); 15 | 16 | INSERT INTO VARCHAR_TBL (f1) VALUES ('3'); 17 | 18 | -- zero-length char 19 | INSERT INTO VARCHAR_TBL (f1) VALUES (''); 20 | 21 | -- try varchar's of greater than 1 length 22 | INSERT INTO VARCHAR_TBL (f1) VALUES ('cd'); 23 | INSERT INTO VARCHAR_TBL (f1) VALUES ('c '); 24 | 25 | 26 | SELECT '' AS seven, * FROM VARCHAR_TBL; 27 | 28 | SELECT '' AS six, c.* 29 | FROM VARCHAR_TBL c 30 | WHERE c.f1 <> 'a'; 31 | 32 | SELECT '' AS one, c.* 33 | FROM VARCHAR_TBL c 34 | WHERE c.f1 = 'a'; 35 | 36 | SELECT '' AS five, c.* 37 | FROM VARCHAR_TBL c 38 | WHERE c.f1 < 'a'; 39 | 40 | SELECT '' AS six, c.* 41 | FROM VARCHAR_TBL c 42 | WHERE c.f1 <= 'a'; 43 | 44 | SELECT '' AS one, c.* 45 | FROM VARCHAR_TBL c 46 | WHERE c.f1 > 'a'; 47 | 48 | SELECT '' AS two, c.* 49 | FROM VARCHAR_TBL c 50 | WHERE c.f1 >= 'a'; 51 | 52 | DROP TABLE VARCHAR_TBL; 53 | 54 | -- 55 | -- Now test longer arrays of char 56 | -- 57 | 58 | CREATE TABLE VARCHAR_TBL(f1 varchar(4)); 59 | 60 | INSERT INTO VARCHAR_TBL (f1) VALUES ('a'); 61 | INSERT INTO VARCHAR_TBL (f1) VALUES ('ab'); 62 | INSERT INTO VARCHAR_TBL (f1) VALUES ('abcd'); 63 | INSERT INTO VARCHAR_TBL (f1) VALUES ('abcde'); 64 | INSERT INTO VARCHAR_TBL (f1) VALUES ('abcd '); 65 | 66 | SELECT '' AS four, * FROM VARCHAR_TBL; 67 | -------------------------------------------------------------------------------- /t/pg-test-files/sql/write_parallel.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- PARALLEL 3 | -- 4 | 5 | -- Serializable isolation would disable parallel query, so explicitly use an 6 | -- arbitrary other level. 7 | begin isolation level repeatable read; 8 | 9 | -- encourage use of parallel plans 10 | set parallel_setup_cost=0; 11 | set parallel_tuple_cost=0; 12 | set min_parallel_table_scan_size=0; 13 | set max_parallel_workers_per_gather=4; 14 | 15 | -- 16 | -- Test write operations that has an underlying query that is eligble 17 | -- for parallel plans 18 | -- 19 | explain (costs off) create table parallel_write as 20 | select length(stringu1) from tenk1 group by length(stringu1); 21 | create table parallel_write as 22 | select length(stringu1) from tenk1 group by length(stringu1); 23 | drop table parallel_write; 24 | 25 | explain (costs off) select length(stringu1) into parallel_write 26 | from tenk1 group by length(stringu1); 27 | select length(stringu1) into parallel_write 28 | from tenk1 group by length(stringu1); 29 | drop table parallel_write; 30 | 31 | explain (costs off) create materialized view parallel_mat_view as 32 | select length(stringu1) from tenk1 group by length(stringu1); 33 | create materialized view parallel_mat_view as 34 | select length(stringu1) from tenk1 group by length(stringu1); 35 | drop materialized view parallel_mat_view; 36 | 37 | prepare prep_stmt as select length(stringu1) from tenk1 group by length(stringu1); 38 | explain (costs off) create table parallel_write as execute prep_stmt; 39 | create table parallel_write as execute prep_stmt; 40 | drop table parallel_write; 41 | 42 | rollback; 43 | -------------------------------------------------------------------------------- /t/redshift.kw: -------------------------------------------------------------------------------- 1 | AES128 2 | AES256 3 | ALLOWOVERWRITE 4 | BACKUP 5 | BLANKSASNULL 6 | BYTEDICT 7 | BZIP2 8 | CREDENTIALS 9 | CURRENT_USER_ID 10 | DEFLATE 11 | DEFRAG 12 | DELTA 13 | DELTA32K 14 | DISABLE 15 | DISTKEY 16 | EMPTYASNULL 17 | ENABLE 18 | ENCODE 19 | ENCRYPT 20 | ENCRYPTION 21 | ESCAPE 22 | EXPLICIT 23 | GLOBALDICT256 24 | GLOBALDICT64K 25 | GZIP 26 | INTERLEAVED 27 | LUN 28 | LUNS 29 | LZO 30 | LZOP 31 | MINUS 32 | MOSTLY13 33 | MOSTLY32 34 | MOSTLY8 35 | NEW 36 | OFFLINE 37 | OFFSET 38 | OLD 39 | OID 40 | PARALLEL 41 | PERCENT 42 | PERMISSIONS 43 | RAW 44 | READRATIO 45 | RECOVER 46 | REJECTLOG 47 | RESORT 48 | RESPECT 49 | RESTORE 50 | SORTKEY 51 | SYSDATE 52 | TAG 53 | TDES 54 | TEXT255 55 | TEXT32K 56 | TIMESTAMP 57 | TOP 58 | TRUNCATECOLUMNS 59 | UNLOAD 60 | WALLET 61 | ADDQUOTES 62 | -------------------------------------------------------------------------------- /t/test-files/ex0.sql: -------------------------------------------------------------------------------- 1 | select a,b,c 2 | from tablea 3 | join tableb on ( tablea.a=tableb.a) 4 | join tablec on ( tablec.a=tableb.a) 5 | left outer join tabled on ( tabled.a=tableb.a) 6 | left join tablee on ( tabled.a=tableb.a) 7 | where tablea.x = 1 and tableb.y=1 8 | group by tablea.a, tablec.c 9 | order by tablea.a, tablec.c; 10 | 11 | 12 | -------------------------------------------------------------------------------- /t/test-files/ex10.sql: -------------------------------------------------------------------------------- 1 | with a as ( 2 | select x, y, z 3 | from twelve 4 | join nine 5 | on a = 2 6 | and b = a 7 | ), b as ( 8 | select * 9 | from a 10 | ) select * 11 | from b; 12 | -------------------------------------------------------------------------------- /t/test-files/ex11.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM a, ONLY (c) JOIN b USING (id, id2) LEFT JOIN d USING (id) WHERE id > 10 AND id <= 20; 2 | 3 | CREATE OR REPLACE FUNCTION test_evtrig_no_rewrite() RETURNS event_trigger 4 | LANGUAGE plpgsql AS $$ 5 | BEGIN 6 | RAISE NOTICE 'Table ''%'' is being rewritten (reason = %)', 7 | pg_event_trigger_table_rewrite_oid()::regclass, 8 | pg_event_trigger_table_rewrite_reason(); 9 | END; 10 | $$; 11 | 12 | SELECT 13 | lives_ok ('INSERT INTO "order".v_order (status, order_id, name) 14 | VALUES (''complete'', ''' || get_order_id () || ''', '' caleb ''', 'with all parameters'); 15 | 16 | prepare q as 17 | select 'some"text' as "a""title", E' \n' as "junk", 18 | ' ' as "empty", n as int 19 | from generate_series(1,2) as n; 20 | 21 | select websearch_to_tsquery('''abc''''def'''); 22 | 23 | create function raise_exprs() returns void as $$ 24 | declare 25 | a integer[] = '{10,20,30}'; 26 | c varchar = 'xyz'; 27 | i integer; 28 | begin 29 | i := 2; 30 | raise notice '%; %; %; %; %; %', a, a[i], c, (select c || 'abc'), row(10,'aaa',NULL,30), NULL; 31 | end;$$ language plpgsql; 32 | 33 | -------------------------------------------------------------------------------- /t/test-files/ex12.sql: -------------------------------------------------------------------------------- 1 | SET client_encoding TO 'UTF8'; 2 | 3 | UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT 4 | WHERE city = 'San Francisco' AND date = '2003-07-03' RETURNING temp_lo,temp_hi,prcp; 5 | 6 | \set ON_ERROR_STOP ON 7 | 8 | SELECT * FROM (SELECT * FROM mytable FOR UPDATE) AS ss WHERE col1 = 5; 9 | 10 | BEGIN; 11 | SELECT * FROM mytable WHERE key = 1 FOR NO KEY UPDATE; 12 | SAVEPOINT s; 13 | UPDATE mytable SET col1=NULL WHERE key = 1; 14 | ROLLBACK TO s; 15 | 16 | -------------------------------------------------------------------------------- /t/test-files/ex13.sql: -------------------------------------------------------------------------------- 1 | WITH RECURSIVE employee_recursive (distance, employee_name, manager_name) AS ( 2 | SELECT 1, employee_name, manager_name 3 | FROM employee 4 | WHERE manager_name = 'Mary' 5 | UNION ALL 6 | SELECT er.distance + 1, e.employee_name, e.manager_name 7 | FROM employee_recursive er, employee e 8 | WHERE er.employee_name = e.manager_name 9 | ) 10 | SELECT distance, employee_name FROM employee_recursive; 11 | 12 | WITH RECURSIVE t(nombre) AS ( 13 | VALUES (2) 14 | UNION ALL 15 | SELECT 2 * nombre FROM t WHERE 2 * nombre < 100 16 | ) 17 | SELECT nombre FROM t; 18 | 19 | CREATE FUNCTION tg_phone_bu () 20 | RETURNS TRIGGER 21 | AS $$ 22 | BEGIN 23 | IF new.slotname != old.slotname THEN 24 | DELETE FROM PHone 25 | WHERE slotname = old.slotname; 26 | INSERT INTO PHone (slotname, comment, 27 | slotlink) 28 | VALUES (new.slotname, 29 | new.comment, 30 | new.slotlink); 31 | RETURN NULL; 32 | END IF; 33 | RETURN new; 34 | END; 35 | $$ 36 | LANGUAGE plpgsql; 37 | -------------------------------------------------------------------------------- /t/test-files/ex14.sql: -------------------------------------------------------------------------------- 1 | SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g'); 2 | SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})'); 3 | SELECT m.name AS mname, pname 4 | FROM manufacturers m, LATERAL get_product_names(m.id) pname; 5 | SELECT m.name AS mname, pname 6 | FROM manufacturers m LEFT JOIN LATERAL get_product_names(m.id) pname ON true; 7 | with one as (select 1 one) select count(one),avg(one) from one; 8 | SELECT * FROM a FULL JOIN b USING (c); 9 | SELECT * FROM a FULL OUTER JOIN b USING (c); 10 | CREATE TYPE jwt_token AS (token TEXT, field: TEXT); 11 | -------------------------------------------------------------------------------- /t/test-files/ex15.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | user_id, 3 | view_homepage, 4 | view_homepage_time, 5 | enter_credit_card, 6 | enter_credit_card_time 7 | FROM ( -- Get the first time each user viewed the homepage. 8 | SELECT 9 | user_id, 10 | 1 AS view_homepage, 11 | min (time) AS view_homepage_time 12 | FROM 13 | event 14 | WHERE 15 | data ->> 'type' = 'view_homepage' 16 | GROUP BY 17 | user_id) 18 | e1 19 | LEFT JOIN LATERAL ( -- For each row, get the first time the user_id did the enter_credit_card 20 | -- event, if one exists within two weeks of view_homepage_time. 21 | SELECT 22 | 1 AS enter_credit_card, 23 | time AS enter_credit_card_time 24 | FROM 25 | event 26 | WHERE 27 | user_id = e1.user_id 28 | AND data ->> 'type' = 'enter_credit_card' 29 | AND time BETWEEN view_homepage_time 30 | AND (view_homepage_time + 1000 * 60 * 60 * 24 * 14) 31 | ORDER BY 32 | time 33 | LIMIT 1) 34 | e2 ON TRUE; 35 | -------------------------------------------------------------------------------- /t/test-files/ex16.sql: -------------------------------------------------------------------------------- 1 | SET client_encoding TO 'UTF8'; 2 | 3 | \set ON_ERROR_STOP ON 4 | 5 | CREATE TABLE new_table_test ( 6 | start_date timestamp NOT NULL, 7 | end_date timestamp NOT NULL, 8 | name varchar(40) NOT NULL CHECK (name <> ''), 9 | fk_organization_unit_id numeric(20), 10 | fk_product_id numeric(20), 11 | id numeric(20) NOT NULL PRIMARY KEY, 12 | migrated varchar(1) 13 | ) ; 14 | COMMENT ON TABLE new_table_test IS E'Associação dos produtos as Unidades de Estrutura responsável'; 15 | COMMENT ON COLUMN new_table_test.end_date IS E'Data fim da associação da unidade. '; 16 | COMMENT ON COLUMN new_table_test.migrated IS E'Indica se o registro foi migrado. Valores possíveis: S - Sim, N - Não.'; 17 | CREATE INDEX ni_ansu_3 ON new_table_test (fk_product_id, start_date); 18 | CREATE INDEX ni_ansu_2 ON new_table_test (fk_organization_unit_id asc, fk_product_id desc); 19 | CREATE INDEX ni_ansu_1 ON new_table_test (fk_product_id asc, start_date asc); 20 | ALTER TABLE new_table_test ADD CONSTRAINT ora2pg_ckey_fk_organization_unit_id CHECK (fk_organization_unit_id is not NULL); 21 | 22 | ALTER TABLE new_table_test ADD CONSTRAINT ck_ansu_fk_org_unit_id CHECK (fk_organization_unit_id is not NULL AND fk_organization_unit_id > 1000); 23 | 24 | ALTER TABLE new_table_test ADD CONSTRAINT fk_ansu_produ_id FOREIGN KEY (FK_PRODUCT_ID) REFERENCES PRODUCT (ID); 25 | 26 | CREATE TABLE test_uuid ( 27 | nom varchar(25), 28 | uid_col bytea NOT NULL DEFAULT uuid_generate_v4() 29 | ) ; 30 | 31 | CREATE TABLE test_boolean ( 32 | id bigint, 33 | is_deleted boolean, 34 | is_updated boolean 35 | ) ; 36 | ALTER TABLE test_boolean ADD PRIMARY KEY (id,is_deleted,is_updated); 37 | 38 | CREATE TABLE table_0 ( 39 | ref_1 integer REFERENCES table_1 ON DELETE RESTRICT, 40 | ref_2 integer REFERENCES table_2 ON DELETE CASCADE, 41 | ref_3 integer REFERENCES table_3 ON DELETE SET NULL 42 | ); 43 | 44 | 45 | COMMENT ON TABLE foo.bar IS 46 | $comment$ 47 | This is a nicely formatted comment line that spans 48 | over multiple lines that should remain untouched 49 | 50 | $comment$; 51 | 52 | COMMENT ON TABLE foo2 IS 'Hello world'; 53 | 54 | -- Multi-line comment 55 | comment on table test is 56 | E'foo\n' 57 | 'bar\n' 58 | 'bat'; 59 | -------------------------------------------------------------------------------- /t/test-files/ex18.sql: -------------------------------------------------------------------------------- 1 | GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA foo TO role_bar, role_baz; 2 | 3 | SELECT * FROM t WHERE a IS NOT DISTINCT FROM b; 4 | 5 | -- Deploy schemas/custom/grants/grant_schema_to_authenticated to pg 6 | -- requires: schemas/custom/schema 7 | BEGIN; 8 | 9 | GRANT USAGE ON SCHEMA custom 10 | TO authenticated; 11 | GRANT USAGE ON SCHEMA custom TO authenticated; 12 | 13 | COMMIT; 14 | -------------------------------------------------------------------------------- /t/test-files/ex2.sql: -------------------------------------------------------------------------------- 1 | SELECT n.nspname as "Schema", 2 | p.proname as "Name", 3 | pg_catalog.pg_get_function_result(p.oid) as "Result data type", 4 | pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types", 5 | CASE 6 | WHEN p.proisagg THEN 'agg' 7 | WHEN p.proiswindow THEN 'window' 8 | WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger' 9 | ELSE 'normal' 10 | END as "Type" 11 | FROM pg_catalog.pg_proc p 12 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace 13 | WHERE p.proname ~ '^(version)$' 14 | AND pg_catalog.pg_function_is_visible(p.oid) 15 | ORDER BY 1, 2, 4; 16 | 17 | SELECT CASE WHEN (FALSE) THEN 0 WHEN (TRUE) THEN 2 END AS dummy1 FROM my_table; 18 | 19 | CREATE OR REPLACE FUNCTION task_job_maint_after () 20 | RETURNS TRIGGER 21 | AS $$ 22 | BEGIN 23 | CASE new.state 24 | WHEN 'final' THEN 25 | NOTIFY task_job_final; 26 | ELSE 27 | NULL; 28 | END CASE; 29 | RETURN NEW; 30 | END; 31 | $$ 32 | LANGUAGE plpgsql; 33 | 34 | -------------------------------------------------------------------------------- /t/test-files/ex20.sql: -------------------------------------------------------------------------------- 1 | SELECT group_concat(k.column_name ORDER BY k.ordinal_position) AS column_names, t.table_name AS table_name, t.table_schema AS table_schema, t.constraint_name AS constraint_name FROM information_schema.table_constraints t LEFT JOIN information_schema.key_column_usage k USING (constraint_name, table_schema, table_name) WHERE t.constraint_type = 'PRIMARY KEY' GROUP BY t.table_schema, t.table_name; 2 | -------------------------------------------------------------------------------- /t/test-files/ex21.sql: -------------------------------------------------------------------------------- 1 | SELECT 1 as a \gset 2 | \echo :a \\ SELECT :a; \unset a 3 | SELECT 'test' \g testfile.txt 4 | \! cat testfile.txt 5 | SELECT current_timestamp; 6 | \watch 3 7 | 8 | -------------------------------------------------------------------------------- /t/test-files/ex22.sql: -------------------------------------------------------------------------------- 1 | SELECT code.code, properties_code.valeur, properties_code1.valeur, properties_code2.valeur FROM code INNER JOIN linkcode ON (code.id_code = linkcode.id_codeparent) INNER JOIN linkcode linkcode1 ON (linkcode.id_codeenfant = linkcode1.id_codeparent) INNER JOIN code code1 ON (linkcode1.id_codeenfant = code1.id_code) INNER JOIN properties_code ON (code1.id_code = properties_code.id_code) INNER JOIN properties_code properties_code1 ON (code1.id_code = properties_code1.id_code) INNER JOIN properties_code properties_code2 ON (code1.id_code = properties_code2.id_code) INNER JOIN properties ON (properties_code.id_propriete = properties.id_propriete) INNER JOIN properties properties1 ON (properties_code1.id_propriete = properties1.id_propriete) INNER JOIN properties properties2 ON (properties_code2.id_propriete = properties2.id_propriete) INNER JOIN variables ON (properties.id_variable = variables.id_variable) INNER JOIN variables variables1 ON (properties1.id_variable = variables1.id_variable) INNER JOIN variables variables2 ON (properties2.id_variable = variables2.id_variable) INNER JOIN mvt_temps ON (code.id_code = mvt_temps.id_code) INNER JOIN transactions ON (mvt_temps.id_transaction = transactions.id_transaction) INNER JOIN products ON (code.id_product = products.id_product) WHERE variables.name = 'etat_DEC_BPO_sMacAdresse' AND variables1.name = 'variable_name' AND variables2.name = 'variable_name' AND transactions.code = 'XXXXXXXXXXXXX' AND mvt_temps.statutok = True AND products.codeproduct = '123456789' 2 | -------------------------------------------------------------------------------- /t/test-files/ex23.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO task_item (task_item_id, task_item_description, priority, on_hold, task_type) VALUES (1, 'a desc', 'low', 't', 'an item'); 2 | -------------------------------------------------------------------------------- /t/test-files/ex24.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | id, 3 | count(test), 4 | CASE WHEN true THEN 1 END AS looks_good, 5 | count( CASE WHEN true THEN 1 END) AS looks_wrong_and_indent_is_off, 6 | count( CASE WHEN false THEN 1 END) AS looks_wrong 7 | FROM test; 8 | -------------------------------------------------------------------------------- /t/test-files/ex25.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE demo.test ( 2 | foo text, 3 | bar timestamp WITH time zone, baz text 4 | ) WITH (OIDS = FALSE); 5 | -------------------------------------------------------------------------------- /t/test-files/ex26.sql: -------------------------------------------------------------------------------- 1 | PREPARE demo AS 2 | INSERT INTO demo VALUES (1, 2, 3, 4); 3 | 4 | PREPARE demo AS 5 | SELECT * FROM demo WHERE id IN (1, 2, 3, 4); 6 | 7 | PREPARE demo AS 8 | UPDATE demo SET lbl = 'unknown' WHERE id IN (1, 2, 3, 4); 9 | 10 | PREPARE demo AS 11 | DELETE FROM demo WHERE id IN (1, 2, 3, 4); 12 | 13 | -------------------------------------------------------------------------------- /t/test-files/ex28.sql: -------------------------------------------------------------------------------- 1 | BEGIN; 2 | CREATE FUNCTION basename(path text) 3 | RETURNS text AS 4 | $$ 5 | return path.replace(/.*\//, ''); 6 | $$ 7 | LANGUAGE 'plv8' IMMUTABLE; 8 | COMMIT; 9 | 10 | UPDATE article a SET title = 'x' WHERE (a.perm & 8)::bool 11 | -------------------------------------------------------------------------------- /t/test-files/ex29.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT (current_database())::information_schema.sql_identifier AS view_catalog, (nv.nspname)::information_schema.sql_identifier AS view_schema, (v.relname)::information_schema.sql_identifier AS view_name, (current_database())::information_schema.sql_identifier AS table_catalog, (nt.nspname)::information_schema.sql_identifier AS table_schema, (t.relname)::information_schema.sql_identifier AS table_name FROM pg_namespace nv, pg_class v, pg_depend dv, pg_depend dt, pg_class t, pg_namespace nt WHERE ((((((((((((((nv.oid = v.relnamespace) AND (v.relkind = 'v'::"char")) AND (v.oid = dv.refobjid)) AND (dv.refclassid = ('pg_class'::regclass)::oid)) AND (dv.classid = ('pg_rewrite'::regclass)::oid)) AND (dv.deptype = 'i'::"char")) AND (dv.objid = dt.objid)) AND (dv.refobjid <> dt.refobjid)) AND (dt.classid = ('pg_rewrite'::regclass)::oid)) AND (dt.refclassid = ('pg_class'::regclass)::oid)) AND (dt.refobjid = t.oid)) AND (t.relnamespace = nt.oid)) AND (t.relkind = ANY (ARRAY['r'::"char", 'v'::"char"]))) AND pg_has_role(t.relowner, 'USAGE'::text)) ORDER BY (current_database())::information_schema.sql_identifier, (nv.nspname)::information_schema.sql_identifier, (v.relname)::information_schema.sql_identifier, (current_database())::information_schema.sql_identifier, (nt.nspname)::information_schema.sql_identifier, (t.relname)::information_schema.sql_identifier; 2 | 3 | -------------------------------------------------------------------------------- /t/test-files/ex3.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE test (col text); 2 | INSERT INTO test VALUES ('123'); 3 | 4 | CREATE FUNCTION fonction_reference(refcursor) RETURNS refcursor AS $$ 5 | BEGIN 6 | OPEN $1 FOR SELECT col FROM test; 7 | RETURN $1; 8 | END; 9 | $$ LANGUAGE plpgsql; 10 | 11 | BEGIN; 12 | SELECT fonction_reference('curseur_fonction'); 13 | FETCH ALL IN curseur_fonction; 14 | COMMIT; 15 | -------------------------------------------------------------------------------- /t/test-files/ex30.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE OR REPLACE FUNCTION foo () 3 | RETURNS TRIGGER 4 | AS $$ 5 | BEGIN 6 | IF NEW.role NOT IN ( SELECT rolname FROM pg_authid) THEN 7 | RAISE EXCEPTION 'role % does not exist.', NEW.role; 8 | END IF; 9 | END; 10 | $$ 11 | LANGUAGE 'plpgsql'; 12 | 13 | CREATE OR REPLACE FUNCTION plpython_demo () 14 | RETURNS void 15 | AS $$ 16 | from this import s, d 17 | 18 | for char in s: 19 | print(d.get(char, char), end="") 20 | 21 | print() 22 | $$ 23 | LANGUAGE 'plpython3u'; 24 | 25 | CREATE OR REPLACE FUNCTION plpython_demo2 () 26 | RETURNS void 27 | LANGUAGE 'plpython3u' 28 | AS $body$ 29 | from this import u, f 30 | 31 | for char in u: 32 | print(f.get(char, char), end="") 33 | 34 | print() 35 | $body$ 36 | ; 37 | 38 | CREATE FUNCTION add(integer, integer) RETURNS integer 39 | LANGUAGE sql IMMUTABLE STRICT 40 | AS $_$select $1 + $2;$_$; 41 | 42 | CREATE FUNCTION dup(integer, OUT f1 integer, OUT f2 text) RETURNS record 43 | LANGUAGE sql 44 | AS $_$ SELECT $1, CAST($1 AS text) || ' is text' $_$; 45 | 46 | CREATE TABLE IF NOT EXISTS foo ( 47 | id bigint PRIMARY KEY, 48 | /* 49 | This text will receive an extra level of indentation 50 | every time pg_format is executed 51 | */ 52 | bar text NOT NULL 53 | /* this is the end*/ 54 | ); 55 | 56 | COMMENT ON TABLE xx.yy IS 'Line 1 57 | - Line 2 58 | - Line 3'; 59 | 60 | CREATE TABLE IF NOT EXISTS foo ( 61 | /******************************************************* 62 | * This text will receive an extra level of indentation * 63 | * every time pg_format is executed * 64 | ********************************************************/ 65 | id bigint PRIMARY KEY, 66 | /* 67 | This text will receive an extra level of indentation 68 | every time pg_format is executed 69 | */ 70 | bar text NOT NULL 71 | /* this is the end*/ 72 | ); 73 | 74 | ALTER TABLE app_public.users ENABLE ROW LEVEL SECURITY; 75 | 76 | COMMENT ON FUNCTION my_function () IS 'Here is my function that has a comment; will this become a sql clause or statement?'; 77 | 78 | -------------------------------------------------------------------------------- /t/test-files/ex31.sql: -------------------------------------------------------------------------------- 1 | CREATE PROCEDURE insert_data(a integer, b integer) 2 | LANGUAGE SQL 3 | AS $$ 4 | INSERT INTO tbl VALUES (a); 5 | INSERT INTO tbl VALUES (b); 6 | $$; 7 | 8 | CALL insert_data(1, 2); 9 | 10 | ALTER PROCEDURE insert_data1(integer, integer) RENAME TO insert_record; 11 | ALTER PROCEDURE insert_data2(integer, integer) OWNER TO joe; 12 | ALTER PROCEDURE insert_data3(integer, integer) SET SCHEMA accounting; 13 | ALTER PROCEDURE insert_data4(integer, integer) DEPENDS ON EXTENSION myext; 14 | ALTER PROCEDURE check_password1(text) SET search_path = admin, pg_temp; 15 | ALTER PROCEDURE check_password2(text) RESET search_path; 16 | 17 | ALTER ROUTINE foo1(integer) RENAME TO foobar; 18 | ALTER ROUTINE foo2(integer, varchar(255)) OWNER TO ufoo; 19 | ALTER ROUTINE foo3(integer, varchar(255), boolean) SET SCHEMA sfoo; 20 | ALTER ROUTINE foo4(varchar(25),integer) DEPENDS ON EXTENSION fooext; 21 | ALTER ROUTINE foo5(integer) IMMUTABLE; 22 | ALTER ROUTINE foo6(integer) SECURITY INVOKER; 23 | ALTER ROUTINE foo7(integer) RESET ALL; 24 | ALTER ROUTINE foo8(integer) SET work_mem = '1GB'; 25 | ALTER ROUTINE foo9(integer) SET work_mem FROM CURRENT; 26 | 27 | CREATE PUBLICATION mypublication1 FOR TABLE ONLY emps; 28 | CREATE PUBLICATION mypublication2 FOR TABLE users, departments; 29 | CREATE PUBLICATION alltables FOR ALL TABLES; 30 | CREATE PUBLICATION insert_only FOR 31 | TABLE mydata 32 | WITH ( 33 | publish = 'insert' 34 | ); 35 | 36 | SELECT my_func(p_type_cd => t.type_cd) AS my_func_result FROM tab t WHERE t.id=12; 37 | 38 | -------------------------------------------------------------------------------- /t/test-files/ex32.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE projects ( 2 | id UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4 (), 3 | name citext NOT NULL CHECK (name ~* '^[a-z0-9_-]{3,255}$'), 4 | owner_id UUID NOT NULL, 5 | UNIQUE (owner_id, name), 6 | PRIMARY KEY (id, name) 7 | ); 8 | 9 | CREATE TABLE projects ( 10 | id UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4 (), 11 | name citext NOT NULL CHECK (name ~* '^[a-z0-9_-]{3,255}$'), 12 | owner_id UUID NOT NULL, 13 | UNIQUE name, 14 | PRIMARY KEY id 15 | ); 16 | 17 | CREATE TABLE sample ( 18 | fkey integer REFERENCES other (id) ON UPDATE CASCADE ON DELETE CASCADE, 19 | other integer, 20 | columns integer 21 | ); 22 | 23 | -------------------------------------------------------------------------------- /t/test-files/ex33.sql: -------------------------------------------------------------------------------- 1 | SELECT CASE WHEN my_col IS NOT NULL THEN 'Y' ELSE 'N' END AS my_new_col, CASE WHEN TRIM(my_other_col) = 'confirmed' THEN 'Y' ELSE 'N' END AS new_col FROM my_table; 2 | 3 | select distinct on (a, b) a, b, c from d order by a, b, c; 4 | 5 | select distinct a, b, b, c from d order by a, b, c; 6 | -------------------------------------------------------------------------------- /t/test-files/ex34.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | count(*) filter (where b) as count_b, 3 | count(*) filter (where c) as count_c 4 | FROM a; 5 | 6 | SELECT array_agg(a ORDER BY b DESC) FROM table; 7 | 8 | SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY income) FROM households; 9 | 10 | SELECT 11 | count(*) AS unfiltered, 12 | count(*) FILTER (WHERE i < 5) AS filtered 13 | FROM generate_series(1,10) AS s(i); 14 | 15 | SELECT make, model, GROUPING(make,model), sum(sales) FROM items_sold GROUP BY ROLLUP(make,model); 16 | 17 | -------------------------------------------------------------------------------- /t/test-files/ex35.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE kbln ( 2 | id integer NOT NULL, 3 | blank_series varchar(50) NOT NULL, 4 | company_id varchar(8)) partition by 5 | range ( id); 6 | 7 | create table kbln_p0 partition of kbln for 8 | values from ( minvalue) to ( 500000) partition by hash ( blank_series); 9 | 10 | create table kbln_p0_1 partition of kbln_p0 for 11 | values with ( modulus 2, remainder 0); 12 | 13 | create table kbln_p0_2 partition of kbln_p0 for 14 | values with ( modulus 2, remainder 1); 15 | 16 | alter table t1 detach partition t1_a; 17 | alter table t1 attach partition t1_a for values in (1, 2, 3); 18 | 19 | CREATE TABLE kbln ( 20 | id integer NOT NULL, 21 | blank_series varchar(50) NOT NULL, 22 | company_id varchar(8)) partition by 23 | list ( id); 24 | 25 | select 26 | id, 27 | for_group, 28 | some_val, 29 | sum(some_val) over (partition by for_group order by id) as sum_so_far_in_group, 30 | sum(some_val) over (partition by for_group) as sum_in_group, 31 | sum(some_val) over (partition by for_group order by id range 3 preceding) as sum_current_and_3_preceeding, 32 | sum(some_val) over (partition by for_group order by id range between 3 preceding and 3 following) as sum_current_and_3_preceeding_and_3_following, 33 | sum(some_val) over (partition by for_group order by id range between current row and unbounded following) as sum_current_and_all_following 34 | from test 35 | order by for_group, id; 36 | 37 | -------------------------------------------------------------------------------- /t/test-files/ex36.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION foo () RETURNS TRIGGER AS $$ 2 | BEGIN 3 | CREATE TEMPORARY TABLE tb ( 4 | id integer 5 | ); 6 | SELECT * FROM nothing; 7 | END; 8 | $$ LANGUAGE 'plpgsql'; 9 | 10 | -------------------------------------------------------------------------------- /t/test-files/ex37.sql: -------------------------------------------------------------------------------- 1 | CREATE POLICY can_select_object ON object 2 | FOR SELECT 3 | USING ( 4 | can_do_the_thing(get_current_user(), owner_id) 5 | ); 6 | 7 | CREATE POLICY can_insert_object ON object 8 | FOR INSERT 9 | WITH CHECK ( 10 | can_do_the_thing(get_current_user(), owner_id) 11 | ); 12 | 13 | CREATE POLICY can_update_object ON object 14 | FOR UPDATE 15 | USING ( 16 | can_do_the_thing(get_current_user(), owner_id) 17 | ); 18 | 19 | CREATE POLICY can_delete_object ON object 20 | FOR DELETE 21 | USING ( 22 | can_do_the_thing(get_current_user(), owner_id) 23 | ); 24 | 25 | CREATE POLICY fp_s ON information FOR SELECT 26 | USING (group_id <= (SELECT group_id FROM users WHERE user_name = current_user)); 27 | CREATE POLICY fp_s ON information FOR SELECT 28 | USING (group_id <= 10); 29 | CREATE POLICY p1 ON ec1 USING (f1 < '5'::int8alias1); 30 | 31 | CREATE POLICY fp_s ON information FOR SELECT 32 | WITH CHECK (group_id <= (SELECT group_id FROM users WHERE user_name = current_user)); 33 | CREATE POLICY fp_s ON information FOR SELECT 34 | WITH CHECK (group_id <= 10); 35 | CREATE POLICY p1 ON ec1 WITH CHECK (f1 < '5'::int8alias1); 36 | 37 | CREATE FUNCTION fonction_reference (refcursor) RETURNS refcursor AS $$ 38 | BEGIN 39 | OPEN $1 FOR SELECT col FROM test; 40 | RETURN $1; 41 | END; 42 | $$ 43 | LANGUAGE plpgsql; 44 | 45 | CREATE PUBLICATION all_tables FOR ALL TABLES; 46 | 47 | CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert'); 48 | -------------------------------------------------------------------------------- /t/test-files/ex38.sql: -------------------------------------------------------------------------------- 1 | CREATE TRIGGER check_update 2 | BEFORE UPDATE ON accounts 3 | FOR EACH ROW 4 | EXECUTE PROCEDURE check_account_update(); 5 | 6 | CREATE TRIGGER check_update 7 | BEFORE UPDATE OF balance ON accounts 8 | FOR EACH ROW 9 | EXECUTE PROCEDURE check_account_update(); 10 | 11 | CREATE TRIGGER check_update 12 | BEFORE UPDATE ON accounts 13 | FOR EACH ROW 14 | WHEN (OLD.balance IS DISTINCT FROM NEW.balance) 15 | EXECUTE PROCEDURE check_account_update(); 16 | 17 | -------------------------------------------------------------------------------- /t/test-files/ex39.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE boxes 2 | ADD CONSTRAINT my_constraint 3 | EXCLUDE USING gist 4 | ( some_id WITH =, make_tsrange(created_at, expires_at) WITH &&); 5 | 6 | CREATE TABLE circles ( 7 | c circle, 8 | EXCLUDE USING gist (c WITH &&) 9 | ); 10 | 11 | ALTER TABLE ONLY public.circles 12 | ADD CONSTRAINT circles_c_excl EXCLUDE USING gist (c WITH &&); 13 | 14 | ALTER TABLE truck ADD EXCLUDE 15 | USING gist (id WITH =, system_period WITH &&); 16 | -------------------------------------------------------------------------------- /t/test-files/ex4.sql: -------------------------------------------------------------------------------- 1 | SELECT 1, 2, 10, 'depesz', 'hubert', 'depesz', 'hubert depesz', '1 2 3 4'; 2 | 3 | SELECT tbl_lots.id, to_char ( tbl_lots.dt_crea, 'DD/MM/YYYY HH24:MI:SS' ) AS date_crea FROM tbl_lots WHERE tbl_lots.dt_crea > current_timestamp - interval '1 day' AND tbl_lots.dt_crea > current_timestamp - (dayrett || ' days')::interval AND tbl_lots.type = 'SECRET'; 4 | 5 | SELECT extract( year from school_day ) AS year; 6 | 7 | SELECT substring( firstname from 1 for 10 ) AS sname; 8 | 9 | select * from (select 1 i) a INNER JOIN (select 1 i) b ON (a.i=b.i) inner join (select 1 i) ON (c.i=a.i) where a.i=1 ; 10 | -------------------------------------------------------------------------------- /t/test-files/ex40.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION myfunc() 2 | 3 | RETURNS void 4 | AS 5 | $BODY$ 6 | BEGIN 7 | set client_min_messages to warning; 8 | DROP TABLE IF EXISTS tt_BIP; 9 | DROP TABLE IF EXISTS tt_tmp; 10 | set client_min_messages to notice; 11 | END; 12 | $BODY$ 13 | LANGUAGE plpgsql VOLATILE 14 | COST 100; 15 | 16 | -------------------------------------------------------------------------------- /t/test-files/ex41.sql: -------------------------------------------------------------------------------- 1 | /* Insert values for environment 1 */ 2 | INSERT INTO table 3 | VALUES (1, 2, '3'); 4 | 5 | /* Insert values for environment 2 */ 6 | /* Insert values for environment 3 */ 7 | INSERT INTO table 8 | VALUES (2, 17, 'hello'); 9 | -- New comment for a query 10 | SELECT library.column1, 11 | --------------------- 12 | -- This is a line -- 13 | -- comment in a -- 14 | -- SQL statement -- 15 | --------------------- 16 | library.column2, library.column3 -- inline comment 17 | FROM library; 18 | -------------------------------------------------------------------------------- /t/test-files/ex42.sql: -------------------------------------------------------------------------------- 1 | WITH styles AS ( 2 | SELECT 3 | style, 4 | color 5 | FROM 6 | style_table s 7 | WHERE 8 | s.id = 'D' 9 | ), 10 | reviews AS ( 11 | SELECT 12 | style, body 13 | FROM 14 | review_table 15 | ) 16 | SELECT 17 | * 18 | FROM 19 | styles s; 20 | 21 | 22 | WITH cte AS NOT MATERIALIZED ( 23 | SELECT id, jsonb_strip_nulls ( jsonb_build_object( 'name', name)) AS data FROM t 24 | ) SELECT * FROM cte; 25 | 26 | WITH cte AS ( 27 | SELECT id, jsonb_strip_nulls ( jsonb_build_object( 'name', name)) AS data FROM t 28 | ) SELECT * FROM cte; 29 | 30 | -------------------------------------------------------------------------------- /t/test-files/ex43.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO tt_tmp_wrk1 ( SELECT MAR , ( CASE WHEN MAR = 'SEUIL_DEST' THEN ( SELECT COUNT(*) FROM CUS WHERE MAR.obj4 = ( CASE WHEN MAR.type4 = 'CF' THEN CUS.account_no::text WHEN MAR.type4 = 'CR' THEN CUS.owning_account_no::text WHEN MAR.type4 = 'SI' THEN CUS.point_origin END ) AND ( CUS = ( SELECT DISTINCT DD FROM PCM WHERE PCM.compo = CMF.compo ) OR CUS = ( SELECT DISTINCT CPIR FROM PCM WHERE PCM.compo = CMF.compo ) OR CUS = ( SELECT DISTINCT CPIR FROM PCM, CT, PIDR, CPIR, CMF WHERE PCM.compo = CMF.compo ) ) ) WHEN MAR = 'SEUIL_OBJ' THEN ( SELECT COUNT(*) FROM CPC WHERE MAR.obj4 = ( CASE WHEN MAR.type4 = 'CF' THEN CPC.parent::text WHEN MAR.type4 = 'CR' THEN CPC.parent::text WHEN MAR.type4 = 'SI' THEN CPC.parent::text END ) AND CPC IN ( SELECT SPT FROM SPT WHERE SPT = 'SEUIL_OBJ' AND SPT IN ( SELECT DISTINCT compo FROM CPC WHERE MAR = ( CASE WHEN MAR = 'CF' THEN CPC.parent::text WHEN MAR = 'CR' THEN CPC.parent::text WHEN MAR.type4 = 'SI' THEN CPC.parent::text END ) ) ) ) END ) FROM MAR, BIP WHERE BIP = (MAR)::int ) ; 2 | -------------------------------------------------------------------------------- /t/test-files/ex44.sql: -------------------------------------------------------------------------------- 1 | SELECT * FROM tabl WHERE (a = c) AND (b = c OR c = d); 2 | SELECT * FROM tabl WHERE (a = c AND b = c) OR (c = d); 3 | SELECT * FROM tabl WHERE (a = c) AND (b = c) OR (c = d); 4 | 5 | -------------------------------------------------------------------------------- /t/test-files/ex45.sql: -------------------------------------------------------------------------------- 1 | WITH cte1 AS ( 2 | SELECT 3 | * 4 | FROM 5 | table_a a 6 | -- Comment out or remove this JOIN to fix the indentation issue below 7 | INNER JOIN table_b b ON a.id = b.id 8 | ), 9 | cte2 AS ( 10 | SELECT 11 | CASE WHEN TRUE THEN 12 | TRUE 13 | WHEN NULL IS NULL 14 | OR TRUE = FALSE THEN 15 | NULL 16 | -- Indentation is off starting here 17 | WHEN FALSE 18 | AND TRUE THEN 19 | TRUE 20 | ELSE 21 | FALSE 22 | END AS value 23 | FROM 24 | cte1 25 | -- Indentation is correct after this line 26 | ) 27 | SELECT 28 | * 29 | FROM 30 | cte2; 31 | 32 | SELECT *, 33 | SUM(( SELECT count(*) FROM b )) AS something, SUM(( SELECT count(*) FROM b )) AS something, a.b 34 | FROM 35 | a; 36 | -------------------------------------------------------------------------------- /t/test-files/ex46.sql: -------------------------------------------------------------------------------- 1 | SELECT a, b, c, d FROM t_1, t_2, t3 WHERE a = 10 AND b = 10 AND c = 10 AND d IN (1, 2, 3 , 4, 5 , 6, 7); 2 | 3 | SELECT a, b, c, d FROM t_1, t_2, (SELECT * FROM t6) as t3, t4 WHERE a = 10 AND b = 10 AND c = 10 AND d IN (1, 2, 3 , 4, 5 , 6, 7); 4 | 5 | SELECT '--data' FROM test; 6 | 7 | DECLARE c_mvcc_demo CURSOR FOR 8 | SELECT xmin, xmax, cmax, * 9 | FROM mvcc_demo; 10 | SELECT 1; 11 | 12 | SELECT * FROM pg_class ORDER BY relname; 13 | 14 | SELECT RANK() OVER s AS dept_rank 15 | FROM emp 16 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 17 | ORDER BY department, salary DESC; 18 | 19 | SELECT x, COUNT(x) OVER w, SUM(x) OVER w 20 | FROM generate_series(1, 10) AS f (x) 21 | WINDOW w AS (); 22 | 23 | SELECT name, department, salary, RANK() OVER s AS dept_rank, RANK() OVER () AS global_rank 24 | FROM empa 25 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 26 | ORDER BY department, salary DESC; 27 | 28 | SELECT name, department, salary, RANK() OVER () AS global_rank, RANK() OVER s AS dept_rank 29 | FROM empb 30 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 31 | ORDER BY department, salary DESC; 32 | 33 | UPDATE mvcc_demo 34 | SET val = val + 1 35 | WHERE val > 0; 36 | 37 | WITH driver (name) AS ( 38 | SELECT DISTINCT 39 | unnest(xpath('//driver/text()', 40 | doc))::text 41 | FROM printer 42 | ) 43 | SELECT name 44 | FROM driver 45 | WHERE name LIKE 'hp%' 46 | ORDER BY 1; 47 | 48 | WITH source (x1, x2) AS ( 49 | SELECT 1 50 | ) 51 | SELECT * 52 | FROM source; 53 | 54 | SELECT DISTINCT relkind, relname 55 | FROM pg_class 56 | ORDER BY 1, 2; 57 | 58 | SELECT 59 | salary, 60 | RANK() OVER s 61 | FROM emp 62 | WINDOW s AS ( 63 | ORDER BY salary DESC) 64 | ORDER BY 65 | salary DESC; 66 | 67 | EXPLAIN 68 | (COSTS OFF, ANALYZE 69 | ) 70 | SELECT count(*) 71 | FROM quad_point_tbl 72 | WHERE p IS NULL; 73 | 74 | -------------------------------------------------------------------------------- /t/test-files/ex47.sql: -------------------------------------------------------------------------------- 1 | WITH source AS source2 AS ( 2 | DELETE FROM items USING source2 3 | ) 4 | INSERT INTO old_orders SELECT order_id FROM source2; 5 | 6 | SELECT '--data' FROM test; 7 | 8 | DECLARE c_mvcc_demo CURSOR FOR 9 | SELECT xmin, xmax, cmax, * 10 | FROM mvcc_demo; 11 | SELECT 1; 12 | 13 | SELECT * FROM pg_class ORDER BY relname; 14 | 15 | SELECT RANK() OVER s AS dept_rank 16 | FROM emp 17 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 18 | ORDER BY department, salary DESC; 19 | 20 | SELECT x, COUNT(x) OVER w, SUM(x) OVER w 21 | FROM generate_series(1, 10) AS f (x) 22 | WINDOW w AS (); 23 | 24 | SELECT name, department, salary, RANK() OVER s AS dept_rank, RANK() OVER () AS global_rank 25 | FROM empa 26 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 27 | ORDER BY department, salary DESC; 28 | 29 | SELECT name, department, salary, RANK() OVER () AS global_rank, RANK() OVER s AS dept_rank 30 | FROM empb 31 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 32 | ORDER BY department, salary DESC; 33 | 34 | UPDATE mvcc_demo 35 | SET val = val + 1 36 | WHERE val > 0; 37 | 38 | WITH driver (name) AS ( 39 | SELECT DISTINCT 40 | unnest(xpath('//driver/text()', 41 | doc))::text 42 | FROM printer 43 | ) 44 | SELECT name 45 | FROM driver 46 | WHERE name LIKE 'hp%' 47 | ORDER BY 1; 48 | 49 | WITH source (x1, x2) AS ( 50 | SELECT 1 51 | ) 52 | SELECT * 53 | FROM source; 54 | 55 | SELECT DISTINCT relkind, relname 56 | FROM pg_class 57 | ORDER BY 1, 2; 58 | 59 | SELECT 60 | salary, 61 | RANK() OVER s 62 | FROM emp 63 | WINDOW s AS ( 64 | ORDER BY salary DESC) 65 | ORDER BY 66 | salary DESC; 67 | 68 | CREATE TRIGGER test_trigger_exists 69 | BEFORE UPDATE ON test_exists 70 | FOR EACH ROW EXECUTE PROCEDURE suppress_redundant_updates_trigger(); 71 | 72 | CREATE RULE test_rule_exists AS ON INSERT TO test_exists 73 | DO INSTEAD 74 | INSERT INTO test_exists VALUES (NEW.a, NEW.b || NEW.a::text); 75 | DROP RULE test_rule_exists ON test_exists; 76 | 77 | -------------------------------------------------------------------------------- /t/test-files/ex48.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO test_exists VALUES (NEW.a, NEW.b || NEW.a::text); 2 | 3 | CREATE RULE base_tab_def_view_ins_rule AS ON INSERT TO base_tab_def_view 4 | DO ALSO 5 | INSERT INTO base_tab_def VALUES (new.a, new.b, new.c, new.d, new.e); 6 | 7 | CREATE RULE base_tab_def_view_ins_rule AS ON INSERT TO base_tab_def_view 8 | DO INSTEAD 9 | INSERT INTO base_tab_def VALUES (new.a, new.b, new.c, new.d, new.e); 10 | 11 | CREATE RULE test_rule_exists AS ON INSERT TO test_exists 12 | DO INSTEAD 13 | INSERT INTO test_exists VALUES (NEW.a, NEW.b || NEW.a::text); 14 | 15 | DROP RULE test_rule_exists ON test_exists; 16 | 17 | DROP SEQUENCE test_sequence_exists; 18 | 19 | CREATE GROUP regress_test_g1; 20 | 21 | DROP GROUP regress_test_g1; 22 | 23 | DROP USER IF EXISTS regress_test_u1, regress_test_u2; 24 | 25 | CREATE CONVERSION test_conversion_exists FOR 'LATIN1' TO 'UTF8' 26 | FROM 27 | iso8859_1_to_utf8; 28 | 29 | CREATE DOMAIN test_domain_exists AS int NOT NULL CHECK ( 30 | value > 0 31 | ); 32 | 33 | DROP OPERATOR @#@ (int, int); 34 | 35 | DROP OPERATOR IF EXISTS @#@ (int, int); 36 | 37 | CREATE OPERATOR @#@ 38 | (leftarg = int8, rightarg = int8, procedure = int8xor); 39 | 40 | DROP OPERATOR @#@ (int8, int8); 41 | 42 | create operator alter1.=(procedure = alter1.same, leftarg = alter1.ctype, rightarg = alter1.ctype); 43 | 44 | CREATE OPERATOR !== ( 45 | PROCEDURE = int8ne, 46 | LEFTARG = bigint, 47 | RIGHTARG = bigint, 48 | COMMUTATOR = !==, 49 | NEGATOR = === 50 | ); 51 | 52 | EXPLAIN 53 | (COSTS OFF, ANALYZE 54 | ) 55 | SELECT count(*) 56 | FROM quad_point_tbl 57 | WHERE p IS NULL; 58 | 59 | CREATE FUNCTION sql_is_distinct_from (anyelement, anyelement) 60 | RETURNS boolean 61 | LANGUAGE sql 62 | AS 'INSERT INTO dom_table VALUES (1, 2, 3)' 63 | ; 64 | 65 | INSERT INTO dom_table 66 | VALUES ('1'); 67 | INSERT INTO dom_table 68 | VALUES ('1'); 69 | 70 | CREATE FUNCTION customcontsel(internal, oid, internal, integer) 71 | RETURNS float8 AS 'contsel' LANGUAGE internal STABLE STRICT; 72 | CREATE VIEW attmp_view (unique1) AS SELECT unique1 FROM tenk1; 73 | CREATE VIEW attmp_view (col1, col2) AS SELECT cola, colb FROM tenk1; 74 | 75 | CREATE TABLE a (u) AS SELECT u; 76 | 77 | -------------------------------------------------------------------------------- /t/test-files/ex5.sql: -------------------------------------------------------------------------------- 1 | create or replace function inserta_esquema_pago_backup() returns trigger as $BODY$ 2 | BEGIN 3 | INSERT INTO educaciondistancia.esquema_pago_backup (curso, numpago, montopagar, estatus, usuario, fecha ) VALUES ( NEW.curso, NEW.numpago, NEW.montopagar, NEW.estatus, NEW.usuario, NEW.fecha ); 4 | RETURN NEW; 5 | END; 6 | $BODY$ LANGUAGE plpgsql VOLATILE COST 100; 7 | -------------------------------------------------------------------------------- /t/test-files/ex51.sql: -------------------------------------------------------------------------------- 1 | /* comment1 */ 2 | -- comment2 3 | 4 | select * from table; 5 | 6 | select 7 | * 8 | from 9 | pg_stat_activity 10 | where 11 | state = 'active' -- tabs should be used only for indentation 12 | ; 13 | 14 | 15 | COPY public.subjects (id, name, gender, dataset_id) FROM stdin; 16 | 1 subject1 F 1 17 | 2 subject2 F 1 18 | 3 subject3 F 1 19 | \. 20 | 21 | COPY public.datasets (id, name, units, reference_frame_up, reference_frame_look) FROM stdin; 22 | 1 2020-09-19 cm {0,1,0} {0,0,1} 23 | \. 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /t/test-files/ex55.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION job_next () 2 | RETURNS SETOF job 3 | AS $$ 4 | DECLARE 5 | id uuid; 6 | BEGIN 7 | SELECT 8 | id INTO id 9 | FROM 10 | job 11 | WHERE 12 | state = 'sched' 13 | AND scheduled >= now() 14 | ORDER BY 15 | scheduled, 16 | modified 17 | LIMIT 1 18 | FOR UPDATE SKIP LOCKED; -- ### 1 indent lost 19 | END; 20 | $$ 21 | LANGUAGE plpgsql; 22 | 23 | SELECT 24 | * 25 | FROM ( 26 | SELECT 27 | * 28 | FROM 29 | mytable 30 | FOR UPDATE) AS ss 31 | WHERE 32 | col1 = 5; 33 | 34 | BEGIN; 35 | SELECT 36 | * 37 | FROM 38 | mytable 39 | WHERE 40 | KEY = 1 41 | FOR NO KEY UPDATE; 42 | END; 43 | -------------------------------------------------------------------------------- /t/test-files/ex57.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION fn_opf12 (int4, int2) 2 | RETURNS BIGINT 3 | AS 'SELECT NULL::BIGINT;' 4 | LANGUAGE SQL; 5 | 6 | SELECT 7 | 1::numeriC, 8 | cast(2 aS vArchar(10)); 9 | 10 | SELECT 11 | 1::numeric, 12 | '1.2'::floAt8, 13 | (12)::floAt4, 14 | cast(2 as varChar(10)); 15 | 16 | INSERT INTO ( field_one, field_two, field_three) VALUES ( 1, 2, 3); 17 | 18 | INSERT INTO (field_one, field_two, field_3) VALUES (1,2,3), (4,5,6), (7,8,9); 19 | 20 | CREATE cast( int8 AS int8alias1) WITHOUT FUNCTION; 21 | 22 | CREATE TABLE test11a AS (SELECT 1::PRIV_TESTDOMAIN1 AS a); 23 | SELECT f1, f1::INTERVAL DAY TO MINUTE AS "minutes", 24 | (f1 + INTERVAL '1 month')::INTERVAL MONTH::INTERVAL YEAR AS "years" 25 | FROM interval_tbl; 26 | 27 | CREATE FUNCTION foo ( 28 | bar1 IN text, 29 | bar2 IN date 30 | ) 31 | LANGUAGE plpgsql 32 | AS $procedure$ 33 | DECLARE 34 | foobar1 BIGINT; 35 | foobar2 DATE; 36 | foobar3 TEXT; 37 | foobar4 INT; 38 | BEGIN 39 | NULL; 40 | END 41 | $procedure$; 42 | 43 | SELECT format($$ A %I B %s C %L %% D $$, 'pg_user', 22, NULL); 44 | 45 | create role admin with connection limit 3; 46 | -------------------------------------------------------------------------------- /t/test-files/ex59.sql: -------------------------------------------------------------------------------- 1 | DELIMITER $$ -- We change the delimiter: it is now $$ instead of ; 2 | CREATE PROCEDURE TEST () 3 | BEGIN 4 | SELECT "Hello World"; 5 | END; 6 | $$ -- This is the delimiter that marks the end of the procedure definition. 7 | 8 | DELIMITER ; 9 | 10 | DROP SCHEMA IF EXISTS TEST_ISSUE_191; 11 | CREATE SCHEMA TEST_ISSUE_191; 12 | USE TEST_ISSUE_191; 13 | 14 | CREATE TABLE TEST ( 15 | A INT 16 | ); 17 | 18 | INSERT INTO TEST VALUES (1); 19 | 20 | DELIMITER // 21 | CREATE PROCEDURE PROCEDURE_TEST () 22 | BEGIN 23 | SELECT * 24 | FROM TEST; 25 | END; 26 | // 27 | DELIMITER ; 28 | 29 | CALL PROCEDURE_TEST (); 30 | 31 | DELIMITER $$ 32 | CREATE PROCEDURE PROCEDURE_TEST () 33 | BEGIN 34 | SELECT * 35 | FROM TEST; 36 | END; 37 | $$ 38 | DELIMITER ; 39 | 40 | CALL PROCEDURE_TEST (); 41 | 42 | -------------------------------------------------------------------------------- /t/test-files/ex6.sql: -------------------------------------------------------------------------------- 1 | SELECT attributes->'key' FROM json_test; 2 | 3 | CREATE TABLE foobar ( 4 | id integer NOT NULL, 5 | version integer NOT NULL, 6 | prev_id integer NOT NULL, 7 | prev_version integer NOT NULL, 8 | PRIMARY KEY (id, version), 9 | UNIQUE (prev_id, prev_version), 10 | FOREIGN KEY (prev_id, prev_version) REFERENCES foobar (id, version), 11 | FOREIGN KEY (id, version) REFERENCES barfoo (next_id, next_version) 12 | ); 13 | -------------------------------------------------------------------------------- /t/test-files/ex61.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | myfunction (123, 'ertez', col1), 3 | pg_sleep(12) 4 | FROM 5 | mytbl1; 6 | 7 | CREATE OR REPLACE FUNCTION f() RETURNS BOOL AS '/foo','bar' LANGUAGE C; 8 | 9 | SELECT 10 | lives_ok ('INSERT INTO "order".v_order (status, order_id, name) 11 | VALUES (''complete'', ''' || get_order_id () || ''', '' caleb ''', 'with all parameters'); 12 | 13 | INSERT INTO table1 14 | (id, name) 15 | VALUES 16 | -- 5 min <= duration < 15 min 17 | (10, 'duration'); 18 | 19 | INSERT INTO table1 20 | (id, name) 21 | VALUES 22 | -- name 23 | (10, 'duration') 24 | 25 | -------------------------------------------------------------------------------- /t/test-files/ex62.sql: -------------------------------------------------------------------------------- 1 | -- test placeholder: perl pg_format samples/ex62.sql -w 60 -C -p 'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)' 2 | 3 | -- This code simply contains a long url in a comment. https://github.com/aubertc/pgFormatter/blob/master/t/test-files/ex62.sql 4 | -------------------------------------------------------------------------------- /t/test-files/ex63.sql: -------------------------------------------------------------------------------- 1 | SELECT 1 + coalesce(( SELECT sum(amount) FROM transfers WHERE id IS NOT NULL), 0); 2 | 3 | SELECT id, jsonb_build_object('subs', ( SELECT json_agg(q.col) AS data FROM q WHERE q.id = t.pid)) AS data FROM t; 4 | 5 | SELECT ( SELECT max(( SELECT i.unique2 FROM tenk1 i WHERE i.unique1 = o.unique1))) FROM tenk1 o; 6 | 7 | DELETE FROM xx1 USING ( SELECT * FROM int4_tbl WHERE f1 = xx1.x1) ss; 8 | 9 | DELETE FROM xx1 USING LATERAL ( SELECT * FROM int4_tbl WHERE f1 = x1) ss; 10 | 11 | INSERT INTO shipped_view (ordnum, partnum, value) VALUES (0, 1, ( SELECT COST FROM parts WHERE partnum = '1')); 12 | 13 | SELECT lead(ten, ( SELECT two FROM tenk1 WHERE s.unique2 = unique2)) OVER (PARTITION BY four ORDER BY ten) FROM tenk1 s WHERE unique2 < 10; 14 | 15 | SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM( 16 | SELECT *, 17 | CASE WHEN enroll_date < '2008-01-01' THEN 2008 - extract(YEAR FROM enroll_date) END * 500 AS bonus, 18 | CASE WHEN 19 | AVG(salary) OVER (PARTITION BY depname) < salary 20 | THEN 200 END AS depadj FROM empsalary 21 | )s; 22 | 23 | -------------------------------------------------------------------------------- /t/test-files/ex64.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | * 3 | FROM 4 | accounts 5 | WHERE 6 | type = 'good' 7 | AND amount > 0; 8 | 9 | CREATE OR REPLACE FUNCTION get_from_partitioned_table(partitioned_table.a%type) 10 | RETURNS partitioned_table AS $$ 11 | DECLARE 12 | a_val partitioned_table.a%TYPE; 13 | result partitioned_table%ROWTYPE; 14 | BEGIN 15 | a_val:= $1; 16 | 17 | SELECT * INTO result FROM partitioned_table WHERE a = a_val; 18 | 19 | RETURN result; 20 | END; $$ LANGUAGE plpgsql; 21 | 22 | 23 | -------------------------------------------------------------------------------- /t/test-files/ex65.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | date_trunc('month', 日期) FROM ti; 3 | 4 | SELECT 5 | CASE WHEN test = 1 THEN 6 | (test_table.end_date - ('1 month'::interval) - '1 day'::interval) 7 | ELSE 8 | (test_table.end_date - (test_table.number_days * '1 day'::interval) - '1 day'::interval)::date 9 | END test_start_date 10 | FROM 11 | test_table; 12 | 13 | -------------------------------------------------------------------------------- /t/test-files/ex66.sql: -------------------------------------------------------------------------------- 1 | SELECT col::"text" FROM tab; 2 | 3 | ALTER TABLE tab ALTER COLUMN col TYPE "schema"."dataType" USING col::text::"schema"."dataType"; 4 | -------------------------------------------------------------------------------- /t/test-files/ex67.sql: -------------------------------------------------------------------------------- 1 | SELECT CASE WHEN my_col IS NOT NULL THEN 'Y' ELSE 'N' END AS my_new_col, CASE WHEN TRIM(my_other_col) = 'confirmed' THEN 'Y' ELSE 'N' END AS new_col FROM my_table WHERE col1 = 123 and col2 = 56 and col3 = 'hello'; 2 | 3 | -------------------------------------------------------------------------------- /t/test-files/ex68.sql: -------------------------------------------------------------------------------- 1 | create function rw_view1_trig_fn() 2 | returns trigger as 3 | $$ 4 | begin 5 | if tg_op = 'insert' then 6 | insert into base_tbl values (new.a, new.b); 7 | return new; 8 | elsif tg_op = 'update' then 9 | update base_tbl set b=new.b where a=old.a; 10 | return new; 11 | elsif tg_op = 'delete' then 12 | delete from base_tbl where a=old.a; 13 | return old; 14 | end if; 15 | end; 16 | $$ 17 | language plpgsql; 18 | 19 | 20 | INSERT INTO users ("username", "email", "password") 21 | VALUES ('user1', 'user1@email.com', 'password1'), 22 | ('user2', 'user2@email.com', 'password2'), 23 | ('user3', 'user3@email.com', 'password3'), 24 | ('user4', 'user4@email.com', 'password4'), 25 | ('user5', 'user5@email.com', 'password5'); 26 | 27 | SELECT 'a' 28 | 'b' 29 | 'c', 30 | 'hello'; 31 | 32 | 33 | CREATE SCHEMA hollywood 34 | CREATE TABLE films (title text, release date, awards text[]) 35 | CREATE VIEW winners AS 36 | SELECT title, release FROM films WHERE awards IS NOT NULL; 37 | 38 | 39 | CREATE SCHEMA evttrig 40 | CREATE TABLE one (col_a SERIAL PRIMARY KEY, col_b text DEFAULT 'forty two') 41 | CREATE INDEX one_idx ON one (col_b) 42 | CREATE TABLE two (col_c INTEGER CHECK (col_c > 0) REFERENCES one DEFAULT 42); 43 | 44 | CREATE SCHEMA test_ns_schema_1 45 | CREATE UNIQUE INDEX abc_a_idx ON abc (a) 46 | 47 | CREATE VIEW abc_view AS 48 | SELECT a+1 AS a, b+1 AS b FROM abc 49 | 50 | CREATE TABLE abc ( 51 | a serial, 52 | b int UNIQUE 53 | ) 54 | CREATE UNIQUE INDEX abc_a_idx2 ON abc (b); 55 | 56 | 57 | CREATE TABLE IF NOT EXISTS hello 58 | ( 59 | foo char(20) NOT NULL UNIQUE, 60 | 61 | -- A comment 62 | bar char(25) 63 | ); 64 | 65 | 66 | CREATE TABLE stock 67 | ( 68 | id character varying(6) NOT NULL DEFAULT lpad(cast(nextval('stock_id_seq'::regclass) as character varying(6)), 6, '0'), 69 | part_number text NOT NULL, 70 | quantity integer NOT NULL 71 | ) 72 | WITH ( 73 | OIDS = FALSE 74 | ) 75 | TABLESPACE pg_default; 76 | 77 | -------------------------------------------------------------------------------- /t/test-files/ex69.sql: -------------------------------------------------------------------------------- 1 | copy time 2 | from 's3://mybucket/data/timerows.gz' 3 | iam_role 'arn:aws:iam::0123456789012:role/MyRedshiftRole' 4 | gzip 5 | delimiter '|'; 6 | 7 | 8 | SELECT a.b AS "B", a.c AS "C" FROM a WHERE d IN ( 9 | SELECT e FROM ( SELECT f FF FROM h HH) i 10 | UNION ALL 11 | SELECT j FROM l LL WHERE h.o IS NULL) 12 | ORDER BY 13 | DECODE(p, 'a', SYSDATE, 'b', SYSDATE, 'c', SYSDATE, d), NVL(q, 1) ASC, CASE WHEN r = 0 AND s != 'N' THEN a.y ELSE a.z END ASC; 14 | 15 | -------------------------------------------------------------------------------- /t/test-files/ex7.sql: -------------------------------------------------------------------------------- 1 | -- From 9.4 JSON page - examples 2 | SELECT '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json -> 2; 3 | SELECT '{"a": {"b":"foo"}}'::json -> 'a'; 4 | SELECT '[1,2,3]'::json ->> 2; 5 | SELECT '{"a":1,"b":2}'::json ->> 'b'; 6 | SELECT '{"a": {"b":{"c": "foo"}}}'::json #> '{a,b}'; 7 | SELECT '{"a":[1,2,3],"b":[4,5,6]}'::json #>> '{a,2}'; 8 | -- From 9.4 JSON page - jsonb examples 9 | SELECT '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb; 10 | SELECT '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb; 11 | SELECT '{"a":1, "b":2}'::jsonb ? 'b'; 12 | SELECT '{"a":1, "b":2, "c":3}'::jsonb ?| ARRAY [ 'b', 'c' ]; 13 | SELECT '{"a":1, "b":2, "c":3}'::jsonb ?| ARRAY [ 'b', 'c' ]; 14 | SELECT '["a", "b"]'::jsonb ?& ARRAY [ 'a', 'b' ]; 15 | SELECT '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}', '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'; 16 | -------------------------------------------------------------------------------- /t/test-files/ex70.sql: -------------------------------------------------------------------------------- 1 | SELECT app_public.hello(test); 2 | 3 | DROP FUNCTION IF EXISTS app_public.hello(a text); 4 | 5 | TRUNCATE table001, table002, table003, table004, table005, table006, table007, table008, table009, table010 RESTART IDENTITY CASCADE ; 6 | 7 | TRUNCATE t1; 8 | 9 | TRUNCATE t2 RESTART IDENTITY CASCADE; 10 | -------------------------------------------------------------------------------- /t/test-files/ex71.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE foo ADD COLUMN bar INT; 2 | 3 | CREATE OR REPLACE PROCEDURE do_something () 4 | LANGUAGE plpgsql 5 | AS $$ 6 | DECLARE 7 | salt text; 8 | BEGIN 9 | -- Create a temporary table 10 | CREATE TEMPORARY TABLE IF NOT EXISTS temp_id ( 11 | id varchar 12 | ) ON COMMIT DELETE ROWS; 13 | TRUNCATE TABLE temp_id; 14 | INSERT INTO temp_id SELECT id FROM jones; 15 | INSERT INTO temp_id SELECT id FROM freds; 16 | END; 17 | $$ 18 | -------------------------------------------------------------------------------- /t/test-files/ex72.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION name () 2 | RETURNS text 3 | LANGUAGE plpgsql 4 | SET search_path 5 | FROM 6 | current 7 | AS $$ 8 | BEGIN 9 | RETURN 'text'; 10 | END; 11 | $$; 12 | 13 | -------------------------------------------------------------------------------- /t/test-files/ex73.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION loader_os(OUT o_rc INTEGER, OUT o_err CHARACTER VARYING, IN i_acctoken CHARACTER VARYING, IN i_os TEXT) 2 | RETURNS record 3 | AS $$ 4 | -- Description1 5 | -- Description2 6 | DECLARE 7 | v_os BIGINT; 8 | v_id BIGINT; 9 | BEGIN 10 | SELECT * INTO o_rc, o_err 11 | FROM loader_add i_acctoken; 12 | 13 | -- Description3 14 | IF o_rc != 0 THEN 15 | o_err = '(): ' || o_err; 16 | END IF; 17 | 18 | SELECT ost_id INTO STRICT v_os 19 | FROM os_form 20 | WHERE UPPER(ost_form) = UPPER(i_os); 21 | 22 | SELECT os_id INTO v_id 23 | FROM os_get(v_os); 24 | 25 | UPDATE 26 | mbile 27 | SET os_id = 1 28 | WHERE id = v_id; 29 | 30 | UPDATE 31 | mbsim 32 | SET reg_id = 2 33 | WHERE id = v_id; 34 | END 35 | $$ LANGUAGE plpgsql; 36 | 37 | SELECT 38 | ligne.numligne, 39 | ligne.numero, 40 | ligne.date_mouvement, 41 | ligne.libelle, 42 | ligne.debit, 43 | ligne.credit, 44 | releve.releve_uuid, 45 | releve.date_fin, 46 | compte.compte_uuid, 47 | compte.numcompte, 48 | devise.devise_uuid, 49 | devise.code, 50 | CASE WHEN ligne.typemvt IS NULL THEN 51 | ( 52 | SELECT 53 | typemvt_uuid 54 | FROM 55 | typemvt 56 | WHERE 57 | upper(typemvt.code) = 'PRELEVEMENT') 58 | ELSE 59 | ( 60 | SELECT 61 | typemvt_uuid 62 | FROM 63 | typemvt 64 | WHERE 65 | upper(typemvt.code) = upper(ligne.typemvt)) 66 | END, 67 | ligne.ligne_uuid, 68 | analytique.analytique_uuid 69 | FROM 70 | ligne 71 | INNER JOIN releve ON (releve.releve_uuid = ligne.releve_uuid) 72 | INNER JOIN compte ON (compte.compte_uuid = releve.compte_uuid), 73 | devise, 74 | analytique 75 | WHERE 76 | ligne_uuid = :ligne_uuid 77 | AND devise.defaut = 'O' 78 | AND upper(analytique.groupe) = 'DIVERS'; 79 | 80 | CREATE OR REPLACE FUNCTION loader_os (OUT o_rc integer, OUT o_err character varying, IN i_acctoken character varying, IN i_os text) RETURNS record AS $$ 81 | DECLARE 82 | v_os bigint; 83 | v_id bigint; 84 | BEGIN ATOMIC 85 | SELECT 86 | * INTO o_rc, 87 | o_err 88 | FROM 89 | loader_add i_acctoken; 90 | END 91 | $$ 92 | LANGUAGE plpgsql; 93 | 94 | -------------------------------------------------------------------------------- /t/test-files/ex74.sql: -------------------------------------------------------------------------------- 1 | WITH example AS (SELECT a, b FROM tablename) 2 | INSERT INTO example2 (a, b) SELECT COALESCE(a, 1) as a, b FROM example 3 | -------------------------------------------------------------------------------- /t/test-files/ex75.sql: -------------------------------------------------------------------------------- 1 | WITH example AS (SELECT a, b FROM tablename), 2 | example2 AS (SELECT COALESCE(a, 1) AS a, b FROM example) 3 | INSERT INTO example3 (a, b) 4 | SELECT a, b FROM example 5 | -------------------------------------------------------------------------------- /t/test-files/ex8.sql: -------------------------------------------------------------------------------- 1 | select 'hello', 2+2, 'o\'grady', 0, '', count(*), 'that\'s the position you\'re in now no matter where you select it'; 2 | 3 | select 1e-5, 1e+5, 10 +20, colname+12; 4 | 5 | CREATE TABLE public.sample ( a integer, b integer, c integer); 6 | 7 | CREATE TABLE "public".sample ( a integer, b integer, c integer); 8 | 9 | CREATE TABLE public."sample" ( a integer, b integer, c integer); 10 | 11 | CREATE TABLE "public"."sample" ( a integer, b integer, c integer); 12 | 13 | CREATE TABLE collate_test_fail ( a int, b text COLLATE "ja_JP.eucjp-x-icu"); 14 | 15 | -------------------------------------------------------------------------------- /t/test-files/ex9.sql: -------------------------------------------------------------------------------- 1 | -- test placeholder: perl pg_format samples/ex9.sql -p '<<(?:.*)?>>' 2 | SELECT * FROM projects WHERE projectnumber in <> and username = <>; 3 | 4 | 5 | CREATE TEMPORARY TABLE tt_monthly_data AS 6 | WITH a1 AS ( 7 | SELECT * FROM test1 8 | ) 9 | SELECT ROUND(AVG(t1)) avg_da, ROUND(AVG(t2)) FROM a1; 10 | 11 | insert into videos(sha, idx, title, language, codec, mime_codec, width, height, is_default, bitrate) 12 | values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) 13 | on conflict (sha, idx) do update set 14 | sha = excluded.sha, 15 | idx = excluded.idx, 16 | title = excluded.title, 17 | language = excluded.language, 18 | codec = excluded.codec, 19 | mime_codec = excluded.mime_codec, 20 | width = excluded.width, 21 | height = excluded.height, 22 | is_default = excluded.is_default, 23 | bitrate = excluded.bitrate; 24 | -------------------------------------------------------------------------------- /t/test-files/expected/ex0.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | a, 3 | b, 4 | c 5 | FROM 6 | tablea 7 | JOIN tableb ON (tablea.a = tableb.a) 8 | JOIN tablec ON (tablec.a = tableb.a) 9 | LEFT OUTER JOIN tabled ON (tabled.a = tableb.a) 10 | LEFT JOIN tablee ON (tabled.a = tableb.a) 11 | WHERE 12 | tablea.x = 1 13 | AND tableb.y = 1 14 | GROUP BY 15 | tablea.a, 16 | tablec.c 17 | ORDER BY 18 | tablea.a, 19 | tablec.c; 20 | 21 | -------------------------------------------------------------------------------- /t/test-files/expected/ex10.sql: -------------------------------------------------------------------------------- 1 | WITH a AS ( 2 | SELECT 3 | x, 4 | y, 5 | z 6 | FROM 7 | twelve 8 | JOIN nine ON a = 2 9 | AND b = a 10 | ), 11 | b AS ( 12 | SELECT 13 | * 14 | FROM 15 | a 16 | ) 17 | SELECT 18 | * 19 | FROM 20 | b; 21 | 22 | -------------------------------------------------------------------------------- /t/test-files/expected/ex11.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | * 3 | FROM 4 | a, 5 | ONLY (c) 6 | JOIN b USING (id, id2) 7 | LEFT JOIN d USING (id) 8 | WHERE 9 | id > 10 10 | AND id <= 20; 11 | 12 | CREATE OR REPLACE FUNCTION test_evtrig_no_rewrite () 13 | RETURNS event_trigger 14 | LANGUAGE plpgsql 15 | AS $$ 16 | BEGIN 17 | RAISE NOTICE 'Table ''%'' is being rewritten (reason = %)', pg_event_trigger_table_rewrite_oid ()::regclass, pg_event_trigger_table_rewrite_reason (); 18 | END; 19 | $$; 20 | 21 | SELECT 22 | lives_ok ('INSERT INTO "order".v_order (status, order_id, name) 23 | VALUES (''complete'', ''' || get_order_id () || ''', '' caleb ''', 'with all parameters'); 24 | 25 | PREPARE q AS 26 | SELECT 27 | 'some"text' AS "a""title", 28 | E' \n' AS "junk", 29 | ' ' AS "empty", 30 | n AS int 31 | FROM 32 | generate_series(1, 2) AS n; 33 | 34 | SELECT 35 | websearch_to_tsquery('''abc''''def'''); 36 | 37 | CREATE FUNCTION raise_exprs () 38 | RETURNS void 39 | AS $$ 40 | DECLARE 41 | a integer[] = '{10,20,30}'; 42 | c varchar = 'xyz'; 43 | i integer; 44 | BEGIN 45 | i := 2; 46 | RAISE NOTICE '%; %; %; %; %; %', a, a[i], c, ( 47 | SELECT 48 | c || 'abc'), 49 | ROW (10, 50 | 'aaa', 51 | NULL, 52 | 30), 53 | NULL; 54 | END; 55 | $$ 56 | LANGUAGE plpgsql; 57 | 58 | -------------------------------------------------------------------------------- /t/test-files/expected/ex12.sql: -------------------------------------------------------------------------------- 1 | SET client_encoding TO 'UTF8'; 2 | 3 | UPDATE 4 | weather 5 | SET 6 | temp_lo = temp_lo + 1, 7 | temp_hi = temp_lo + 15, 8 | prcp = DEFAULT 9 | WHERE 10 | city = 'San Francisco' 11 | AND date = '2003-07-03' 12 | RETURNING 13 | temp_lo, 14 | temp_hi, 15 | prcp; 16 | 17 | \set ON_ERROR_STOP ON 18 | SELECT 19 | * 20 | FROM ( 21 | SELECT 22 | * 23 | FROM 24 | mytable 25 | FOR UPDATE) AS ss 26 | WHERE 27 | col1 = 5; 28 | 29 | BEGIN; 30 | SELECT 31 | * 32 | FROM 33 | mytable 34 | WHERE 35 | key = 1 36 | FOR NO KEY UPDATE; 37 | SAVEPOINT s; 38 | UPDATE 39 | mytable 40 | SET 41 | col1 = NULL 42 | WHERE 43 | key = 1; 44 | ROLLBACK TO s; 45 | 46 | -------------------------------------------------------------------------------- /t/test-files/expected/ex13.sql: -------------------------------------------------------------------------------- 1 | WITH RECURSIVE employee_recursive ( 2 | distance, 3 | employee_name, 4 | manager_name 5 | ) AS ( 6 | SELECT 7 | 1, 8 | employee_name, 9 | manager_name 10 | FROM 11 | employee 12 | WHERE 13 | manager_name = 'Mary' 14 | UNION ALL 15 | SELECT 16 | er.distance + 1, 17 | e.employee_name, 18 | e.manager_name 19 | FROM 20 | employee_recursive er, 21 | employee e 22 | WHERE 23 | er.employee_name = e.manager_name 24 | ) 25 | SELECT 26 | distance, 27 | employee_name 28 | FROM 29 | employee_recursive; 30 | 31 | WITH RECURSIVE t ( 32 | nombre 33 | ) AS ( 34 | VALUES (2) 35 | UNION ALL 36 | SELECT 37 | 2 * nombre 38 | FROM 39 | t 40 | WHERE 41 | 2 * nombre < 100 42 | ) 43 | SELECT 44 | nombre 45 | FROM 46 | t; 47 | 48 | CREATE FUNCTION tg_phone_bu () 49 | RETURNS TRIGGER 50 | AS $$ 51 | BEGIN 52 | IF NEW.slotname != OLD.slotname THEN 53 | DELETE FROM PHone 54 | WHERE slotname = OLD.slotname; 55 | INSERT INTO PHone (slotname, comment, slotlink) 56 | VALUES (NEW.slotname, NEW.comment, NEW.slotlink); 57 | RETURN NULL; 58 | END IF; 59 | RETURN new; 60 | END; 61 | $$ 62 | LANGUAGE plpgsql; 63 | 64 | -------------------------------------------------------------------------------- /t/test-files/expected/ex14.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g'); 3 | 4 | SELECT 5 | SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})'); 6 | 7 | SELECT 8 | m.name AS mname, 9 | pname 10 | FROM 11 | manufacturers m, 12 | LATERAL get_product_names (m.id) pname; 13 | 14 | SELECT 15 | m.name AS mname, 16 | pname 17 | FROM 18 | manufacturers m 19 | LEFT JOIN LATERAL get_product_names (m.id) pname ON TRUE; 20 | 21 | WITH one AS ( 22 | SELECT 23 | 1 one 24 | ) 25 | SELECT 26 | count(one), 27 | avg(one) 28 | FROM 29 | one; 30 | 31 | SELECT 32 | * 33 | FROM 34 | a 35 | FULL JOIN b USING (c); 36 | 37 | SELECT 38 | * 39 | FROM 40 | a 41 | FULL OUTER JOIN b USING (c); 42 | 43 | CREATE TYPE jwt_token AS ( 44 | token text, 45 | field: text 46 | ); 47 | 48 | -------------------------------------------------------------------------------- /t/test-files/expected/ex15.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | user_id, 3 | view_homepage, 4 | view_homepage_time, 5 | enter_credit_card, 6 | enter_credit_card_time 7 | FROM ( -- Get the first time each user viewed the homepage. 8 | SELECT 9 | user_id, 10 | 1 AS view_homepage, 11 | min(time) AS view_homepage_time 12 | FROM 13 | event 14 | WHERE 15 | data ->> 'type' = 'view_homepage' 16 | GROUP BY 17 | user_id) e1 18 | LEFT JOIN LATERAL ( -- For each row, get the first time the user_id did the enter_credit_card 19 | -- event, if one exists within two weeks of view_homepage_time. 20 | SELECT 21 | 1 AS enter_credit_card, 22 | time AS enter_credit_card_time 23 | FROM 24 | event 25 | WHERE 26 | user_id = e1.user_id 27 | AND data ->> 'type' = 'enter_credit_card' 28 | AND time BETWEEN view_homepage_time AND (view_homepage_time + 1000 * 60 * 60 * 24 * 14) 29 | ORDER BY 30 | time 31 | LIMIT 1) e2 ON TRUE; 32 | 33 | -------------------------------------------------------------------------------- /t/test-files/expected/ex18.sql: -------------------------------------------------------------------------------- 1 | GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA foo TO role_bar, role_baz; 2 | 3 | SELECT 4 | * 5 | FROM 6 | t 7 | WHERE 8 | a IS NOT DISTINCT FROM b; 9 | 10 | -- Deploy schemas/custom/grants/grant_schema_to_authenticated to pg 11 | -- requires: schemas/custom/schema 12 | BEGIN; 13 | GRANT USAGE ON SCHEMA custom TO authenticated; 14 | GRANT USAGE ON SCHEMA custom TO authenticated; 15 | COMMIT; 16 | 17 | -------------------------------------------------------------------------------- /t/test-files/expected/ex2.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | n.nspname AS "Schema", 3 | p.proname AS "Name", 4 | pg_catalog.pg_get_function_result(p.oid) AS "Result data type", 5 | pg_catalog.pg_get_function_arguments(p.oid) AS "Argument data types", 6 | CASE WHEN p.proisagg THEN 7 | 'agg' 8 | WHEN p.proiswindow THEN 9 | 'window' 10 | WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 11 | 'trigger' 12 | ELSE 13 | 'normal' 14 | END AS "Type" 15 | FROM 16 | pg_catalog.pg_proc p 17 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace 18 | WHERE 19 | p.proname ~ '^(version)$' 20 | AND pg_catalog.pg_function_is_visible(p.oid) 21 | ORDER BY 22 | 1, 23 | 2, 24 | 4; 25 | 26 | SELECT 27 | CASE WHEN (FALSE) THEN 28 | 0 29 | WHEN (TRUE) THEN 30 | 2 31 | END AS dummy1 32 | FROM 33 | my_table; 34 | 35 | CREATE OR REPLACE FUNCTION task_job_maint_after () 36 | RETURNS TRIGGER 37 | AS $$ 38 | BEGIN 39 | CASE NEW.state 40 | WHEN 'final' THEN 41 | NOTIFY task_job_final; 42 | ELSE 43 | NULL; 44 | END CASE; 45 | RETURN NEW; 46 | END; 47 | $$ 48 | LANGUAGE plpgsql; 49 | 50 | -------------------------------------------------------------------------------- /t/test-files/expected/ex20.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | group_concat (k.column_name ORDER BY k.ordinal_position) AS column_names, 3 | t.table_name AS table_name, 4 | t.table_schema AS table_schema, 5 | t.constraint_name AS constraint_name 6 | FROM 7 | information_schema.table_constraints t 8 | LEFT JOIN information_schema.key_column_usage k USING (constraint_name, table_schema, table_name) 9 | WHERE 10 | t.constraint_type = 'PRIMARY KEY' 11 | GROUP BY 12 | t.table_schema, 13 | t.table_name; 14 | 15 | -------------------------------------------------------------------------------- /t/test-files/expected/ex21.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | 1 AS a \gset 3 | 4 | \echo :a \\ SELECT :a; 5 | \unset a 6 | SELECT 7 | 'test' \g testfile.txt 8 | 9 | \! cat testfile.txt 10 | SELECT 11 | CURRENT_TIMESTAMP; 12 | 13 | \watch 3 14 | 15 | -------------------------------------------------------------------------------- /t/test-files/expected/ex22.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | code.code, 3 | properties_code.valeur, 4 | properties_code1.valeur, 5 | properties_code2.valeur 6 | FROM 7 | code 8 | INNER JOIN linkcode ON (code.id_code = linkcode.id_codeparent) 9 | INNER JOIN linkcode linkcode1 ON (linkcode.id_codeenfant = linkcode1.id_codeparent) 10 | INNER JOIN code code1 ON (linkcode1.id_codeenfant = code1.id_code) 11 | INNER JOIN properties_code ON (code1.id_code = properties_code.id_code) 12 | INNER JOIN properties_code properties_code1 ON (code1.id_code = properties_code1.id_code) 13 | INNER JOIN properties_code properties_code2 ON (code1.id_code = properties_code2.id_code) 14 | INNER JOIN properties ON (properties_code.id_propriete = properties.id_propriete) 15 | INNER JOIN properties properties1 ON (properties_code1.id_propriete = properties1.id_propriete) 16 | INNER JOIN properties properties2 ON (properties_code2.id_propriete = properties2.id_propriete) 17 | INNER JOIN variables ON (properties.id_variable = variables.id_variable) 18 | INNER JOIN variables variables1 ON (properties1.id_variable = variables1.id_variable) 19 | INNER JOIN variables variables2 ON (properties2.id_variable = variables2.id_variable) 20 | INNER JOIN mvt_temps ON (code.id_code = mvt_temps.id_code) 21 | INNER JOIN transactions ON (mvt_temps.id_transaction = transactions.id_transaction) 22 | INNER JOIN products ON (code.id_product = products.id_product) 23 | WHERE 24 | variables.name = 'etat_DEC_BPO_sMacAdresse' 25 | AND variables1.name = 'variable_name' 26 | AND variables2.name = 'variable_name' 27 | AND transactions.code = 'XXXXXXXXXXXXX' 28 | AND mvt_temps.statutok = TRUE 29 | AND products.codeproduct = '123456789' 30 | -------------------------------------------------------------------------------- /t/test-files/expected/ex23.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO task_item (task_item_id, task_item_description, priority, on_hold, task_type) 2 | VALUES (1, 'a desc', 'low', 't', 'an item'); 3 | 4 | -------------------------------------------------------------------------------- /t/test-files/expected/ex24.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | id, 3 | count(test), 4 | CASE WHEN TRUE THEN 5 | 1 6 | END AS looks_good, 7 | count( 8 | CASE WHEN TRUE THEN 9 | 1 10 | END) AS looks_wrong_and_indent_is_off, 11 | count( 12 | CASE WHEN FALSE THEN 13 | 1 14 | END) AS looks_wrong 15 | FROM 16 | test; 17 | 18 | -------------------------------------------------------------------------------- /t/test-files/expected/ex25.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE demo.test ( 2 | foo text, 3 | bar timestamp with time zone, 4 | baz text 5 | ) 6 | WITH ( 7 | OIDS = FALSE 8 | ); 9 | 10 | -------------------------------------------------------------------------------- /t/test-files/expected/ex26.sql: -------------------------------------------------------------------------------- 1 | PREPARE demo AS 2 | INSERT INTO demo 3 | VALUES (1, 2, 3, 4); 4 | 5 | PREPARE demo AS 6 | SELECT 7 | * 8 | FROM 9 | demo 10 | WHERE 11 | id IN (1, 2, 3, 4); 12 | 13 | PREPARE demo AS 14 | UPDATE 15 | demo 16 | SET 17 | lbl = 'unknown' 18 | WHERE 19 | id IN (1, 2, 3, 4); 20 | 21 | PREPARE demo AS 22 | DELETE FROM demo 23 | WHERE id IN (1, 2, 3, 4); 24 | 25 | -------------------------------------------------------------------------------- /t/test-files/expected/ex28.sql: -------------------------------------------------------------------------------- 1 | BEGIN; 2 | CREATE FUNCTION basename (path text) 3 | RETURNS text 4 | AS $$ 5 | return path.replace(/.*\//, ''); 6 | $$ 7 | LANGUAGE 'plv8' 8 | IMMUTABLE; 9 | COMMIT; 10 | 11 | UPDATE 12 | article a 13 | SET 14 | title = 'x' 15 | WHERE (a.perm & 8)::bool 16 | -------------------------------------------------------------------------------- /t/test-files/expected/ex29.sql: -------------------------------------------------------------------------------- 1 | SELECT DISTINCT 2 | (current_database())::information_schema.sql_identifier AS view_catalog, 3 | (nv.nspname)::information_schema.sql_identifier AS view_schema, 4 | (v.relname)::information_schema.sql_identifier AS view_name, 5 | (current_database())::information_schema.sql_identifier AS table_catalog, 6 | (nt.nspname)::information_schema.sql_identifier AS table_schema, 7 | (t.relname)::information_schema.sql_identifier AS table_name 8 | FROM 9 | pg_namespace nv, 10 | pg_class v, 11 | pg_depend dv, 12 | pg_depend dt, 13 | pg_class t, 14 | pg_namespace nt 15 | WHERE ((((((((((((((nv.oid = v.relnamespace) 16 | AND (v.relkind = 'v'::"char")) 17 | AND (v.oid = dv.refobjid)) 18 | AND (dv.refclassid = ('pg_class'::regclass)::oid)) 19 | AND (dv.classid = ('pg_rewrite'::regclass)::oid)) 20 | AND (dv.deptype = 'i'::"char")) 21 | AND (dv.objid = dt.objid)) 22 | AND (dv.refobjid <> dt.refobjid)) 23 | AND (dt.classid = ('pg_rewrite'::regclass)::oid)) 24 | AND (dt.refclassid = ('pg_class'::regclass)::oid)) 25 | AND (dt.refobjid = t.oid)) 26 | AND (t.relnamespace = nt.oid)) 27 | AND (t.relkind = ANY (ARRAY['r'::"char", 'v'::"char"]))) 28 | AND pg_has_role(t.relowner, 'USAGE'::text)) 29 | ORDER BY 30 | (current_database())::information_schema.sql_identifier, 31 | (nv.nspname)::information_schema.sql_identifier, 32 | (v.relname)::information_schema.sql_identifier, 33 | (current_database())::information_schema.sql_identifier, 34 | (nt.nspname)::information_schema.sql_identifier, 35 | (t.relname)::information_schema.sql_identifier; 36 | 37 | -------------------------------------------------------------------------------- /t/test-files/expected/ex3.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE test ( 2 | col text 3 | ); 4 | 5 | INSERT INTO test 6 | VALUES ('123'); 7 | 8 | CREATE FUNCTION fonction_reference (refcursor) 9 | RETURNS refcursor 10 | AS $$ 11 | BEGIN 12 | OPEN $1 FOR 13 | SELECT 14 | col 15 | FROM 16 | test; 17 | RETURN $1; 18 | END; 19 | $$ 20 | LANGUAGE plpgsql; 21 | 22 | BEGIN; 23 | SELECT 24 | fonction_reference ('curseur_fonction'); 25 | FETCH ALL IN curseur_fonction; 26 | COMMIT; 27 | 28 | -------------------------------------------------------------------------------- /t/test-files/expected/ex31.sql: -------------------------------------------------------------------------------- 1 | CREATE PROCEDURE insert_data (a integer, b integer) 2 | LANGUAGE SQL 3 | AS $$ 4 | INSERT INTO tbl 5 | VALUES (a); 6 | INSERT INTO tbl 7 | VALUES (b); 8 | $$; 9 | 10 | CALL insert_data (1, 2); 11 | 12 | ALTER PROCEDURE insert_data1 (integer, integer) RENAME TO insert_record; 13 | 14 | ALTER PROCEDURE insert_data2 (integer, integer) OWNER TO joe; 15 | 16 | ALTER PROCEDURE insert_data3 (integer, integer) SET SCHEMA accounting; 17 | 18 | ALTER PROCEDURE insert_data4 (integer, integer) DEPENDS ON EXTENSION myext; 19 | 20 | ALTER PROCEDURE check_password1 (text) SET search_path = admin, pg_temp; 21 | 22 | ALTER PROCEDURE check_password2 (text) RESET search_path; 23 | 24 | ALTER ROUTINE foo1 (integer) RENAME TO foobar; 25 | 26 | ALTER ROUTINE foo2 (integer, varchar(255)) OWNER TO ufoo; 27 | 28 | ALTER ROUTINE foo3 (integer, varchar(255), boolean) SET SCHEMA sfoo; 29 | 30 | ALTER ROUTINE foo4 (varchar(25), integer) DEPENDS ON EXTENSION fooext; 31 | 32 | ALTER ROUTINE foo5 (integer) IMMUTABLE; 33 | 34 | ALTER ROUTINE foo6 (integer) SECURITY INVOKER; 35 | 36 | ALTER ROUTINE foo7 (integer) RESET ALL; 37 | 38 | ALTER ROUTINE foo8 (integer) SET work_mem = '1GB'; 39 | 40 | ALTER ROUTINE foo9 (integer) SET work_mem FROM CURRENT; 41 | 42 | CREATE PUBLICATION mypublication1 FOR TABLE ONLY emps; 43 | 44 | CREATE PUBLICATION mypublication2 FOR TABLE users, departments; 45 | 46 | CREATE PUBLICATION alltables FOR ALL TABLES; 47 | 48 | CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert'); 49 | 50 | SELECT 51 | my_func (p_type_cd => t.type_cd) AS my_func_result 52 | FROM 53 | tab t 54 | WHERE 55 | t.id = 12; 56 | 57 | -------------------------------------------------------------------------------- /t/test-files/expected/ex32.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE projects ( 2 | id uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4 (), 3 | name citext NOT NULL CHECK (name ~* '^[a-z0-9_-]{3,255}$'), 4 | owner_id uuid NOT NULL, 5 | UNIQUE (owner_id, name), 6 | PRIMARY KEY (id, name) 7 | ); 8 | 9 | CREATE TABLE projects ( 10 | id uuid PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4 (), 11 | name citext NOT NULL CHECK (name ~* '^[a-z0-9_-]{3,255}$'), 12 | owner_id uuid NOT NULL, 13 | UNIQUE name, 14 | PRIMARY KEY id 15 | ); 16 | 17 | CREATE TABLE sample ( 18 | fkey integer REFERENCES other (id) ON UPDATE CASCADE ON DELETE CASCADE, 19 | other integer, 20 | columns integer 21 | ); 22 | 23 | -------------------------------------------------------------------------------- /t/test-files/expected/ex33.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | CASE WHEN my_col IS NOT NULL THEN 3 | 'Y' 4 | ELSE 5 | 'N' 6 | END AS my_new_col, 7 | CASE WHEN TRIM(my_other_col) = 'confirmed' THEN 8 | 'Y' 9 | ELSE 10 | 'N' 11 | END AS new_col 12 | FROM 13 | my_table; 14 | 15 | SELECT DISTINCT ON (a, b) 16 | a, 17 | b, 18 | c 19 | FROM 20 | d 21 | ORDER BY 22 | a, 23 | b, 24 | c; 25 | 26 | SELECT DISTINCT 27 | a, 28 | b, 29 | b, 30 | c 31 | FROM 32 | d 33 | ORDER BY 34 | a, 35 | b, 36 | c; 37 | 38 | -------------------------------------------------------------------------------- /t/test-files/expected/ex34.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | count(*) FILTER (WHERE b) AS count_b, 3 | count(*) FILTER (WHERE c) AS count_c 4 | FROM 5 | a; 6 | 7 | SELECT 8 | array_agg(a ORDER BY b DESC) 9 | FROM 10 | TABLE; 11 | 12 | SELECT 13 | percentile_cont(0.5) WITHIN GROUP (ORDER BY income) 14 | FROM 15 | households; 16 | 17 | SELECT 18 | count(*) AS unfiltered, 19 | count(*) FILTER (WHERE i < 5) AS filtered 20 | FROM 21 | generate_series(1, 10) AS s (i); 22 | 23 | SELECT 24 | make, 25 | model, 26 | GROUPING (make, model), 27 | sum(sales) 28 | FROM 29 | items_sold 30 | GROUP BY 31 | ROLLUP (make, model); 32 | 33 | -------------------------------------------------------------------------------- /t/test-files/expected/ex35.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE kbln ( 2 | id integer NOT NULL, 3 | blank_series varchar(50) NOT NULL, 4 | company_id varchar(8) 5 | ) 6 | PARTITION BY RANGE (id); 7 | 8 | CREATE TABLE kbln_p0 PARTITION OF kbln 9 | FOR VALUES FROM (MINVALUE) TO (500000) 10 | PARTITION BY HASH (blank_series); 11 | 12 | CREATE TABLE kbln_p0_1 PARTITION OF kbln_p0 13 | FOR VALUES WITH (MODULUS 2, REMAINDER 0); 14 | 15 | CREATE TABLE kbln_p0_2 PARTITION OF kbln_p0 16 | FOR VALUES WITH (MODULUS 2, REMAINDER 1); 17 | 18 | ALTER TABLE t1 DETACH PARTITION t1_a; 19 | 20 | ALTER TABLE t1 ATTACH PARTITION t1_a 21 | FOR VALUES IN (1, 2, 3); 22 | 23 | CREATE TABLE kbln ( 24 | id integer NOT NULL, 25 | blank_series varchar(50) NOT NULL, 26 | company_id varchar(8) 27 | ) 28 | PARTITION BY LIST (id); 29 | 30 | SELECT 31 | id, 32 | for_group, 33 | some_val, 34 | sum(some_val) OVER (PARTITION BY for_group ORDER BY id) AS sum_so_far_in_group, 35 | sum(some_val) OVER (PARTITION BY for_group) AS sum_in_group, 36 | sum(some_val) OVER (PARTITION BY for_group ORDER BY id RANGE 3 PRECEDING) AS sum_current_and_3_preceeding, 37 | sum(some_val) OVER (PARTITION BY for_group ORDER BY id RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING) AS sum_current_and_3_preceeding_and_3_following, 38 | sum(some_val) OVER (PARTITION BY for_group ORDER BY id RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS sum_current_and_all_following 39 | FROM 40 | test 41 | ORDER BY 42 | for_group, 43 | id; 44 | 45 | -------------------------------------------------------------------------------- /t/test-files/expected/ex36.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION foo () 2 | RETURNS TRIGGER 3 | AS $$ 4 | BEGIN 5 | CREATE TEMPORARY TABLE tb ( 6 | id integer 7 | ); 8 | SELECT 9 | * 10 | FROM 11 | NOTHING; 12 | END; 13 | $$ 14 | LANGUAGE 'plpgsql'; 15 | 16 | -------------------------------------------------------------------------------- /t/test-files/expected/ex37.sql: -------------------------------------------------------------------------------- 1 | CREATE POLICY can_select_object ON object 2 | FOR SELECT 3 | USING (can_do_the_thing (get_current_user (), owner_id)); 4 | 5 | CREATE POLICY can_insert_object ON object 6 | FOR INSERT 7 | WITH CHECK (can_do_the_thing (get_current_user (), owner_id)); 8 | 9 | CREATE POLICY can_update_object ON object 10 | FOR UPDATE 11 | USING (can_do_the_thing (get_current_user (), owner_id)); 12 | 13 | CREATE POLICY can_delete_object ON object 14 | FOR DELETE 15 | USING (can_do_the_thing (get_current_user (), owner_id)); 16 | 17 | CREATE POLICY fp_s ON information 18 | FOR SELECT 19 | USING (group_id <= ( 20 | SELECT 21 | group_id 22 | FROM 23 | users 24 | WHERE 25 | user_name = CURRENT_USER)); 26 | 27 | CREATE POLICY fp_s ON information 28 | FOR SELECT 29 | USING (group_id <= 10); 30 | 31 | CREATE POLICY p1 ON ec1 32 | USING (f1 < '5'::int8alias1); 33 | 34 | CREATE POLICY fp_s ON information 35 | FOR SELECT 36 | WITH CHECK (group_id <= ( 37 | SELECT 38 | group_id 39 | FROM 40 | users 41 | WHERE 42 | user_name = CURRENT_USER)); 43 | 44 | CREATE POLICY fp_s ON information 45 | FOR SELECT 46 | WITH CHECK (group_id <= 10); 47 | 48 | CREATE POLICY p1 ON ec1 49 | WITH CHECK (f1 < '5'::int8alias1); 50 | 51 | CREATE FUNCTION fonction_reference (refcursor) 52 | RETURNS refcursor 53 | AS $$ 54 | BEGIN 55 | OPEN $1 FOR 56 | SELECT 57 | col 58 | FROM 59 | test; 60 | RETURN $1; 61 | END; 62 | $$ 63 | LANGUAGE plpgsql; 64 | 65 | CREATE PUBLICATION all_tables FOR ALL TABLES; 66 | 67 | CREATE PUBLICATION insert_only FOR TABLE mydata WITH (publish = 'insert'); 68 | 69 | -------------------------------------------------------------------------------- /t/test-files/expected/ex38.sql: -------------------------------------------------------------------------------- 1 | CREATE TRIGGER check_update 2 | BEFORE UPDATE ON accounts 3 | FOR EACH ROW 4 | EXECUTE PROCEDURE check_account_update (); 5 | 6 | CREATE TRIGGER check_update 7 | BEFORE UPDATE OF balance ON accounts 8 | FOR EACH ROW 9 | EXECUTE PROCEDURE check_account_update (); 10 | 11 | CREATE TRIGGER check_update 12 | BEFORE UPDATE ON accounts 13 | FOR EACH ROW 14 | WHEN (OLD.balance IS DISTINCT FROM NEW.balance) 15 | EXECUTE PROCEDURE check_account_update (); 16 | 17 | -------------------------------------------------------------------------------- /t/test-files/expected/ex39.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE boxes 2 | ADD CONSTRAINT my_constraint 3 | EXCLUDE USING gist (some_id WITH =, make_tsrange(created_at, expires_at) WITH &&); 4 | 5 | CREATE TABLE circles ( 6 | c circle, 7 | EXCLUDE USING gist (c WITH &&) 8 | ); 9 | 10 | ALTER TABLE ONLY public.circles 11 | ADD CONSTRAINT circles_c_excl 12 | EXCLUDE USING gist (c WITH &&); 13 | 14 | ALTER TABLE truck 15 | ADD EXCLUDE USING gist (id WITH =, system_period WITH &&); 16 | 17 | -------------------------------------------------------------------------------- /t/test-files/expected/ex4.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | 1, 3 | 2, 4 | 10, 5 | 'depesz', 6 | 'hubert', 7 | 'depesz', 8 | 'hubert depesz', 9 | '1 2 3 4'; 10 | 11 | SELECT 12 | tbl_lots.id, 13 | to_char(tbl_lots.dt_crea, 'DD/MM/YYYY HH24:MI:SS') AS date_crea 14 | FROM 15 | tbl_lots 16 | WHERE 17 | tbl_lots.dt_crea > CURRENT_TIMESTAMP - interval '1 day' 18 | AND tbl_lots.dt_crea > CURRENT_TIMESTAMP - (dayrett || ' days')::interval 19 | AND tbl_lots.type = 'SECRET'; 20 | 21 | SELECT 22 | extract(year FROM school_day) AS year; 23 | 24 | SELECT 25 | substring(firstname FROM 1 FOR 10) AS sname; 26 | 27 | SELECT 28 | * 29 | FROM ( 30 | SELECT 31 | 1 i) a 32 | INNER JOIN ( 33 | SELECT 34 | 1 i) b ON (a.i = b.i) 35 | INNER JOIN ( 36 | SELECT 37 | 1 i) ON (c.i = a.i) 38 | WHERE 39 | a.i = 1; 40 | 41 | -------------------------------------------------------------------------------- /t/test-files/expected/ex40.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION myfunc () 2 | RETURNS void 3 | AS $BODY$ 4 | BEGIN 5 | SET client_min_messages TO warning; 6 | DROP TABLE IF EXISTS tt_BIP; 7 | DROP TABLE IF EXISTS tt_tmp; 8 | SET client_min_messages TO notice; 9 | END; 10 | $BODY$ 11 | LANGUAGE plpgsql 12 | VOLATILE 13 | COST 100; 14 | 15 | -------------------------------------------------------------------------------- /t/test-files/expected/ex41.sql: -------------------------------------------------------------------------------- 1 | /* Insert values for environment 1 */ 2 | INSERT INTO TABLE 3 | VALUES (1, 2, '3'); 4 | 5 | 6 | /* Insert values for environment 2 */ 7 | /* Insert values for environment 3 */ 8 | INSERT INTO TABLE 9 | VALUES (2, 17, 'hello'); 10 | 11 | -- New comment for a query 12 | SELECT 13 | library.column1, 14 | --------------------- 15 | -- This is a line -- 16 | -- comment in a -- 17 | -- SQL statement -- 18 | --------------------- 19 | library.column2, 20 | library.column3 -- inline comment 21 | FROM 22 | library; 23 | 24 | -------------------------------------------------------------------------------- /t/test-files/expected/ex42.sql: -------------------------------------------------------------------------------- 1 | WITH styles AS ( 2 | SELECT 3 | style, 4 | color 5 | FROM 6 | style_table s 7 | WHERE 8 | s.id = 'D' 9 | ), 10 | reviews AS ( 11 | SELECT 12 | style, 13 | body 14 | FROM 15 | review_table 16 | ) 17 | SELECT 18 | * 19 | FROM 20 | styles s; 21 | 22 | WITH cte AS NOT MATERIALIZED ( 23 | SELECT 24 | id, 25 | jsonb_strip_nulls (jsonb_build_object('name', name)) AS data 26 | FROM 27 | t 28 | ) 29 | SELECT 30 | * 31 | FROM 32 | cte; 33 | 34 | WITH cte AS ( 35 | SELECT 36 | id, 37 | jsonb_strip_nulls (jsonb_build_object('name', name)) AS data 38 | FROM 39 | t 40 | ) 41 | SELECT 42 | * 43 | FROM 44 | cte; 45 | 46 | -------------------------------------------------------------------------------- /t/test-files/expected/ex44.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | * 3 | FROM 4 | tabl 5 | WHERE (a = c) 6 | AND (b = c 7 | OR c = d); 8 | 9 | SELECT 10 | * 11 | FROM 12 | tabl 13 | WHERE (a = c 14 | AND b = c) 15 | OR (c = d); 16 | 17 | SELECT 18 | * 19 | FROM 20 | tabl 21 | WHERE (a = c) 22 | AND (b = c) 23 | OR (c = d); 24 | 25 | -------------------------------------------------------------------------------- /t/test-files/expected/ex45.sql: -------------------------------------------------------------------------------- 1 | WITH cte1 AS ( 2 | SELECT 3 | * 4 | FROM 5 | table_a a 6 | -- Comment out or remove this JOIN to fix the indentation issue below 7 | INNER JOIN table_b b ON a.id = b.id 8 | ), 9 | cte2 AS ( 10 | SELECT 11 | CASE WHEN TRUE THEN 12 | TRUE 13 | WHEN NULL IS NULL 14 | OR TRUE = FALSE THEN 15 | NULL 16 | -- Indentation is off starting here 17 | WHEN FALSE 18 | AND TRUE THEN 19 | TRUE 20 | ELSE 21 | FALSE 22 | END AS value 23 | FROM 24 | cte1 25 | -- Indentation is correct after this line 26 | ) 27 | SELECT 28 | * 29 | FROM 30 | cte2; 31 | 32 | SELECT 33 | *, 34 | SUM(( 35 | SELECT 36 | count(*) 37 | FROM b)) AS something, 38 | SUM(( 39 | SELECT 40 | count(*) 41 | FROM b)) AS something, 42 | a.b 43 | FROM 44 | a; 45 | 46 | -------------------------------------------------------------------------------- /t/test-files/expected/ex46.sql: -------------------------------------------------------------------------------- 1 | SELECT a, b, c, d 2 | FROM t_1, t_2, t3 3 | WHERE a = 10 4 | AND b = 10 5 | AND c = 10 6 | AND d IN (1, 2, 3, 4, 5, 6, 7); 7 | 8 | SELECT a, b, c, d 9 | FROM t_1, t_2, ( 10 | SELECT * 11 | FROM t6) AS t3, t4 12 | WHERE a = 10 13 | AND b = 10 14 | AND c = 10 15 | AND d IN (1, 2, 3, 4, 5, 6, 7); 16 | 17 | SELECT '--data' 18 | FROM test; 19 | 20 | DECLARE c_mvcc_demo CURSOR FOR 21 | SELECT xmin, xmax, cmax, * 22 | FROM mvcc_demo; 23 | 24 | SELECT 1; 25 | 26 | SELECT * 27 | FROM pg_class 28 | ORDER BY relname; 29 | 30 | SELECT RANK() OVER s AS dept_rank 31 | FROM emp 32 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 33 | ORDER BY department, salary DESC; 34 | 35 | SELECT x, COUNT(x) OVER w, SUM(x) OVER w 36 | FROM generate_series(1, 10) AS f (x) 37 | WINDOW w AS (); 38 | 39 | SELECT name, department, salary, RANK() OVER s AS dept_rank, 40 | RANK() OVER () AS global_rank 41 | FROM empa 42 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 43 | ORDER BY department, salary DESC; 44 | 45 | SELECT name, department, salary, RANK() OVER () AS global_rank, 46 | RANK() OVER s AS dept_rank 47 | FROM empb 48 | WINDOW s AS (PARTITION BY department ORDER BY salary DESC) 49 | ORDER BY department, salary DESC; 50 | 51 | UPDATE 52 | mvcc_demo 53 | SET val = val + 1 54 | WHERE val > 0; 55 | 56 | WITH driver (name) AS ( 57 | SELECT DISTINCT unnest(xpath('//driver/text()', doc))::text 58 | FROM printer 59 | ) 60 | SELECT name 61 | FROM driver 62 | WHERE name LIKE 'hp%' 63 | ORDER BY 1; 64 | 65 | WITH source (x1, x2) AS ( 66 | SELECT 1 67 | ) 68 | SELECT * 69 | FROM source; 70 | 71 | SELECT DISTINCT relkind, relname 72 | FROM pg_class 73 | ORDER BY 1, 2; 74 | 75 | SELECT salary, RANK() OVER s 76 | FROM emp 77 | WINDOW s AS (ORDER BY salary DESC) 78 | ORDER BY salary DESC; 79 | 80 | EXPLAIN (COSTS OFF, ANALYZE) 81 | SELECT count(*) 82 | FROM quad_point_tbl 83 | WHERE p IS NULL; 84 | 85 | -------------------------------------------------------------------------------- /t/test-files/expected/ex5.sql: -------------------------------------------------------------------------------- 1 | CREATE OR REPLACE FUNCTION inserta_esquema_pago_backup () 2 | RETURNS TRIGGER 3 | AS $BODY$ 4 | BEGIN 5 | INSERT INTO educaciondistancia.esquema_pago_backup (curso, numpago, montopagar, estatus, usuario, fecha) 6 | VALUES (NEW.curso, NEW.numpago, NEW.montopagar, NEW.estatus, NEW.usuario, NEW.fecha); 7 | RETURN NEW; 8 | END; 9 | $BODY$ 10 | LANGUAGE plpgsql 11 | VOLATILE 12 | COST 100; 13 | 14 | -------------------------------------------------------------------------------- /t/test-files/expected/ex51.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | * 3 | FROM 4 | TABLE; 5 | 6 | SELECT 7 | * 8 | FROM 9 | pg_stat_activity 10 | WHERE 11 | state = 'active'; 12 | 13 | COPY public.subjects (id, name, gender, dataset_id) FROM stdin; 14 | 1 subject1 F 1 15 | 2 subject2 F 1 16 | 3 subject3 F 1 17 | \. 18 | 19 | COPY public.datasets (id, name, units, reference_frame_up, reference_frame_look) FROM stdin; 20 | 1 2020-09-19 cm {0,1,0} {0,0,1} 21 | \. 22 | 23 | -------------------------------------------------------------------------------- /t/test-files/expected/ex55.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION job_next () 2 | RETURNS SETOF job 3 | AS $$ 4 | DECLARE 5 | id uuid; 6 | BEGIN 7 | SELECT 8 | id INTO id 9 | FROM 10 | job 11 | WHERE 12 | state = 'sched' 13 | AND scheduled >= now() 14 | ORDER BY 15 | scheduled, 16 | modified 17 | LIMIT 1 18 | FOR UPDATE 19 | SKIP LOCKED; 20 | -- ### 1 indent lost 21 | END; 22 | $$ 23 | LANGUAGE plpgsql; 24 | 25 | SELECT 26 | * 27 | FROM ( 28 | SELECT 29 | * 30 | FROM 31 | mytable 32 | FOR UPDATE) AS ss 33 | WHERE 34 | col1 = 5; 35 | 36 | BEGIN; 37 | SELECT 38 | * 39 | FROM 40 | mytable 41 | WHERE 42 | KEY = 1 43 | FOR NO KEY UPDATE; 44 | END; 45 | -------------------------------------------------------------------------------- /t/test-files/expected/ex57.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION fn_opf12 (INT4, INT2) 2 | RETURNS BIGINT 3 | AS ' 4 | SELECT 5 | NULL::BIGINT; 6 | ' 7 | LANGUAGE SQL; 8 | 9 | SELECT 10 | 1::NUMERIC, 11 | cast(2 AS VARCHAR(10)); 12 | 13 | SELECT 14 | 1::NUMERIC, 15 | '1.2'::FLOAT8, 16 | (12)::FLOAT4, 17 | cast(2 AS VARCHAR(10)); 18 | 19 | INSERT INTO ( 20 | field_one, 21 | field_two, 22 | field_three) 23 | VALUES ( 24 | 1, 25 | 2, 26 | 3); 27 | 28 | INSERT INTO ( 29 | field_one, 30 | field_two, 31 | field_3) 32 | VALUES ( 33 | 1, 34 | 2, 35 | 3), 36 | ( 37 | 4, 38 | 5, 39 | 6), 40 | ( 41 | 7, 42 | 8, 43 | 9); 44 | 45 | CREATE CAST (INT8 AS int8alias1) WITHOUT FUNCTION; 46 | 47 | CREATE TABLE test11a AS ( 48 | SELECT 49 | 1::PRIV_TESTDOMAIN1 AS a 50 | ); 51 | 52 | SELECT 53 | f1, 54 | f1::INTERVAL DAY TO MINUTE AS "minutes", 55 | (f1 + INTERVAL '1 month')::INTERVAL MONTH::INTERVAL YEAR AS "years" 56 | FROM 57 | interval_tbl; 58 | 59 | CREATE FUNCTION foo (bar1 IN TEXT, bar2 IN DATE) 60 | LANGUAGE plpgsql 61 | AS $procedure$ 62 | DECLARE 63 | foobar1 BIGINT; 64 | foobar2 DATE; 65 | foobar3 TEXT; 66 | foobar4 INT; 67 | BEGIN 68 | NULL; 69 | END 70 | $procedure$; 71 | 72 | SELECT 73 | format($$ A %I B %s C %L %% D $$, 'pg_user', 22, NULL); 74 | 75 | CREATE ROLE admin WITH connection LIMIT 3; 76 | 77 | -------------------------------------------------------------------------------- /t/test-files/expected/ex59.sql: -------------------------------------------------------------------------------- 1 | DELIMITER $$ 2 | -- We change the delimiter: it is now $$ instead of ; 3 | CREATE PROCEDURE TEST () 4 | BEGIN 5 | SELECT 6 | "Hello World"; 7 | 8 | END; 9 | $$ 10 | -- This is the delimiter that marks the end of the procedure definition. 11 | DELIMITER ; 12 | 13 | DROP SCHEMA IF EXISTS TEST_ISSUE_191; 14 | 15 | CREATE SCHEMA TEST_ISSUE_191; 16 | 17 | USE TEST_ISSUE_191; 18 | 19 | CREATE TABLE TEST ( 20 | A int 21 | ); 22 | 23 | INSERT INTO TEST 24 | VALUES (1); 25 | 26 | DELIMITER // 27 | CREATE PROCEDURE PROCEDURE_TEST () 28 | BEGIN 29 | SELECT 30 | * 31 | FROM 32 | TEST; 33 | 34 | END; 35 | // 36 | DELIMITER ; 37 | 38 | CALL PROCEDURE_TEST (); 39 | 40 | DELIMITER $$ 41 | CREATE PROCEDURE PROCEDURE_TEST () 42 | BEGIN 43 | SELECT 44 | * 45 | FROM 46 | TEST; 47 | 48 | END; 49 | $$ 50 | DELIMITER ; 51 | 52 | CALL PROCEDURE_TEST (); 53 | 54 | -------------------------------------------------------------------------------- /t/test-files/expected/ex6.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | attributes -> 'key' 3 | FROM 4 | json_test; 5 | 6 | CREATE TABLE foobar ( 7 | id integer NOT NULL, 8 | version integer NOT NULL, 9 | prev_id integer NOT NULL, 10 | prev_version integer NOT NULL, 11 | PRIMARY KEY (id, version), 12 | UNIQUE (prev_id, prev_version), 13 | FOREIGN KEY (prev_id, prev_version) REFERENCES foobar (id, version), 14 | FOREIGN KEY (id, version) REFERENCES barfoo (next_id, next_version) 15 | ); 16 | 17 | -------------------------------------------------------------------------------- /t/test-files/expected/ex61.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | myfunction(123, 'ertez', col1), 3 | pg_sleep(12) 4 | FROM 5 | mytbl1; 6 | 7 | CREATE OR REPLACE FUNCTION f () 8 | RETURNS bool 9 | AS ' 10 | / foo; 11 | 12 | ', 'bar' 13 | LANGUAGE C; 14 | 15 | SELECT 16 | lives_ok ('INSERT INTO "order".v_order (status, order_id, name) 17 | VALUES (''complete'', ''' || get_order_id () || ''', '' caleb ''', 'with all parameters'); 18 | 19 | INSERT INTO table1 (id, name) 20 | VALUES 21 | -- 5 min <= duration < 15 min 22 | (10, 'duration'); 23 | 24 | INSERT INTO table1 (id, name) 25 | VALUES 26 | -- name 27 | (10, 'duration') 28 | -------------------------------------------------------------------------------- /t/test-files/expected/ex62.sql: -------------------------------------------------------------------------------- 1 | -- test placeholder: perl pg_format samples/ex62.sql -w 60 -C 2 | -- -p 'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)' 3 | -- This code simply contains a long url in a comment. 4 | -- https://github.com/aubertc/pgFormatter/blob/master/t/test- 5 | -- files/ex62.sql 6 | -------------------------------------------------------------------------------- /t/test-files/expected/ex63.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | 1 + coalesce(( 3 | SELECT 4 | sum(amount) 5 | FROM transfers 6 | WHERE 7 | id IS NOT NULL), 0); 8 | 9 | SELECT 10 | id, 11 | jsonb_build_object('subs', ( 12 | SELECT 13 | json_agg(q.col) AS data FROM q 14 | WHERE 15 | q.id = t.pid)) AS data 16 | FROM 17 | t; 18 | 19 | SELECT 20 | ( 21 | SELECT 22 | max( 23 | SELECT 24 | i.unique2 25 | FROM tenk1 i 26 | WHERE 27 | i.unique1 = o.unique1)) 28 | FROM 29 | tenk1 o; 30 | 31 | DELETE FROM xx1 USING ( 32 | SELECT 33 | * 34 | FROM 35 | int4_tbl 36 | WHERE 37 | f1 = xx1.x1) ss; 38 | 39 | DELETE FROM xx1 USING LATERAL ( 40 | SELECT 41 | * 42 | FROM 43 | int4_tbl 44 | WHERE 45 | f1 = x1) ss; 46 | 47 | INSERT INTO shipped_view (ordnum, partnum, value) 48 | VALUES (0, 1, ( 49 | SELECT 50 | COST 51 | FROM parts 52 | WHERE 53 | partnum = '1')); 54 | 55 | SELECT 56 | lead(ten, ( 57 | SELECT 58 | two 59 | FROM tenk1 60 | WHERE 61 | s.unique2 = unique2)) OVER (PARTITION BY four ORDER BY ten) 62 | FROM 63 | tenk1 s 64 | WHERE 65 | unique2 < 10; 66 | 67 | SELECT 68 | empno, 69 | depname, 70 | salary, 71 | bonus, 72 | depadj, 73 | MIN(bonus) OVER (ORDER BY empno), 74 | MAX(depadj) OVER () 75 | FROM ( 76 | SELECT 77 | *, 78 | CASE WHEN enroll_date < '2008-01-01' THEN 79 | 2008 - extract(YEAR FROM enroll_date) 80 | END * 500 AS bonus, 81 | CASE WHEN AVG(salary) OVER (PARTITION BY depname) < salary THEN 82 | 200 83 | END AS depadj 84 | FROM 85 | empsalary) s; 86 | 87 | -------------------------------------------------------------------------------- /t/test-files/expected/ex64.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | * 3 | FROM 4 | accounts 5 | WHERE 6 | type = 'good' 7 | AND amount > 0; 8 | 9 | CREATE OR REPLACE FUNCTION get_from_partitioned_table (partitioned_table.a%type) 10 | RETURNS partitioned_table 11 | AS $$ 12 | DECLARE 13 | a_val partitioned_table.a%TYPE; 14 | result partitioned_table%ROWTYPE; 15 | BEGIN 16 | a_val := $1; 17 | 18 | SELECT 19 | * INTO result 20 | FROM 21 | partitioned_table 22 | WHERE 23 | a = a_val; 24 | 25 | RETURN result; 26 | END; 27 | $$ 28 | LANGUAGE plpgsql; 29 | 30 | -------------------------------------------------------------------------------- /t/test-files/expected/ex65.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | date_trunc('month', 日期) 3 | FROM 4 | ti; 5 | 6 | SELECT 7 | CASE WHEN test = 1 THEN 8 | (test_table.end_date - ('1 month'::interval) - '1 day'::interval) 9 | ELSE 10 | (test_table.end_date - (test_table.number_days * '1 day'::interval) - '1 day'::interval)::date 11 | END test_start_date 12 | FROM 13 | test_table; 14 | 15 | -------------------------------------------------------------------------------- /t/test-files/expected/ex66.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | col::"text" 3 | FROM 4 | tab; 5 | 6 | ALTER TABLE tab 7 | ALTER COLUMN col TYPE "schema"."dataType" 8 | USING col::text::"schema"."dataType"; 9 | 10 | -------------------------------------------------------------------------------- /t/test-files/expected/ex69.sql: -------------------------------------------------------------------------------- 1 | COPY time 2 | FROM 3 | 's3://mybucket/data/timerows.gz' iam_role 'arn:aws:iam::0123456789012:role/MyRedshiftRole' GZIP DELIMITER '|'; 4 | 5 | SELECT 6 | a.b AS "B", 7 | a.c AS "C" 8 | FROM 9 | a 10 | WHERE 11 | d IN ( 12 | SELECT 13 | e 14 | FROM ( 15 | SELECT 16 | f FF 17 | FROM 18 | h HH) i 19 | UNION ALL 20 | SELECT 21 | j 22 | FROM 23 | l LL 24 | WHERE 25 | h.o IS NULL) 26 | ORDER BY 27 | DECODE(p, 'a', SYSDATE, 'b', SYSDATE, 'c', SYSDATE, d), 28 | NVL (q, 1) ASC, 29 | CASE WHEN r = 0 30 | AND s != 'N' THEN 31 | a.y 32 | ELSE 33 | a.z 34 | END ASC; 35 | 36 | -------------------------------------------------------------------------------- /t/test-files/expected/ex7.sql: -------------------------------------------------------------------------------- 1 | -- From 9.4 JSON page - examples 2 | SELECT 3 | '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json -> 2; 4 | 5 | SELECT 6 | '{"a": {"b":"foo"}}'::json -> 'a'; 7 | 8 | SELECT 9 | '[1,2,3]'::json ->> 2; 10 | 11 | SELECT 12 | '{"a":1,"b":2}'::json ->> 'b'; 13 | 14 | SELECT 15 | '{"a": {"b":{"c": "foo"}}}'::json #> '{a,b}'; 16 | 17 | SELECT 18 | '{"a":[1,2,3],"b":[4,5,6]}'::json #>> '{a,2}'; 19 | 20 | -- From 9.4 JSON page - jsonb examples 21 | SELECT 22 | '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb; 23 | 24 | SELECT 25 | '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb; 26 | 27 | SELECT 28 | '{"a":1, "b":2}'::jsonb ? 'b'; 29 | 30 | SELECT 31 | '{"a":1, "b":2, "c":3}'::jsonb ?| ARRAY['b', 'c']; 32 | 33 | SELECT 34 | '{"a":1, "b":2, "c":3}'::jsonb ?| ARRAY['b', 'c']; 35 | 36 | SELECT 37 | '["a", "b"]'::jsonb ?& ARRAY['a', 'b']; 38 | 39 | SELECT 40 | '{"a": {"b":{"c": "foo"}}}'::json #> '{a,b}', 41 | '{"a":[1,2,3],"b":[4,5,6]}'::json #>> '{a,2}'; 42 | 43 | -------------------------------------------------------------------------------- /t/test-files/expected/ex70.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | app_public.hello(test); 3 | 4 | DROP FUNCTION IF EXISTS app_public.hello(a text); 5 | 6 | TRUNCATE 7 | table001, 8 | table002, 9 | table003, 10 | table004, 11 | table005, 12 | table006, 13 | table007, 14 | table008, 15 | table009, 16 | table010 17 | RESTART IDENTITY 18 | CASCADE; 19 | 20 | TRUNCATE t1; 21 | 22 | TRUNCATE t2 RESTART IDENTITY CASCADE; 23 | 24 | -------------------------------------------------------------------------------- /t/test-files/expected/ex71.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE foo 2 | ADD COLUMN bar int; 3 | 4 | CREATE OR REPLACE PROCEDURE do_something () 5 | LANGUAGE plpgsql 6 | AS $$ 7 | DECLARE 8 | salt text; 9 | BEGIN 10 | -- Create a temporary table 11 | CREATE TEMPORARY TABLE IF NOT EXISTS temp_id ( 12 | id varchar 13 | ) ON COMMIT DELETE ROWS; 14 | TRUNCATE TABLE temp_id; 15 | INSERT INTO temp_id 16 | SELECT 17 | id 18 | FROM 19 | jones; 20 | INSERT INTO temp_id 21 | SELECT 22 | id 23 | FROM 24 | freds; 25 | END; 26 | $$ 27 | -------------------------------------------------------------------------------- /t/test-files/expected/ex72.sql: -------------------------------------------------------------------------------- 1 | CREATE FUNCTION name () 2 | RETURNS text 3 | LANGUAGE plpgsql 4 | SET search_path FROM current 5 | AS $$ 6 | BEGIN 7 | RETURN 'text'; 8 | END; 9 | $$; 10 | 11 | -------------------------------------------------------------------------------- /t/test-files/expected/ex74.sql: -------------------------------------------------------------------------------- 1 | WITH example AS ( 2 | SELECT 3 | a, 4 | b 5 | FROM 6 | tablename) 7 | INSERT INTO example2 (a, b) 8 | SELECT 9 | COALESCE(a, 1) AS a, 10 | b 11 | FROM 12 | example 13 | -------------------------------------------------------------------------------- /t/test-files/expected/ex75.sql: -------------------------------------------------------------------------------- 1 | WITH example AS ( 2 | SELECT 3 | a, 4 | b 5 | FROM 6 | tablename 7 | ), 8 | example2 AS ( 9 | SELECT 10 | COALESCE(a, 1) AS a, 11 | b 12 | FROM 13 | example) 14 | INSERT INTO example3 (a, b) 15 | SELECT 16 | a, 17 | b 18 | FROM 19 | example 20 | -------------------------------------------------------------------------------- /t/test-files/expected/ex8.sql: -------------------------------------------------------------------------------- 1 | SELECT 2 | 'hello', 3 | 2 + 2, 4 | 'o\'grady', 5 | 0, 6 | '', 7 | count(*), 8 | 'that\'s the position you\'re in now no matter where you select it'; 9 | 10 | SELECT 11 | 1e-5, 12 | 1e+5, 13 | 10 + 20, 14 | colname + 12; 15 | 16 | CREATE TABLE public.sample ( 17 | a integer, 18 | b integer, 19 | c integer 20 | ); 21 | 22 | CREATE TABLE "public".sample ( 23 | a integer, 24 | b integer, 25 | c integer 26 | ); 27 | 28 | CREATE TABLE public."sample" ( 29 | a integer, 30 | b integer, 31 | c integer 32 | ); 33 | 34 | CREATE TABLE "public"."sample" ( 35 | a integer, 36 | b integer, 37 | c integer 38 | ); 39 | 40 | CREATE TABLE collate_test_fail ( 41 | a int, 42 | b text COLLATE "ja_JP.eucjp-x-icu" 43 | ); 44 | 45 | -------------------------------------------------------------------------------- /t/test-files/expected/ex9.sql: -------------------------------------------------------------------------------- 1 | -- test placeholder: perl pg_format samples/ex9.sql -p '<<(?:.*)?>>' 2 | SELECT 3 | * 4 | FROM 5 | projects 6 | WHERE 7 | projectnumber IN << internalprojects >> 8 | AND username = << loginname >>; 9 | 10 | CREATE TEMPORARY TABLE tt_monthly_data AS 11 | WITH a1 AS ( 12 | SELECT 13 | * 14 | FROM 15 | test1 16 | ) 17 | SELECT 18 | ROUND(AVG(t1)) avg_da, 19 | ROUND(AVG(t2)) 20 | FROM 21 | a1; 22 | 23 | INSERT INTO videos (sha, idx, title, language, codec, mime_codec, width, height, is_default, bitrate) 24 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) 25 | ON CONFLICT (sha, idx) 26 | DO UPDATE SET 27 | sha = excluded.sha, 28 | idx = excluded.idx, 29 | title = excluded.title, 30 | language = excluded.language, 31 | codec = excluded.codec, 32 | mime_codec = excluded.mime_codec, 33 | width = excluded.width, 34 | height = excluded.height, 35 | is_default = excluded.is_default, 36 | bitrate = excluded.bitrate; 37 | 38 | --------------------------------------------------------------------------------