1212 *by PostgreSQL
1313 *
1414 * IDENTIFICATION
15- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.496 2008/07/18 03:32:52 tgl Exp $
15+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.497 2008/07/20 18:43:30 tgl Exp $
1616 *
1717 *-------------------------------------------------------------------------
1818 */
@@ -71,6 +71,7 @@ boolattrNames;/* put attr names into insert strings */
7171bool schemaOnly ;
7272bool dataOnly ;
7373bool aclsSkip ;
74+ const char * lockWaitTimeout ;
7475
7576/* subquery used to convert user ID (eg, datdba) to user name */
7677static const char * username_subquery ;
@@ -264,6 +265,7 @@ main(int argc, char **argv)
264265 */
265266{"disable-dollar-quoting" ,no_argument ,& disable_dollar_quoting ,1 },
266267{"disable-triggers" ,no_argument ,& disable_triggers ,1 },
268+ {"lock-wait-timeout" ,required_argument ,NULL ,2 },
267269{"no-tablespaces" ,no_argument ,& outputNoTablespaces ,1 },
268270{"use-set-session-authorization" ,no_argument ,& use_setsessauth ,1 },
269271
@@ -279,6 +281,7 @@ main(int argc, char **argv)
279281strcpy (g_opaque_type ,"opaque" );
280282
281283dataOnly = schemaOnly = dumpInserts = attrNames = false;
284+ lockWaitTimeout = NULL ;
282285
283286progname = get_progname (argv [0 ]);
284287
@@ -437,6 +440,11 @@ main(int argc, char **argv)
437440/* This covers the long options equivalent to -X xxx. */
438441break ;
439442
443+ case 2 :
444+ /* lock-wait-timeout */
445+ lockWaitTimeout = optarg ;
446+ break ;
447+
440448default :
441449fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );
442450exit (1 );
@@ -754,12 +762,13 @@ help(const char *progname)
754762printf (_ (" %s [OPTION]... [DBNAME]\n" ),progname );
755763
756764printf (_ ("\nGeneral options:\n" ));
757- printf (_ (" -f, --file=FILENAME output file name\n" ));
758- printf (_ (" -F, --format=c|t|p output file format (custom, tar, plain text)\n" ));
759- printf (_ (" -v, --verbose verbose mode\n" ));
760- printf (_ (" -Z, --compress=0-9 compression level for compressed formats\n" ));
761- printf (_ (" --help show this help, then exit\n" ));
762- printf (_ (" --version output version information, then exit\n" ));
765+ printf (_ (" -f, --file=FILENAME output file name\n" ));
766+ printf (_ (" -F, --format=c|t|p output file format (custom, tar, plain text)\n" ));
767+ printf (_ (" -v, --verbose verbose mode\n" ));
768+ printf (_ (" -Z, --compress=0-9 compression level for compressed formats\n" ));
769+ printf (_ (" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n" ));
770+ printf (_ (" --help show this help, then exit\n" ));
771+ printf (_ (" --version output version information, then exit\n" ));
763772
764773printf (_ ("\nOptions controlling the output content:\n" ));
765774printf (_ (" -a, --data-only dump only the data, not the schema\n" ));
@@ -2957,8 +2966,6 @@ getTables(int *numTables)
29572966int ntups ;
29582967int i ;
29592968PQExpBuffer query = createPQExpBuffer ();
2960- PQExpBuffer delqry = createPQExpBuffer ();
2961- PQExpBuffer lockquery = createPQExpBuffer ();
29622969TableInfo * tblinfo ;
29632970int i_reltableoid ;
29642971int i_reloid ;
@@ -3192,6 +3199,21 @@ getTables(int *numTables)
31923199i_reltablespace = PQfnumber (res ,"reltablespace" );
31933200i_reloptions = PQfnumber (res ,"reloptions" );
31943201
3202+ if (lockWaitTimeout && g_fout -> remoteVersion >=70300 )
3203+ {
3204+ /*
3205+ * Arrange to fail instead of waiting forever for a table lock.
3206+ *
3207+ * NB: this coding assumes that the only queries issued within
3208+ * the following loop are LOCK TABLEs; else the timeout may be
3209+ * undesirably applied to other things too.
3210+ */
3211+ resetPQExpBuffer (query );
3212+ appendPQExpBuffer (query ,"SET statement_timeout = " );
3213+ appendStringLiteralConn (query ,lockWaitTimeout ,g_conn );
3214+ do_sql_command (g_conn ,query -> data );
3215+ }
3216+
31953217for (i = 0 ;i < ntups ;i ++ )
31963218{
31973219tblinfo [i ].dobj .objType = DO_TABLE ;
@@ -3246,12 +3268,12 @@ getTables(int *numTables)
32463268 */
32473269if (tblinfo [i ].dobj .dump && tblinfo [i ].relkind == RELKIND_RELATION )
32483270{
3249- resetPQExpBuffer (lockquery );
3250- appendPQExpBuffer (lockquery ,
3271+ resetPQExpBuffer (query );
3272+ appendPQExpBuffer (query ,
32513273"LOCK TABLE %s IN ACCESS SHARE MODE" ,
32523274fmtQualifiedId (tblinfo [i ].dobj .namespace -> dobj .name ,
32533275tblinfo [i ].dobj .name ));
3254- do_sql_command (g_conn ,lockquery -> data );
3276+ do_sql_command (g_conn ,query -> data );
32553277}
32563278
32573279/* Emit notice if join for owner failed */
@@ -3260,6 +3282,11 @@ getTables(int *numTables)
32603282tblinfo [i ].dobj .name );
32613283}
32623284
3285+ if (lockWaitTimeout && g_fout -> remoteVersion >=70300 )
3286+ {
3287+ do_sql_command (g_conn ,"SET statement_timeout = 0" );
3288+ }
3289+
32633290PQclear (res );
32643291
32653292/*
@@ -3292,8 +3319,6 @@ getTables(int *numTables)
32923319}
32933320
32943321destroyPQExpBuffer (query );
3295- destroyPQExpBuffer (delqry );
3296- destroyPQExpBuffer (lockquery );
32973322
32983323return tblinfo ;
32993324}