2000-01-19 00:30:24 +01:00
|
|
|
/*
|
|
|
|
* psql - the PostgreSQL interactive terminal
|
|
|
|
*
|
2017-01-03 19:48:53 +01:00
|
|
|
* Copyright (c) 2000-2017, PostgreSQL Global Development Group
|
2000-01-19 00:30:24 +01:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/bin/psql/large_obj.c
|
2000-01-19 00:30:24 +01:00
|
|
|
*/
|
2001-02-10 03:31:31 +01:00
|
|
|
#include "postgres_fe.h"
|
1999-11-04 22:56:02 +01:00
|
|
|
#include "large_obj.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "settings.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
2015-03-26 19:03:19 +01:00
|
|
|
static void print_lo_result(const char *fmt,...) pg_attribute_printf(1, 2);
|
2007-01-20 17:57:31 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
print_lo_result(const char *fmt,...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (!pset.quiet)
|
|
|
|
{
|
|
|
|
if (pset.popt.topt.format == PRINT_HTML)
|
|
|
|
fputs("<p>", pset.queryFout);
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(pset.queryFout, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (pset.popt.topt.format == PRINT_HTML)
|
|
|
|
fputs("</p>\n", pset.queryFout);
|
|
|
|
else
|
|
|
|
fputs("\n", pset.queryFout);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pset.logfile)
|
|
|
|
{
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(pset.logfile, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
fputs("\n", pset.logfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Prepare to do a large-object operation. We *must* be inside a transaction
|
2003-06-28 02:12:40 +02:00
|
|
|
* block for all these operations, so start one if needed.
|
|
|
|
*
|
2017-08-16 06:22:32 +02:00
|
|
|
* Returns true if okay, false if failed. *own_transaction is set to indicate
|
2003-06-28 02:12:40 +02:00
|
|
|
* if we started our own transaction or not.
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
2003-06-28 02:12:40 +02:00
|
|
|
static bool
|
|
|
|
start_lo_xact(const char *operation, bool *own_transaction)
|
|
|
|
{
|
|
|
|
PGTransactionStatusType tstatus;
|
|
|
|
PGresult *res;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
*own_transaction = false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!pset.db)
|
|
|
|
{
|
|
|
|
psql_error("%s: not connected to a database\n", operation);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tstatus = PQtransactionStatus(pset.db);
|
|
|
|
|
|
|
|
switch (tstatus)
|
|
|
|
{
|
|
|
|
case PQTRANS_IDLE:
|
|
|
|
/* need to start our own xact */
|
2014-10-23 15:33:56 +02:00
|
|
|
if (!(res = PSQLexec("BEGIN")))
|
2003-06-28 02:12:40 +02:00
|
|
|
return false;
|
|
|
|
PQclear(res);
|
|
|
|
*own_transaction = true;
|
|
|
|
break;
|
|
|
|
case PQTRANS_INTRANS:
|
|
|
|
/* use the existing xact */
|
|
|
|
break;
|
|
|
|
case PQTRANS_INERROR:
|
|
|
|
psql_error("%s: current transaction is aborted\n", operation);
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
psql_error("%s: unknown transaction status\n", operation);
|
|
|
|
return false;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
/*
|
|
|
|
* Clean up after a successful LO operation
|
|
|
|
*/
|
1999-11-04 22:56:02 +01:00
|
|
|
static bool
|
2003-06-28 02:12:40 +02:00
|
|
|
finish_lo_xact(const char *operation, bool own_transaction)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
if (own_transaction && pset.autocommit)
|
2003-03-20 07:43:35 +01:00
|
|
|
{
|
2003-06-28 02:12:40 +02:00
|
|
|
/* close out our own xact */
|
2014-10-23 15:33:56 +02:00
|
|
|
if (!(res = PSQLexec("COMMIT")))
|
2003-06-28 02:12:40 +02:00
|
|
|
{
|
2014-10-23 15:33:56 +02:00
|
|
|
res = PSQLexec("ROLLBACK");
|
2003-06-28 02:12:40 +02:00
|
|
|
PQclear(res);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PQclear(res);
|
2003-03-20 07:43:35 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
/*
|
|
|
|
* Clean up after a failed LO operation
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
fail_lo_xact(const char *operation, bool own_transaction)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-08-29 17:19:51 +02:00
|
|
|
if (own_transaction && pset.autocommit)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2003-06-28 02:12:40 +02:00
|
|
|
/* close out our own xact */
|
2014-10-23 15:33:56 +02:00
|
|
|
res = PSQLexec("ROLLBACK");
|
2003-06-28 02:12:40 +02:00
|
|
|
PQclear(res);
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
return false; /* always */
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_export()
|
|
|
|
*
|
|
|
|
* Write a large object to a file
|
|
|
|
*/
|
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_export(const char *loid_arg, const char *filename_arg)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
int status;
|
2003-03-20 07:43:35 +01:00
|
|
|
bool own_transaction;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!start_lo_xact("\\lo_export", &own_transaction))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
SetCancelConn();
|
2001-10-05 00:39:34 +02:00
|
|
|
status = lo_export(pset.db, atooid(loid_arg), filename_arg);
|
2006-06-14 18:49:03 +02:00
|
|
|
ResetCancelConn();
|
|
|
|
|
|
|
|
/* of course this status is documented nowhere :( */
|
1999-11-05 00:14:30 +01:00
|
|
|
if (status != 1)
|
2006-06-14 18:49:03 +02:00
|
|
|
{
|
2012-08-26 04:58:42 +02:00
|
|
|
psql_error("%s", PQerrorMessage(pset.db));
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_export", own_transaction);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!finish_lo_xact("\\lo_export", own_transaction))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
print_lo_result("lo_export");
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_import()
|
|
|
|
*
|
|
|
|
* Copy large object from file to database
|
|
|
|
*/
|
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_import(const char *filename_arg, const char *comment_arg)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
|
|
|
Oid loid;
|
2001-08-10 20:57:42 +02:00
|
|
|
char oidbuf[32];
|
2003-03-20 07:43:35 +01:00
|
|
|
bool own_transaction;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!start_lo_xact("\\lo_import", &own_transaction))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
SetCancelConn();
|
2000-02-08 00:10:11 +01:00
|
|
|
loid = lo_import(pset.db, filename_arg);
|
2006-06-14 18:49:03 +02:00
|
|
|
ResetCancelConn();
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (loid == InvalidOid)
|
|
|
|
{
|
2012-08-26 04:58:42 +02:00
|
|
|
psql_error("%s", PQerrorMessage(pset.db));
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_import", own_transaction);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
/* insert description if given */
|
2003-11-21 23:32:49 +01:00
|
|
|
if (comment_arg)
|
1999-11-05 00:14:30 +01:00
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
char *cmdbuf;
|
|
|
|
char *bufptr;
|
2002-10-03 19:09:42 +02:00
|
|
|
size_t slen = strlen(comment_arg);
|
2001-08-10 20:57:42 +02:00
|
|
|
|
2013-10-23 00:42:13 +02:00
|
|
|
cmdbuf = malloc(slen * 2 + 256);
|
|
|
|
if (!cmdbuf)
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_import", own_transaction);
|
2013-10-23 00:42:13 +02:00
|
|
|
sprintf(cmdbuf, "COMMENT ON LARGE OBJECT %u IS '", loid);
|
|
|
|
bufptr = cmdbuf + strlen(cmdbuf);
|
2006-05-28 23:13:54 +02:00
|
|
|
bufptr += PQescapeStringConn(pset.db, bufptr, comment_arg, slen, NULL);
|
2003-11-21 23:32:49 +01:00
|
|
|
strcpy(bufptr, "'");
|
2001-08-10 20:57:42 +02:00
|
|
|
|
2014-10-23 15:33:56 +02:00
|
|
|
if (!(res = PSQLexec(cmdbuf)))
|
2001-08-10 20:57:42 +02:00
|
|
|
{
|
|
|
|
free(cmdbuf);
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_import", own_transaction);
|
2001-08-10 20:57:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
free(cmdbuf);
|
1999-11-05 00:14:30 +01:00
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!finish_lo_xact("\\lo_import", own_transaction))
|
|
|
|
return false;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
print_lo_result("lo_import %u", loid);
|
|
|
|
|
2001-08-10 20:57:42 +02:00
|
|
|
sprintf(oidbuf, "%u", loid);
|
|
|
|
SetVariable(pset.vars, "LASTOID", oidbuf);
|
1999-11-05 00:14:30 +01:00
|
|
|
|
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_unlink()
|
|
|
|
*
|
|
|
|
* removes a large object out of the database
|
|
|
|
*/
|
1999-11-05 00:14:30 +01:00
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_unlink(const char *loid_arg)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
int status;
|
2001-10-05 00:39:34 +02:00
|
|
|
Oid loid = atooid(loid_arg);
|
2003-03-20 07:43:35 +01:00
|
|
|
bool own_transaction;
|
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!start_lo_xact("\\lo_unlink", &own_transaction))
|
1999-11-05 00:14:30 +01:00
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2006-06-14 18:49:03 +02:00
|
|
|
SetCancelConn();
|
2000-01-14 23:18:03 +01:00
|
|
|
status = lo_unlink(pset.db, loid);
|
2006-06-14 18:49:03 +02:00
|
|
|
ResetCancelConn();
|
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
if (status == -1)
|
|
|
|
{
|
2012-08-26 04:58:42 +02:00
|
|
|
psql_error("%s", PQerrorMessage(pset.db));
|
2003-06-28 02:12:40 +02:00
|
|
|
return fail_lo_xact("\\lo_unlink", own_transaction);
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2003-06-28 02:12:40 +02:00
|
|
|
if (!finish_lo_xact("\\lo_unlink", own_transaction))
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2007-01-20 17:57:31 +01:00
|
|
|
print_lo_result("lo_unlink %u", loid);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do_lo_list()
|
|
|
|
*
|
2000-01-12 20:36:36 +01:00
|
|
|
* Show all large objects in database with comments
|
1999-11-04 22:56:02 +01:00
|
|
|
*/
|
1999-11-05 00:14:30 +01:00
|
|
|
bool
|
2000-01-14 23:18:03 +01:00
|
|
|
do_lo_list(void)
|
1999-11-04 22:56:02 +01:00
|
|
|
{
|
1999-11-05 00:14:30 +01:00
|
|
|
PGresult *res;
|
2000-01-12 20:36:36 +01:00
|
|
|
char buf[1024];
|
2000-01-14 23:18:03 +01:00
|
|
|
printQueryOpt myopt = pset.popt;
|
1999-11-05 00:14:30 +01:00
|
|
|
|
2010-02-17 05:19:41 +01:00
|
|
|
if (pset.sversion >= 90000)
|
2009-12-11 04:34:57 +01:00
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"SELECT oid as \"%s\",\n"
|
|
|
|
" pg_catalog.pg_get_userbyid(lomowner) as \"%s\",\n"
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
" pg_catalog.obj_description(oid, 'pg_largeobject') as \"%s\"\n"
|
2009-12-11 04:34:57 +01:00
|
|
|
" FROM pg_catalog.pg_largeobject_metadata "
|
|
|
|
" ORDER BY oid",
|
|
|
|
gettext_noop("ID"),
|
|
|
|
gettext_noop("Owner"),
|
|
|
|
gettext_noop("Description"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"SELECT loid as \"%s\",\n"
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
" pg_catalog.obj_description(loid, 'pg_largeobject') as \"%s\"\n"
|
|
|
|
"FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) x\n"
|
2009-12-11 04:34:57 +01:00
|
|
|
"ORDER BY 1",
|
|
|
|
gettext_noop("ID"),
|
|
|
|
gettext_noop("Description"));
|
|
|
|
}
|
1999-11-04 22:56:02 +01:00
|
|
|
|
2014-10-23 15:33:56 +02:00
|
|
|
res = PSQLexec(buf);
|
1999-11-05 00:14:30 +01:00
|
|
|
if (!res)
|
|
|
|
return false;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
myopt.topt.tuples_only = false;
|
|
|
|
myopt.nullPrint = NULL;
|
2005-02-22 05:43:23 +01:00
|
|
|
myopt.title = _("Large objects");
|
2008-07-15 00:00:04 +02:00
|
|
|
myopt.translate_header = true;
|
1999-11-04 22:56:02 +01:00
|
|
|
|
Fix behavior of printTable() and friends with externally-invoked pager.
The formatting modes that depend on knowledge of the terminal window width
did not work right when printing a query result that's been fetched in
sections (as a result of FETCH_SIZE). ExecQueryUsingCursor() would force
use of the pager as soon as there's more than one result section, and then
print.c would see an output file pointer that's not stdout and incorrectly
conclude that the terminal window width isn't relevant.
This has been broken all along for non-expanded "wrapped" output format,
and as of 9.5 the issue affects expanded mode as well. The problem also
caused "\pset expanded auto" mode to invariably *not* switch to expanded
output in a segmented result, which seems to me to be exactly backwards.
To fix, we need to pass down an "is_pager" flag to inform the print.c
subroutines that some calling level has already replaced stdout with a
pager pipe, so they should (a) not do that again and (b) nonetheless honor
the window size. (Notably, this makes the first is_pager test in
print_aligned_text() not be dead code anymore.)
This patch is a bit invasive because there are so many existing calls of
printQuery()/printTable(), but fortunately all but a couple can just pass
"false" for the added parameter.
Back-patch to 9.5 but no further. Given the lack of field complaints,
it's not clear that we should change the behavior in stable branches.
Also, the API change for printQuery()/printTable() might possibly break
third-party code, again something we don't like to do in stable branches.
However, it's not quite too late to do this in 9.5, and with the larger
scope of the problem there, it seems worth doing.
2015-12-03 00:20:33 +01:00
|
|
|
printQuery(res, &myopt, pset.queryFout, false, pset.logfile);
|
1999-11-04 22:56:02 +01:00
|
|
|
|
1999-11-05 00:14:30 +01:00
|
|
|
PQclear(res);
|
|
|
|
return true;
|
1999-11-04 22:56:02 +01:00
|
|
|
}
|