Skip to content

Commit

Permalink
Be sure to run the parse analysis before handing over the statement t…
Browse files Browse the repository at this point in the history
…o the rewriter.
  • Loading branch information
dimitri committed Jan 18, 2012
1 parent e9d01ff commit 4bfab63
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 29 deletions.
97 changes: 77 additions & 20 deletions src/backend/tcop/utility.c
Expand Up @@ -152,9 +152,16 @@ CommandIsReadOnly(Node *parsetree)

/*
* Support function for calling the command triggers.
*
* That function is called in standard_ProcessUtility() before entering the
* big'o'switch and before parse analysis have been done for commands that need
* it done, with force set to false. Later in the switch, after parse analysis
* has been done, force is set to true so that we actually call the command
* triggers this time.
*/
static int
call_before_or_insteadof_cmdtriggers(Node *parsetree, CommandContext cmd)
call_before_or_insteadof_cmdtriggers(Node *parsetree, CommandContext cmd,
bool force)
{
switch (nodeTag(parsetree))
{
Expand All @@ -167,7 +174,6 @@ call_before_or_insteadof_cmdtriggers(Node *parsetree, CommandContext cmd)
case T_AlterObjectSchemaStmt:
case T_AlterOwnerStmt:
case T_AlterSeqStmt:
case T_AlterTableStmt:
case T_RenameStmt:
case T_CommentStmt:
case T_DefineStmt:
Expand All @@ -179,16 +185,13 @@ call_before_or_insteadof_cmdtriggers(Node *parsetree, CommandContext cmd)
case T_CreateDomainStmt:
case T_CreateFunctionStmt:
case T_CreateRoleStmt:
case T_IndexStmt:
case T_CreatePLangStmt:
case T_CreateOpClassStmt:
case T_CreateOpFamilyStmt:
case T_AlterOpFamilyStmt:
case T_RuleStmt:
case T_CreateSchemaStmt:
case T_CreateSeqStmt:
case T_CreateStmt:
case T_CreateTableSpaceStmt:
case T_CreateTrigStmt:
case T_CompositeTypeStmt:
case T_CreateEnumStmt:
Expand Down Expand Up @@ -223,14 +226,29 @@ call_before_or_insteadof_cmdtriggers(Node *parsetree, CommandContext cmd)
case T_SecLabelStmt:
return ExecBeforeOrInsteadOfCommandTriggers(parsetree, cmd);

case T_CreateStmt:
case T_CreateTableSpaceStmt:
case T_AlterTableStmt:
case T_IndexStmt:
/* commands that transform the parse tree
*
* we deal with them in their standard_ProcessUtility() branch.
*/
if (force)
return ExecBeforeOrInsteadOfCommandTriggers(parsetree, cmd);
return 0;

default:
/* commands that don't support triggers */
return 0;
}
}

