Re: To know what a macro does

Lists: pgsql-hackers
From: Werner Echezuria <wercool(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: To know what a macro does
Date: 2009-04-26 20:50:41
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Hi, I've been trying to sort a column that performs some calculations, but
postgres says this: ERROR: invalid attnum: -12851. I was searching on the
source code, and I guess the error araises around this macro:
/*
* Copy the given tuple into memory we control, and decrease availMem.
* Then call the common code.
*/
COPYTUP(state, &stup, (void *) slot);

So, I'd like to know what COPYTUP does?, what is availMem?, How can I tell
postgres it is a valid attnum?

Thanks for any help!!!!


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Werner Echezuria <wercool(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: To know what a macro does
Date: 2009-04-26 21:15:29
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Apr 26, 2009 at 04:20:41PM -0430, Werner Echezuria wrote:
> Hi, I've been trying to sort a column that performs some calculations, but
> postgres says this: ERROR: invalid attnum: -12851. I was searching on the
> source code, and I guess the error araises around this macro:

I'm pretty sure this is a "not supposed to happen" thing. Do you have a
repoducable test case? Also, what version of postgres?

> /*
> * Copy the given tuple into memory we control, and decrease availMem.
> * Then call the common code.
> */
> COPYTUP(state, &stup, (void *) slot);
>
> So, I'd like to know what COPYTUP does?, what is availMem?, How can I tell
> postgres it is a valid attnum?

COPYTUP does exactly what the comment says it does. I'm guessing this
is in the sort code somewhere? An "attnum" is a column number, like the
first column is attnum 1. Attnum -12851 is definitly bogus.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.


From: Werner Echezuria <wercool(at)gmail(dot)com>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: To know what a macro does
Date: 2009-04-27 01:03:42
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Well, I do a query like this: "SELECT * FROM historial WHERE
id_grupo=grupo_hist ORDER BY grmemb LIMIT 10;", then in transformSortClause
I know it this way:

/*
* transformSortClause -
* transform an ORDER BY clause
*
* ORDER BY items will be added to the targetlist (as resjunk columns)
* if not already present, so the targetlist must be passed by reference.
*/
List *
transformSortClause(ParseState *pstate,
List *orderlist,
List **targetlist,
bool resolveUnknown)
{
List *sortlist = NIL;
ListCell *olitem;

foreach(olitem, orderlist)
{
SortBy *sortby = lfirst(olitem);
TargetEntry *tle;
//To find out if it is the membership degree
char *namegrmemb = strVal(linitial(((ColumnRef *)
sortby->node)->fields));

*if (strcmp(namegrmemb, "grmemb")==0)
* tle = createTargetFuzzyEntry(targetlist);
else
tle = findTargetlistEntry(pstate, sortby->node,
targetlist, ORDER_CLAUSE);

sortlist = addTargetToSortList(pstate, tle,
sortlist, *targetlist,
sortby->sortby_dir,
sortby->sortby_nulls,
sortby->useOp,
resolveUnknown);

}

return sortlist;
}

|***************************************************************************************************************************|

Then I created a function that includes a new target entry in the
targetlist:

//To sort the membership degree
TargetEntry *
createTargetFuzzyEntry(List **targetlist){

/*I just have to create the fuzzy target entry right here */
TargetEntry *tfp = makeNode(TargetEntry);
Const *cn = makeNode(Const);
float val = 1.0;
TargetEntry *tlast = list_nth(*targetlist, list_length(*targetlist)-1);

cn = makeConst(700, -1, 4, (Float4GetDatum(val)), false, true);
tfp->resorigtbl=tlast->resorigtbl;
tfp->expr = (Expr *) cn;
tfp->resno = list_length(*targetlist) + 1;
tfp->resname = "grmemb";
tfp->resorigcol = list_length(*targetlist) + 1;
tfp->ressortgroupref = 0;
tfp->resjunk = false;

*targetlist = lappend(*targetlist, tfp);

return tfp;
}

|*************************************************************************************************************************|

Later in planner.c on grouping_planner function I do something like this:

/*
* If we were not able to make the plan come out in the right order, add
* an explicit sort step.
*/
if (parse->sortClause)
{
if (!pathkeys_contained_in(sort_pathkeys, current_pathkeys) ||
parse->hasGrMemb)
{
result_plan = (Plan *) make_sort_from_pathkeys(root,
result_plan,
sort_pathkeys,
limit_tuples);
current_pathkeys = sort_pathkeys;
}
}

2009/4/26 Martijn van Oosterhout <kleptog(at)svana(dot)org>

> On Sun, Apr 26, 2009 at 04:20:41PM -0430, Werner Echezuria wrote:
> > Hi, I've been trying to sort a column that performs some calculations,
> but
> > postgres says this: ERROR: invalid attnum: -12851. I was searching on
> the
> > source code, and I guess the error araises around this macro:
>
> I'm pretty sure this is a "not supposed to happen" thing. Do you have a
> repoducable test case? Also, what version of postgres?
>
> > /*
> > * Copy the given tuple into memory we control, and decrease
> availMem.
> > * Then call the common code.
> > */
> > COPYTUP(state, &stup, (void *) slot);
> >
> > So, I'd like to know what COPYTUP does?, what is availMem?, How can I
> tell
> > postgres it is a valid attnum?
>
> COPYTUP does exactly what the comment says it does. I'm guessing this
> is in the sort code somewhere? An "attnum" is a column number, like the
> first column is attnum 1. Attnum -12851 is definitly bogus.
>
> Have a nice day,
> --
> Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> > Please line up in a tree and maintain the heap invariant while
> > boarding. Thank you for flying nlogn airlines.
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iD8DBQFJ9M7xIB7bNG8LQkwRAviuAJ9F9GeldnVLInum3ZaT0IKTNvk3dACdElyo
> 7VZ7cLAgu1q4PncHS8rVYJU=
> =Y5Qw
> -----END PGP SIGNATURE-----
>
>


From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Werner Echezuria <wercool(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: To know what a macro does
Date: 2009-04-27 06:21:23
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

On Sun, Apr 26, 2009 at 08:33:42PM -0430, Werner Echezuria wrote:
> Well, I do a query like this: "SELECT * FROM historial WHERE
> id_grupo=grupo_hist ORDER BY grmemb LIMIT 10;", then in transformSortClause
> I know it this way:

Ok, this is way over my head. But really, it would be helpful to know
what you're trying to achieve. Why are you changing the source code?

Maybe someone else can answer your question specifically.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Please line up in a tree and maintain the heap invariant while
> boarding. Thank you for flying nlogn airlines.


From: Werner Echezuria <wercool(at)gmail(dot)com>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: To know what a macro does
Date: 2009-04-27 12:28:50
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Well, I'm in a project called PostgreSQLf and we're trying to include fuzzy
logic inside PostgreSQL. Now I've been thinking this is getting too hard, do
you know if I can just sort the results with something like this :
Sort(ResultSlot,column)?, I mean without the Order By clause?

2009/4/28 Martijn van Oosterhout <kleptog(at)svana(dot)org>

> On Sun, Apr 26, 2009 at 08:33:42PM -0430, Werner Echezuria wrote:
> > Well, I do a query like this: "SELECT * FROM historial WHERE
> > id_grupo=grupo_hist ORDER BY grmemb LIMIT 10;", then in
> transformSortClause
> > I know it this way:
>
> Ok, this is way over my head. But really, it would be helpful to know
> what you're trying to achieve. Why are you changing the source code?
>
> Maybe someone else can answer your question specifically.
>
> Have a nice day,
> --
> Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> > Please line up in a tree and maintain the heap invariant while
> > boarding. Thank you for flying nlogn airlines.
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
>
> iD8DBQFJ9U7jIB7bNG8LQkwRAhDPAJ9+q/XSGmUo0t2jxdWHtvVKhPt/mACdEoOn
> g9bLCsAzjy0oFBMsjvi7Rfg=
> =Mofr
> -----END PGP SIGNATURE-----
>
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Werner Echezuria <wercool(at)gmail(dot)com>
Cc: Martijn van Oosterhout <kleptog(at)svana(dot)org>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: To know what a macro does
Date: 2009-04-27 14:24:39
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

Werner Echezuria <wercool(at)gmail(dot)com> writes:
> Later in planner.c on grouping_planner function I do something like this:

Well, you've omitted showing us the code where the problem is likely to
be, but I am kinda thinking that you've shot yourself in the foot by
trying to represent your special ordering clause as a simple constant.
The planner is quite smart enough to throw away "order by constant"
as a no-op. By the time you get down to the pathkey logic it's just
going to be ignoring that clause entirely; and if you try to brute-force
it you're more than likely going to break something.

Rather than kluging up any of this code, I wonder whether you couldn't
represent your fuzzy sorting requirement as ORDER BY some_function(...)
and put all the smarts into that function.

regards, tom lane


From: Werner Echezuria <wercool(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org, kleptog(at)svana(dot)org
Subject: Re: To know what a macro does
Date: 2009-04-27 15:19:33
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Lists: pgsql-hackers

2009/4/28 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>

> Well, you've omitted showing us the code where the problem is likely to
> be, but I am kinda thinking that you've shot yourself in the foot by
> trying to represent your special ordering clause as a simple constant.
> The planner is quite smart enough to throw away "order by constant"
> as a no-op. By the time you get down to the pathkey logic it's just
> going to be ignoring that clause entirely; and if you try to brute-force
> it you're more than likely going to break something.
>

Well, I know I'm breaking something, because when I execute "\d" in psql,
the server hangs out.

When I performs the order by clause, I call a function in
transformSelectStmt on analyze.c (This is parse->hasGrMemb, that I call in
grouping_planner ) :

qry->hasGrMemb = hassSortByGrMemb(stmt->sortClause);

|****************************************************************************************************|

Then the function is this (on parse_clause.c):

//To know if the query has an ORDER BY grmemb
bool
hassSortByGrMemb(List *orderlist){

ListCell *olitem;
bool result;

result=false;
foreach(olitem, orderlist)
{
SortBy *sortby = lfirst(olitem);
char *namegrmemb = strVal(linitial(((ColumnRef *)
sortby->node)->fields));

if (strcmp(namegrmemb, "grmemb")==0)
result=true;
}

return result;

}

> Rather than kluging up any of this code, I wonder whether you couldn't
> represent your fuzzy sorting requirement as ORDER BY some_function(...)
> and put all the smarts into that function.
>
> Well, the project force me to include all the source in the core. That is
why I wanna know if there's any chance that I could sort the final
resultSlot?.

regards