static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
const Oid *excludeOps,
bool attrsOnly, bool showTblSpc,
- int prettyFlags);
+ int prettyFlags, bool missing_ok);
static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
- int prettyFlags);
+ int prettyFlags, bool missing_ok);
static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname,
int prettyFlags);
static int print_function_arguments(StringInfo buf, HeapTuple proctup,
{
Oid ruleoid = PG_GETARG_OID(0);
int prettyFlags;
+ char *res;
prettyFlags = PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_ruledef_worker(ruleoid, prettyFlags)));
+
+ res = pg_get_ruledef_worker(ruleoid, prettyFlags);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Oid ruleoid = PG_GETARG_OID(0);
bool pretty = PG_GETARG_BOOL(1);
int prettyFlags;
+ char *res;
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_ruledef_worker(ruleoid, prettyFlags)));
+
+ res = pg_get_ruledef_worker(ruleoid, prettyFlags);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
if (spirc != SPI_OK_SELECT)
elog(ERROR, "failed to get pg_rewrite tuple for rule %u", ruleoid);
if (SPI_processed != 1)
- appendStringInfoChar(&buf, '-');
+ {
+ /*
+ * There is no tuple data available here, just keep the output buffer
+ * empty.
+ */
+ }
else
{
/*
if (SPI_finish() != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed");
+ if (buf.len == 0)
+ return NULL;
+
return buf.data;
}
/* By OID */
Oid viewoid = PG_GETARG_OID(0);
int prettyFlags;
+ char *res;
prettyFlags = PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT)));
+
+ res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Oid viewoid = PG_GETARG_OID(0);
bool pretty = PG_GETARG_BOOL(1);
int prettyFlags;
+ char *res;
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT)));
+
+ res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Datum
Oid viewoid = PG_GETARG_OID(0);
int wrap = PG_GETARG_INT32(1);
int prettyFlags;
+ char *res;
/* calling this implies we want pretty printing */
prettyFlags = PRETTYFLAG_PAREN | PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags, wrap)));
+
+ res = pg_get_viewdef_worker(viewoid, prettyFlags, wrap);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Datum
int prettyFlags;
RangeVar *viewrel;
Oid viewoid;
+ char *res;
prettyFlags = PRETTYFLAG_INDENT;
viewrel = makeRangeVarFromNameList(textToQualifiedNameList(viewname));
viewoid = RangeVarGetRelid(viewrel, NoLock, false);
- PG_RETURN_TEXT_P(string_to_text(pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT)));
+ res = pg_get_viewdef_worker(viewoid, prettyFlags, WRAP_COLUMN_DEFAULT);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
if (spirc != SPI_OK_SELECT)
elog(ERROR, "failed to get pg_rewrite tuple for view %u", viewoid);
if (SPI_processed != 1)
- appendStringInfoString(&buf, "Not a view");
+ {
+ /*
+ * There is no tuple data available here, just keep the output buffer
+ * empty.
+ */
+ }
else
{
/*
if (SPI_finish() != SPI_OK_FINISH)
elog(ERROR, "SPI_finish failed");
+ if (buf.len == 0)
+ return NULL;
+
return buf.data;
}
pg_get_triggerdef(PG_FUNCTION_ARGS)
{
Oid trigid = PG_GETARG_OID(0);
+ char *res;
+
+ res = pg_get_triggerdef_worker(trigid, false);
- PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, false)));
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Datum
{
Oid trigid = PG_GETARG_OID(0);
bool pretty = PG_GETARG_BOOL(1);
+ char *res;
+
+ res = pg_get_triggerdef_worker(trigid, pretty);
- PG_RETURN_TEXT_P(string_to_text(pg_get_triggerdef_worker(trigid, pretty)));
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
static char *
ht_trig = systable_getnext(tgscan);
if (!HeapTupleIsValid(ht_trig))
- elog(ERROR, "could not find tuple for trigger %u", trigid);
+ {
+ systable_endscan(tgscan);
+ heap_close(tgrel, AccessShareLock);
+ return NULL;
+ }
trigrec = (Form_pg_trigger) GETSTRUCT(ht_trig);
{
Oid indexrelid = PG_GETARG_OID(0);
int prettyFlags;
+ char *res;
prettyFlags = PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_indexdef_worker(indexrelid, 0,
- NULL,
- false, false,
- prettyFlags)));
+
+ res = pg_get_indexdef_worker(indexrelid, 0, NULL, false, false,
+ prettyFlags, true);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Datum
int32 colno = PG_GETARG_INT32(1);
bool pretty = PG_GETARG_BOOL(2);
int prettyFlags;
+ char *res;
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_indexdef_worker(indexrelid, colno,
- NULL,
- colno != 0,
- false,
- prettyFlags)));
+
+ res = pg_get_indexdef_worker(indexrelid, colno, NULL, colno != 0, false,
+ prettyFlags, true);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
/* Internal version that returns a palloc'd C string; no pretty-printing */
char *
pg_get_indexdef_string(Oid indexrelid)
{
- return pg_get_indexdef_worker(indexrelid, 0, NULL, false, true, 0);
+ return pg_get_indexdef_worker(indexrelid, 0, NULL, false, true, 0, false);
}
/* Internal version that just reports the column definitions */
int prettyFlags;
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
- return pg_get_indexdef_worker(indexrelid, 0, NULL, true, false, prettyFlags);
+ return pg_get_indexdef_worker(indexrelid, 0, NULL, true, false,
+ prettyFlags, false);
}
/*
pg_get_indexdef_worker(Oid indexrelid, int colno,
const Oid *excludeOps,
bool attrsOnly, bool showTblSpc,
- int prettyFlags)
+ int prettyFlags, bool missing_ok)
{
/* might want a separate isConstraint parameter later */
bool isConstraint = (excludeOps != NULL);
*/
ht_idx = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexrelid));
if (!HeapTupleIsValid(ht_idx))
+ {
+ if (missing_ok)
+ return NULL;
elog(ERROR, "cache lookup failed for index %u", indexrelid);
+ }
idxrec = (Form_pg_index) GETSTRUCT(ht_idx);
indrelid = idxrec->indrelid;
{
Oid constraintId = PG_GETARG_OID(0);
int prettyFlags;
+ char *res;
prettyFlags = PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_constraintdef_worker(constraintId,
- false,
- prettyFlags)));
+
+ res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
Datum
Oid constraintId = PG_GETARG_OID(0);
bool pretty = PG_GETARG_BOOL(1);
int prettyFlags;
+ char *res;
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
- PG_RETURN_TEXT_P(string_to_text(pg_get_constraintdef_worker(constraintId,
- false,
- prettyFlags)));
+
+ res = pg_get_constraintdef_worker(constraintId, false, prettyFlags, true);
+
+ if (res == NULL)
+ PG_RETURN_NULL();
+
+ PG_RETURN_TEXT_P(string_to_text(res));
}
/*
char *
pg_get_constraintdef_command(Oid constraintId)
{
- return pg_get_constraintdef_worker(constraintId, true, 0);
+ return pg_get_constraintdef_worker(constraintId, true, 0, false);
}
/*
*/
static char *
pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
- int prettyFlags)
+ int prettyFlags, bool missing_ok)
{
HeapTuple tup;
Form_pg_constraint conForm;
UnregisterSnapshot(snapshot);
- if (!HeapTupleIsValid(tup)) /* should not happen */
+ if (!HeapTupleIsValid(tup))
+ {
+ if (missing_ok)
+ {
+ systable_endscan(scandesc);
+ heap_close(relation, AccessShareLock);
+ return NULL;
+ }
elog(ERROR, "cache lookup failed for constraint %u", constraintId);
+ }
conForm = (Form_pg_constraint) GETSTRUCT(tup);
operators,
false,
false,
- prettyFlags));
+ prettyFlags,
+ false));
break;
}
default:
/* Look up the function */
proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
if (!HeapTupleIsValid(proctup))
- elog(ERROR, "cache lookup failed for function %u", funcid);
+ PG_RETURN_NULL();
+
proc = (Form_pg_proc) GETSTRUCT(proctup);
name = NameStr(proc->proname);
if (list_length(actions) != 1)
{
- appendStringInfoString(buf, "Not a view");
+ /* keep output buffer empty and leave */
return;
}
if (ev_type != '1' || !is_instead ||
strcmp(ev_qual, "<>") != 0 || query->commandType != CMD_SELECT)
{
- appendStringInfoString(buf, "Not a view");
+ /* keep output buffer empty and leave */
return;
}