/*
* See the description of call_before_or_insteadof_cmdtriggers() above.
*/
static void
call_after_cmdtriggers(Node *parsetree, CommandContext cmd)
call_after_cmdtriggers(Node *parsetree, CommandContext cmd, bool force)
{
switch (nodeTag(parsetree))
{
Expand All @@ -243,7 +261,6 @@ call_after_cmdtriggers(Node *parsetree, CommandContext cmd)
case T_AlterObjectSchemaStmt:
case T_AlterOwnerStmt:
case T_AlterSeqStmt:
case T_AlterTableStmt:
case T_RenameStmt:
case T_CommentStmt:
case T_DefineStmt:
Expand All @@ -255,16 +272,13 @@ call_after_cmdtriggers(Node *parsetree, CommandContext cmd)
case T_CreateDomainStmt:
case T_CreateFunctionStmt:
case T_CreateRoleStmt:
case T_IndexStmt:
case T_CreatePLangStmt:
case T_CreateOpClassStmt:
case T_CreateOpFamilyStmt:
case T_AlterOpFamilyStmt:
case T_RuleStmt:
case T_CreateSchemaStmt:
case T_CreateSeqStmt:
case T_CreateStmt:
case T_CreateTableSpaceStmt:
case T_CreateTrigStmt:
case T_CompositeTypeStmt:
case T_CreateEnumStmt:
Expand Down Expand Up @@ -298,6 +312,19 @@ call_after_cmdtriggers(Node *parsetree, CommandContext cmd)
case T_CreateForeignTableStmt:
case T_SecLabelStmt:
ExecAfterCommandTriggers(parsetree, cmd);
return;

case T_CreateStmt:
case T_CreateTableSpaceStmt:
case T_AlterTableStmt:
case T_IndexStmt:
/* commands that transform the parse tree
*
* we deal with them in their standard_ProcessUtility() branch.
*/
if (force)
ExecAfterCommandTriggers(parsetree, cmd);
return;

default:
/* commands that don't support triggers */
Expand Down Expand Up @@ -518,7 +545,7 @@ standard_ProcessUtility(Node *parsetree,
cmd.tag = (char *) CreateCommandTag(parsetree);
cmd.cmdstr = NULL;

if (call_before_or_insteadof_cmdtriggers(parsetree, &cmd) > 0)
if (call_before_or_insteadof_cmdtriggers(parsetree, &cmd, false) > 0)
return;

switch (nodeTag(parsetree))
Expand Down Expand Up @@ -697,6 +724,10 @@ standard_ProcessUtility(Node *parsetree,
Datum toast_options;
static char *validnsps[] = HEAP_RELOPT_NAMESPACES;

/* care about command triggers if any */
if (call_before_or_insteadof_cmdtriggers(stmt, &cmd, true) > 0)
return;

/* Create the table itself */
relOid = DefineRelation((CreateStmt *) stmt,
RELKIND_RELATION,
Expand All @@ -718,15 +749,25 @@ standard_ProcessUtility(Node *parsetree,
true);

AlterTableCreateToastTable(relOid, toast_options);

/* now care about after command triggers */
CommandCounterIncrement();
call_after_cmdtriggers(stmt, &cmd, true);
}
else if (IsA(stmt, CreateForeignTableStmt))
{
if (call_before_or_insteadof_cmdtriggers(stmt, &cmd, true) > 0)
return;

/* Create the table itself */
relOid = DefineRelation((CreateStmt *) stmt,
RELKIND_FOREIGN_TABLE,
InvalidOid);
CreateForeignTable((CreateForeignTableStmt *) stmt,
relOid);

CommandCounterIncrement();
call_after_cmdtriggers(stmt, &cmd, true);
}
else
{
Expand All @@ -737,11 +778,11 @@ standard_ProcessUtility(Node *parsetree,
false,
None_Receiver,
NULL);
}

/* Need CCI between commands */
if (lnext(l) != NULL)
CommandCounterIncrement();
/* Need CCI between commands */
if (lnext(l) != NULL)
CommandCounterIncrement();
}
}
}
break;
Expand Down Expand Up @@ -896,8 +937,16 @@ standard_ProcessUtility(Node *parsetree,

if (IsA(stmt, AlterTableStmt))
{
/* care about command triggers if any */
if (call_before_or_insteadof_cmdtriggers(stmt, &cmd, true) > 0)
return;

/* Do the table alteration proper */
AlterTable(relid, lockmode, (AlterTableStmt *) stmt);

/* after command trigger */
CommandCounterIncrement();
call_after_cmdtriggers(stmt, &cmd, true);
}
else
{
Expand All @@ -908,11 +957,11 @@ standard_ProcessUtility(Node *parsetree,
false,
None_Receiver,
NULL);
}

/* Need CCI between commands */
if (lnext(l) != NULL)
CommandCounterIncrement();
/* Need CCI between commands */
if (lnext(l) != NULL)
CommandCounterIncrement();
}
}
}
break;
Expand Down Expand Up @@ -1079,6 +1128,10 @@ standard_ProcessUtility(Node *parsetree,
/* Run parse analysis ... */
stmt = transformIndexStmt(stmt, queryString);

/* care about command triggers if any */
if (call_before_or_insteadof_cmdtriggers((Node *)stmt, &cmd, true) > 0)
return;

/* ... and do it */
DefineIndex(stmt->relation, /* relation */
stmt->idxname, /* index name */
Expand All @@ -1100,6 +1153,10 @@ standard_ProcessUtility(Node *parsetree,
false, /* skip_build */
false, /* quiet */
stmt->concurrent); /* concurrent */

/* after command trigger */
CommandCounterIncrement();
call_after_cmdtriggers((Node *)stmt, &cmd, true);
}
break;

Expand Down Expand Up @@ -1374,7 +1431,7 @@ standard_ProcessUtility(Node *parsetree,
(int) nodeTag(parsetree));
break;
}
call_after_cmdtriggers(parsetree, &cmd);
call_after_cmdtriggers(parsetree, &cmd, false);
}

/*
Expand Down
24 changes: 15 additions & 9 deletions src/backend/utils/adt/ruleutils.c
Expand Up @@ -4885,6 +4885,18 @@ get_rule_expr(Node *node, deparse_context *context,
get_const_expr((Const *) node, context, 0);
break;

case T_A_Const:
{
A_Const *con = (A_Const *) node;
Value *val = &con->val;

if (val->type == T_Integer)
appendStringInfo(buf, "%ld", intVal(val));
else
appendStringInfo(buf, "%s", strVal(val));
break;
}

case T_Param:
get_parameter((Param *) node, context);
break;
Expand Down Expand Up @@ -7600,22 +7612,16 @@ _rwColQualList(StringInfo buf, List *constraints, const char *relname)
case CONSTR_CHECK:
{
char *consrc;
List *context;

context = deparse_context_for(relname, InvalidOid);
consrc = deparse_expression(c->raw_expr, context, false, false);
consrc = deparse_expression(c->raw_expr, NIL, false, false);
appendStringInfo(buf, " CHECK (%s)", consrc);
break;
}

case CONSTR_DEFAULT:
{
char *consrc;
List *context;

context = deparse_context_for(relname, InvalidOid);
consrc = deparse_expression(c->raw_expr, context, false, false);
appendStringInfo(buf, " DEFAUT %s", consrc);
consrc = deparse_expression(c->raw_expr, NIL, false, false);
appendStringInfo(buf, " DEFAULT %s", consrc);
break;
}

Expand Down

0 comments on commit 4bfab63

Please sign in to comment.