Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Improve contrib/amcheck's tests for CREATE INDEX CONCURRENTLY.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 28 Oct 2021 15:45:14 +0000 (11:45 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 28 Oct 2021 15:45:14 +0000 (11:45 -0400)
Commits fdd965d07 and 3cd9c3b92 tested CREATE INDEX CONCURRENTLY by
launching two separate pgbench runs concurrently.  This was needed so
that only a single client thread would run CREATE INDEX CONCURRENTLY,
avoiding deadlock between two CICs.  However, there's a better way,
which is to use an advisory lock to prevent concurrent CICs.  That's
better in part because the test code is shorter and more readable, but
mostly because it automatically scales things to launch an appropriate
number of CICs relative to the number of INSERT transactions.
As committed, typically half to three-quarters of the CIC transactions
were pointless because the INSERT transactions had already stopped.

In passing, remove background_pgbench, which was added to support
these tests and isn't needed anymore.  We can always put it back
if we find a use for it later.

Back-patch to v12; older pgbench versions lack the
conditional-execution features needed for this method.

Tom Lane and Andrey Borodin

Discussion: https://postgr.es/m/139687.1635277318@sss.pgh.pa.us

contrib/amcheck/t/002_cic.pl
contrib/amcheck/t/003_cic_2pc.pl
src/test/perl/PostgresNode.pm

index 26b56054b136406f7fe254ffa5609ea7eed03319..832ae8f1cbdb2f52b91ee8bfa8a9213d13f661c6 100644 (file)
@@ -9,7 +9,7 @@ use Config;
 use PostgresNode;
 use TestLib;
 
-use Test::More tests => 4;
+use Test::More tests => 3;
 
 my ($node, $result);
 
@@ -25,32 +25,18 @@ $node->safe_psql('postgres', q(CREATE TABLE tbl(i int)));
 $node->safe_psql('postgres', q(CREATE INDEX idx ON tbl(i)));
 
 #
-# Stress CIC with pgbench
+# Stress CIC with pgbench.
+#
+# pgbench might try to launch more than one instance of the CIC
+# transaction concurrently.  That would deadlock, so use an advisory
+# lock to ensure only one CIC runs at a time.
 #
