@@ -112,9 +112,13 @@ Rules are applied in order with the first match taking precedence.
112112Randomize the rows initially selected from each table. May significantly
113113increase the running time of the script.
114114
115- =item B<--schema= >I<name >
115+ =item B<--sample- schema= >I<schema >
116116
117- The schema name to use for the sample database (defaults to _pg_sample).
117+ Schema name to use for the sample database (defaults to _pg_sample).
118+
119+ =item B<--schema= >I<schema >
120+
121+ Limit sampling to the specified schema. By default, all schemas are sampled.
118122
119123=item B<--trace >
120124
@@ -364,7 +368,7 @@ sub sample_table ($) {
364368 my $table = shift ;
365369
366370 my $sample_table = join ' _' , $table -> schema || ' public' , $table -> table;
367- return Table-> new($opt {schema }, $sample_table );
371+ return Table-> new($opt {sample_schema }, $sample_table );
368372}
369373
370374sub notice (@) {
@@ -373,12 +377,13 @@ sub notice (@) {
373377}
374378
375379%opt = (
376- db_host => ' ' ,
377- db_port => ' ' ,
378- keep => 0,
379- random => 0,
380- schema => ' _pg_sample' ,
381- verbose => 0,
380+ db_host => ' ' ,
381+ db_port => ' ' ,
382+ keep => 0,
383+ random => 0,
384+ schema => undef ,
385+ sample_schema => ' _pg_sample' ,
386+ verbose => 0,
382387);
383388
384389GetOptions(\%opt ,
@@ -396,6 +401,7 @@ GetOptions(\%opt,
396401 " limit=s@" ,
397402 " no-privileges|no-acl|x" ,
398403 " random" ,
404+ " sample_schema=s" ,
399405 " schema=s" ,
400406 " trace" ,
401407 " verbose|v" ,
@@ -428,24 +434,24 @@ my $dbh = connect_db(%opt) or croak "unable to connect to database";
428434
429435my $pg_version = pg_version;
430436
431- if ($opt {schema } eq ' public' ) {
437+ if ($opt {sample_schema } eq ' public' ) {
432438 die " Error: refusing to use 'public' schema for sampling.\n " ;
433439}
434440
435441my ($schema_oid ) = $dbh -> selectrow_array(qq{
436442 SELECT oid
437443 FROM pg_catalog.pg_namespace
438444 WHERE nspname = ?
439- } , undef , $opt {schema });
445+ } , undef , $opt {sample_schema });
440446if ($schema_oid && !$opt {force }) {
441- die " Error: schema '$opt {schema }' already exists. " .
447+ die " Error: schema '$opt {sample_schema }' already exists. " .
442448 " Use --force option to overwrite.\n " ;
443449}
444450
445451$dbh -> do(qq{ SET client_min_messages = warning } ); # suppress notice messages
446452if ($opt {force }) {
447- notice " Dropping sample schema $opt {schema }\n " ;
448- $dbh -> do(qq{ DROP SCHEMA IF EXISTS $opt {schema } CASCADE } );
453+ notice " Dropping sample schema $opt {sample_schema }\n " ;
454+ $dbh -> do(qq{ DROP SCHEMA IF EXISTS $opt {sample_schema } CASCADE } );
449455}
450456
451457if ($opt {file }) {
@@ -470,14 +476,15 @@ unless ($opt{'data-only'}) {
470476
471477 my @cmd = (' pg_dump' , ' --schema-only' );
472478 push @cmd , ' --no-privileges' if $opt {' no-privileges' };
479+ push @cmd , " --schema=$opt {schema}" if $opt {schema };
473480 system (@cmd ) == 0 or croak " command '@cmd ' failed: $? " ;
474481}
475482
476483# If running PostgreSQL 9.1 or later, use UNLOGGED tables
477484my $unlogged = $pg_version >= version-> declare(' 9.1' ) ? ' UNLOGGED' : ' ' ;
478485
479- notice " Creating sample schema $opt {schema }\n " ;
480- $dbh -> do(qq{ CREATE SCHEMA $opt {schema } } );
486+ notice " Creating sample schema $opt {sample_schema }\n " ;
487+ $dbh -> do(qq{ CREATE SCHEMA $opt {sample_schema } } );
481488my $created_schema = 1; # keep track that we actually did it; see END block
482489
483490# parse limit rules
@@ -502,6 +509,7 @@ my $sth = $dbh->table_info(undef, undef, undef, 'TABLE');
502509while (my $row = lower_keys($sth -> fetchrow_hashref)) {
503510 next unless uc $row -> {table_type } eq ' TABLE' ; # skip SYSTEM TABLE values
504511 next if $row -> {table_schem } eq ' information_schema' ; # special pg schema
512+ next if $opt {schema } && $row -> {table_schem } ne $opt {schema };
505513
506514 my $sname = $row -> {pg_schema } || unquote_identifier($row -> {TABLE_SCHEM })
507515 or die " no pg_schema or TABLE_SCHEM value?!" ;
@@ -585,7 +593,7 @@ foreach my $fk (@fks) {
585593 my ($fk_table , $table , @pairs ) = @$fk ;
586594
587595 my $sample_fk_table = $sample_tables { $fk_table };
588- my $idx_name = $dbh -> quote_identifier($opt {schema } . ' _idx' . ++$idx );
596+ my $idx_name = $dbh -> quote_identifier(" $opt {sample_schema} _idx" . ++$idx );
589597 my $fk_cols = join ' , ' , map { $_ -> [0] } @pairs ;
590598 $dbh -> do(qq{ CREATE INDEX $idx_name ON $sample_fk_table ($fk_cols ) } );
591599}
@@ -654,6 +662,8 @@ $sth = $dbh->prepare(qq{
654662$sth -> execute;
655663my %seq ;
656664while (my $row = $sth -> fetchrow_hashref) {
665+ next if $opt {schema } && $row -> {sequence_schema } ne $opt {schema };
666+
657667 my $name = Table-> new($row -> {sequence_schema }, $row -> {sequence_name });
658668 $seq { $name } = 0;
659669}
@@ -666,7 +676,7 @@ foreach my $name (keys %seq) {
666676print <<EOF ;
667677
668678SET client_encoding = '$opt {encoding}';
669- SET standard_conforming_strings = off ;
679+ SET standard_conforming_strings = on ;
670680SET check_function_bodies = false;
671681SET client_min_messages = warning;
672682SET escape_string_warning = off;
@@ -677,7 +687,7 @@ notice "Exporting sequences\n";
677687print " \n " ;
678688foreach my $name (sort keys %seq ) {
679689 my $constant = quote_constant($name );
680- print " SELECT pg_catalog.setval($constant , $seq {$name });\n " ;
690+ print " SELECT pg_catalog.setval($constant , $seq { $name });\n " ;
681691}
682692print " \n " ;
683693
@@ -711,8 +721,8 @@ print "\n";
711721END {
712722 # remove sample tables unless requested not to
713723 if ($created_schema && !$opt {keep }) {
714- notice " Dropping sample schema $opt {schema }\n " ;
715- $dbh -> do(" DROP SCHEMA $opt {schema } CASCADE" );
724+ notice " Dropping sample schema $opt {sample_schema }\n " ;
725+ $dbh -> do(" DROP SCHEMA $opt {sample_schema } CASCADE" );
716726 }
717727
718728 notice " Done.\n " ;
0 commit comments