-
-# Run background pgbench with CIC. We cannot mix-in this script into single
-# pgbench: CIC will deadlock with itself occasionally.
-my $pgbench_out   = '';
-my $pgbench_timer = IPC::Run::timeout(180);
-my $pgbench_h     = $node->background_pgbench(
-   '--no-vacuum --client=1 --transactions=200',
-   {
-       '002_pgbench_concurrent_cic' => q(
-           DROP INDEX CONCURRENTLY idx;
-           CREATE INDEX CONCURRENTLY idx ON tbl(i);
-           SELECT bt_index_check('idx',true);
-          )
-   },
-   \$pgbench_out,
-   $pgbench_timer);
-
-# Run pgbench.
 $node->pgbench(
-   '--no-vacuum --client=5 --transactions=200',
+   '--no-vacuum --client=5 --transactions=100',
    0,
    [qr{actually processed}],
    [qr{^$}],
-   'concurrent INSERTs',
+   'concurrent INSERTs and CIC',
    {
        '002_pgbench_concurrent_transaction' => q(
            BEGIN;
@@ -62,17 +48,17 @@ $node->pgbench(
            SAVEPOINT s1;
            INSERT INTO tbl VALUES(0);
            COMMIT;
+         ),
+       '002_pgbench_concurrent_cic' => q(
+           SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
+           \if :gotlock
+               DROP INDEX CONCURRENTLY idx;
+               CREATE INDEX CONCURRENTLY idx ON tbl(i);
+               SELECT bt_index_check('idx',true);
+               SELECT pg_advisory_unlock(42);
+           \endif
          )
    });
 
-$pgbench_h->pump_nb;
-$pgbench_h->finish();
-$result =
-    ($Config{osname} eq "MSWin32")
-  ? ($pgbench_h->full_results)[0]
-  : $pgbench_h->result(0);
-is($result, 0, "pgbench with CIC works");
-
-# done
 $node->stop;
 done_testing();
index 52760196299f29a823d339482d29bf0ba03ddddc..c5391869655f5f436d9a5d1d373b105915ac276c 100644 (file)
@@ -9,7 +9,7 @@ use Config;
 use PostgresNode;
 use TestLib;
 
-use Test::More tests => 6;
+use Test::More tests => 5;
 
 my ($node, $result);
 
@@ -132,57 +132,52 @@ is($result, '0', 'bt_index_check after 2PC and restart');
 #
 # Stress CIC+2PC with pgbench
 #
+# pgbench might try to launch more than one instance of the CIC
+# transaction concurrently.  That would deadlock, so use an advisory
+# lock to ensure only one CIC runs at a time.
 
 # Fix broken index first
 $node->safe_psql('postgres', q(REINDEX TABLE tbl;));
 
-# Run background pgbench with CIC. We cannot mix-in this script into single
-# pgbench: CIC will deadlock with itself occasionally.
-my $pgbench_out   = '';
-my $pgbench_timer = IPC::Run::timeout(180);
-my $pgbench_h     = $node->background_pgbench(
-   '--no-vacuum --client=1 --transactions=100',
-   {
-       '002_pgbench_concurrent_cic' => q(
-           DROP INDEX CONCURRENTLY idx;
-           CREATE INDEX CONCURRENTLY idx ON tbl(i);
-           SELECT bt_index_check('idx',true);
-          )
-   },
-   \$pgbench_out,
-   $pgbench_timer);
-
 # Run pgbench.
 $node->pgbench(
    '--no-vacuum --client=5 --transactions=100',
    0,
    [qr{actually processed}],
    [qr{^$}],
-   'concurrent INSERTs w/ 2PC',
+   'concurrent INSERTs w/ 2PC and CIC',
    {
-       '002_pgbench_concurrent_2pc' => q(
+       '003_pgbench_concurrent_2pc' => q(
            BEGIN;
            INSERT INTO tbl VALUES(0);
            PREPARE TRANSACTION 'c:client_id';
            COMMIT PREPARED 'c:client_id';
          ),
-       '002_pgbench_concurrent_2pc_savepoint' => q(
+       '003_pgbench_concurrent_2pc_savepoint' => q(
            BEGIN;
            SAVEPOINT s1;
            INSERT INTO tbl VALUES(0);
            PREPARE TRANSACTION 'c:client_id';
            COMMIT PREPARED 'c:client_id';
+         ),
+       '003_pgbench_concurrent_cic' => q(
+           SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
+           \if :gotlock
+               DROP INDEX CONCURRENTLY idx;
+               CREATE INDEX CONCURRENTLY idx ON tbl(i);
+               SELECT bt_index_check('idx',true);
+               SELECT pg_advisory_unlock(42);
+           \endif
+         ),
+       '004_pgbench_concurrent_ric' => q(
+           SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset
+           \if :gotlock
+               REINDEX INDEX CONCURRENTLY idx;
+               SELECT bt_index_check('idx',true);
+               SELECT pg_advisory_unlock(42);
+           \endif
          )
    });
 
-$pgbench_h->pump_nb;
-$pgbench_h->finish();
-$result =
-    ($Config{osname} eq "MSWin32")
-  ? ($pgbench_h->full_results)[0]
-  : $pgbench_h->result(0);
-is($result, 0, "pgbench with CIC works");
-
-# done
 $node->stop;
 done_testing();
index 692818352ef0417acb0e01b924244fb6a0a32a29..e77fe012be67008277f90ab67d6f3f9602c9e0cb 100644 (file)
@@ -1816,55 +1816,6 @@ sub pgbench
 
 =pod
 
-=item $node->background_pgbench($opts, $files, \$stdout, $timer) => harness
-
-Invoke B<pgbench> and return an IPC::Run harness object.  The process's stdin
-is empty, and its stdout and stderr go to the $stdout scalar reference.  This
-allows the caller to act on other parts of the system while B<pgbench> is
-running.  Errors from B<pgbench> are the caller's problem.
-
-The specified timer object is attached to the harness, as well.  It's caller's
-responsibility to select the timeout length, and to restart the timer after
-each command if the timeout is per-command.
-
-Be sure to "finish" the harness when done with it.
-
-=over
-
-=item $opts
-
-Options as a string to be split on spaces.
-
-=item $files
-
-Reference to filename/contents dictionary.
-
-=back
-
-=cut
-
-sub background_pgbench
-{
-   my ($self, $opts, $files, $stdout, $timer) = @_;
-
-   my @cmd =
-     ('pgbench', split(/\s+/, $opts), $self->_pgbench_make_files($files));
-
-   local $ENV{PGHOST} = $self->host;
-   local $ENV{PGPORT} = $self->port;
-
-   my $stdin = "";
-   # IPC::Run would otherwise append to existing contents:
-   $$stdout = "" if ref($stdout);
-
-   my $harness = IPC::Run::start \@cmd, '<', \$stdin, '>', $stdout, '2>&1',
-     $timer;
-
-   return $harness;
-}
-
-=pod
-
 =item $node->poll_query_until($dbname, $query [, $expected ])
 
 Run B<$query> repeatedly, until it returns the B<$expected> result